Orasund/elm-handlebars/1.0.2

Handlebars

compiles Handlebars templates.

Compile Templates

type alias Template =

Template

type Error

Possible error are either syntactical or semantical.

compile : Config -> String -> Value -> Result Error String

parses the template and then evaluates it.

compile config string json =
    parse string
        |> Result.mapError SyntaxError
        |> Result.andThen
            (\expressions ->
                eval config expressions json
                    |> Result.mapError SemanticError
            )

Tests:

import Parser
import Handlebars.Expression as Expression exposing (Expression(..), SubExp(..))
import Result.Extra as Result
import Handlebars
import Json.Decode as D


compile : String -> String -> (Result Error String)
compile  template value =
    case value |> D.decodeString D.value of
        Ok v ->
            v
                |> Handlebars.compile Handlebars.defaultConfig
                    template
        Err err -> Ok (D.errorToString err)


"{ \"valid\":true }"
    |> compile "Hello There"
    --> (Ok "Hello There")
defaultConfig : Config

the default config.

errorToString : Error -> String

Print the error in a nice and readable way.

Advanced

eval : Config -> Template -> Value -> Result Error String

evaluate a template using a json value

parse : String -> Result (List DeadEnd) Template

parse a string into a template