- Notifications
You must be signed in to change notification settings - Fork1
Mustache template specification in Elm
License
emmabastas/elm-mustache
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Mustache templates in Elm.
There are two parts to this package:
- The Elm package with which you can parse and render mustache templates in the browser.
- The NPM package with which you can parse a mustache template in advance and generate a typesafe™ rendering function for use in the browser.
If you have.mustache templates already that you'd want to render with different hashes in the browser then you should probably use the NPM package.If you have a use-case where you can't know in advance what the template will be, then you should use the Elm package.If you don't already have.mustache templates ready and have found this package because you're looking for a nice templating experience in Elm, then you are probably better off writing some rendering functions in Elm directly with the help of multiline strings, the++ operator.
Seehttps://package.elm-lang.org/packages/emmabastas/elm-mustache/latest/
There are two types of rendering functions that can be created: A typesafe but more limited version (It can't iterate a section), and a full version.
Let's say you have a filemytemplate.mustache
{{#section}}Hello{{world}}!{{/section}}
Then you can generate a typesafe rendering function withelm-mustache ./mytemplate.mustache ./src/Mustache/MyTemplate.elm Mustache.MyTemplate, which will give you the Elm module:
moduleMustache.MyTemplateexposing (..)render:Contexta->Stringrenderc=(ifc.sectionthen("""Hello"""++ htmlEscape c.world++"""!""")else"")type aliasContext a={ a| section:Bool, world:String}
One the other hand, if you runelm-mustache again but with the--full flag you'd get the "full" version:
moduleMustache.MyTemplateexposing (..)importJson.Decodeexposing (Value)importMustacheexposing (htmlEscape,lookup,interpolate,section,invertedSection)render:Value->Stringrenderjson=(\context0->(section context0["section"](\context1->"""Hello"""++ htmlEscape(interpolate(lookup context1["world"]))++"""!""")))[json]
While the typesafe version is better in the sense that you can never forget to supply some variable, and typos will be caught by at compile time, it is worse in the sense that it can't iterate a section. With the full version you can call it with:
importJson.Encodeexposing (list,string)render(object[("world", string"Earth"),("world", string"Venus"),("world", string"Pluto")])
which will produce the string
Hello Earth!Hello Venus!Hello Pluto!This is not possible with the typesafe version
When presented with the template
{{#section}}{{foo}}?{{bar}}!!{{/section}}I can't tell if thefoo andbar variables are supposed to change for every iteration of the section, or if they should remain the same, in other words I don't know if theContext type should be
{ section:List{ foo:String, bar:String}}
or
{ section:Bool, foo:String, bar:String}
or something in between.
elm-mustache | elm-mustache --full | Elm package | |
|---|---|---|---|
| variables | ✅ | ✅ | ✅ |
| boolean section | ✅ | ✅ | ✅ |
| list section | ❌ | ✅ | ✅ |
| inverted section | ✅ | ✅ | ✅ |
| set delimiters | ✅ | ✅ | ✅ |
| lambdas | ❌ | ❌ | ❌ |
| partials | ❌ | ❌ | ✅ |
| inheritance | ❌ | ❌ | ❌ |
| dynamic names | ❌ | ❌ | ❌ |
respects\r\n-style line endings? | ✅ | ✅ | ❌ |
npx elm-test to run all unit tests. All unit tests are generated from./tests/specs/*.json which have been fetched from the mustache test suite over athttps://github.com/mustache/spec
Yes please :-)Feel free to email me or open an issue.
Nice-to-haves
elm-codegengenerators to replace the CLI tool (in a separate package)- Fuzzy tests.
v2.0.0 roadmap
- Use
Parser.Advancedto provide useful error messages - Fault-tolerant parsing -- always return a parsed template, along with a list of eventual parse errors.
- What does the spec say about error recovery in parsing?
- Figure out what the rendering strategy is -- Right now, missing keys and so on are silently rendered as empty strings, either be explicit about this or do something else.
- Respect
\r\n-style line endings. - Use a
Hashtype instead ofJson.Value. - The
ContextType is some sort of tree zipper, not a stack (make impossible state impossible.) - Make the implementation be more along the make-impossible-states-impossible philosophy.
GPLv3
About
Mustache template specification in Elm
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.