npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elm-mustache

v1.0.0

Published

Mustache template specification in Elm

Downloads

4

Readme

elm-mustache

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.

Should I use the Elm package or the NPM package?

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.

The Elm package

See https://package.elm-lang.org/packages/emmabastas/elm-mustache/latest/

The NPM package

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 file mytemplate.mustache

{{#section}}
Hello {{world}}!
{{/section}}

Then you can generate a typesafe rendering function with elm-mustache ./mytemplate.mustache ./src/Mustache/MyTemplate.elm Mustache.MyTemplate, which will give you the Elm module:

module Mustache.MyTemplate exposing (..)

render : Context a -> String
render c =
    (if c.section then ("""Hello """ ++ htmlEscape c.world ++ """!
""") else "")

type alias Context a =
    { a
        | section : Bool
        , world : String
    }

One the other hand, if you run elm-mustache again but with the --full flag you'd get the "full" version:

module Mustache.MyTemplate exposing (..)

import Json.Decode exposing (Value)
import Mustache exposing (htmlEscape, lookup, interpolate, section, invertedSection)

render : Value -> String
render json = (\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:

import Json.Encode exposing (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

Why is can't the typesafe version deal with lists?

When presented with the template

{{#section}}
{{foo}}?
{{bar}}!!
{{/section}}

I can't tell if the foo and bar 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 the Context type should be

{ section :
    List { foo : String
         , bar : String
         }
}

or

{ section : Bool
, foo : String
, bar : String
}

or something in between.

Summary of capabilities and spec compliance

| | 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? | ✅ | ✅ | ❌ |

Testing

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 at https://github.com/mustache/spec

Contributing

Yes please :-) Feel free to email me or open an issue.

TODOs

This package isn't immensely high-quality, hopefully it can get the job done for you. Some things that could be nice:

  • Integration tests for the CLI tool
  • Make a elm-codegen generator to replace the CLI tool
  • Use Parser.Advanced to provide useful error messages and fault-tolerant parsing.
  • Unit tests pertaining to weird variable names

License

GPLv3