lettenj61/elm-simple-template/1.0.0

Template

This module provides Mustache (or Handlebars) like template syntax.

Template

A compiled template function, accepts Params and render string.

Can be configured by Options.

type alias Options =
{ trim : Bool
, placeholder : String
}

An Options value is used to configure rendering of a template.

Currently supports:

  • trim - Trim spaces around interpolated value. Defaults to True
  • placeholder - Specify placeholder text when no associated value is found in template parameter. Defaults to empty string ("").
compile : Options -> String -> Template

Compiles string into a template function.

To render template with some parameters, use run.

tmpl : Template
tmpl =
    compile defaultOptions <|
        "Hello, {{name}}"
defaultOptions : Options

The default configuration for a template.

Rendering

run : Params -> Template -> String

Apply template function to some parameters and get rendered text.

See Template.Params module to how you can interact with Params value.

params : Params
params =
    Params.fromList
        [ ( "browser", "Firefox" )
        , ( "language", "JavaScript" )
        ]

t : Template
t =
    compile defaultOptions "My favorite is {{browser}} and I can use {{language}}!"

str : String
str =
    run params t
    -- "My favorite is Firefox and I can use JavaScript!"
render : Params -> Options -> String -> String

Render template string directly with given parameters and options.

Useful when you do not need the compiled function. A quicker way to run!

render
    (Params.fromList [( "food", "Okonomiyaki" )])
    defaultOptions
    "Give me some {{food}}"
renderWithDefault : Params -> String -> String

Same as render but uses default options.

The quickest way to run!

renderWithDefault
    (Params.fromList [( "animal", "dog" )])
    "How this {{animal}} sleep?"