package handlebars-ml

All checked builds pass
  1. Overview
  2. Docs
Handlebars templating for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.3.1.tar.gz
md5=b77cb69ac47130e5caf1d800875c21b2
sha512=a60a707427fb84c7421f9f830b1310abe627810659fdeded987f97f768d1409a0cf1899ec734639b8166589a67559c42fb1a82f9c4723b6bd0cac615972c302c

Description

OCaml port of the Handlebars.js templating engine

Tags

handlebars template

Added to opam-repository:

README

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

Dependencies (4)

  1. ppx_deriving < "6.2.0"
  2. yojson >= "2.0"
  3. ocaml >= "4.14"
  4. dune >= "3.18"

Dev Dependencies (2)

  1. odoc with-doc
  2. alcotest with-test

Used by (1)

  1. raga

Conflicts

None

Build status

Results of building handlebars-ml with each compiler on check.ci.ocaml.org, run of against opam-repository 7cdc698. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
0.3.1 (this version) n/a good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure