package handlebars-ml

  1. Overview
  2. Docs
Handlebars templating for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.3.0.tar.gz
md5=5c8c012fc52c589788f9fd18a3fc7cef
sha512=1d17d3434440efd40930b8b02547cc69a780d7040cf5f4a29cdbee8844acf0b869d75035fce7bfcf01ab5504fef06053c5de0ff038152d876ccbee0ec563472f

doc/README.html

handlebars-ml

Handlebars templating for OCaml.

Installation

Install with opam:

opam install handlebars-ml

CLI usage

opam install handlebars-ml
handlebars-ml -d data.json template.hbs

Optionally, pass a partials directory with -p partials_dir.

Pass the -h (or --help) flag to see all options.

handlebars-ml -h

Library usage

Use Yojson values for passing JSON data as template context.

open Handlebars_ml

let () =
    let data = Yojson.Safe.from_string {| { "name": "World" } |} in
    let template = "Hello, {{name}}!" in
    match Handlebars.compile template data with
    | Ok result -> print_endline result
    | Error err -> prerr_endline ("Error: " ^ err)

Pass get_helper and get_partial functions as named arguments to compile to use custom helpers and partials. Use JSON types for arguments and return values.

open Handlebars_ml

let custom_get_helper name =
    let shout = function
    | [ `String s ] -> Some (`String (String.uppercase_ascii arg))
    | _ -> None
    in
    match name with
    | "shout" -> Some shout
    | _ -> Handlebars.default_get_helper name

let () =
    let data = Yojson.Safe.from_string {| { "name": "World" } |} in
    let template = "Hello, {{shout name}}!" in
    match Handlebars.compile ~get_helper:custom_get_helper template data with
    | Ok result -> print_endline result
    | Error err -> prerr_endline ("Error: " ^ err)

handlebars-ml provides a default_get_helper function in Handlebars_ml that can be used in your custom implementation to still keep the default helpers.

License

MIT