Orasund / elm-handlebars / Handlebars

compiles Handlebars templates.

Compile Templates


type alias Template =
List Expression

Template


type Error

Possible error are either syntactical or semantical.

compile : Expression.Config -> String -> Json.Encode.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 : Expression.Config

the default config.

errorToString : Error -> String

Print the error in a nice and readable way.

Advanced

eval : Expression.Config -> Template -> Json.Encode.Value -> Result Expression.Error String

evaluate a template using a json value

parse : String -> Result (List Parser.DeadEnd) Template

parse a string into a template