Git Product home page Git Product logo

ocaml-mustache's Introduction

ocaml-mustache

mustache.js logic-less templates in OCaml

Example usage

let tmpl =
  try
    Mustache.of_string "Hello {{name}}\n\
                        Mustache is:\n\
                        {{#qualities}}\
                        * {{name}}\n\
                        {{/qualities}}"
  with Mustache.Parse_error err ->
    Format.eprintf "%a@."
      Mustache.pp_template_parse_error err;
    exit 3

let json =
  `O [ "name", `String "OCaml"
     ; "qualities", `A [ `O ["name", `String "awesome"]
                       ; `O ["name", `String "simple"]
                       ; `O ["name", `String "fun"]
                       ]
     ]

let rendered =
  try Mustache.render tmpl json
  with Mustache.Render_error err ->
    Format.eprintf "%a@."
      Mustache.pp_render_error err;
    exit 2

Supported template language

ocaml-mustache accepts the whole Mustache template language, except:

  • it does not support setting delimiter tags to something else than '{{' and '}}'.
  • it does not support lambdas inside the provided data

It is automatically tested against the latest mustache specification testsuite.

ocaml-mustache also supports template inheritance / partials with parameters, tested against the semi-official specification.

Todo/Wish List

  • Support for ropes

http://mustache.github.io/

ocaml-mustache's People

Contributors

alessandro-barbieri avatar anton-trunov avatar armael avatar avsm avatar gasche avatar gmcninch-tufts avatar lysxia avatar mk270 avatar mseri avatar paurkedal avatar psafont avatar rgrinberg avatar sagotch avatar seliopou avatar sonologico avatar toby-m avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ocaml-mustache's Issues

Clarify licensing

You do mention the MIT license in the _oasis and opam files, but you do not embed the license's text into your source files.

I needed to copy/paste some of the source files in my code base so I added the license text as found here in them, with your full name and the year 2015. Are you okay with it?

Float values holding integers are rendered with a trailing period

(version: 2.1.0)

# Mustache.(render (of_string "{{foo}}") @@ `O [ "foo", `Float 3. ]);;
- : string = u"3."

Mustache uses string_of_float, which adds the trailing '.'.

The proper way to render float values is not specified in the mustache (5) manpage, but there's an example which does show an integer being rendered without decimal part, and http://trymustache.com behaves that way too.

Precise control over the formatting requires going through String values, but removing the trailing period for integer values seems sensible.

consider changing the binary name?

Currently mustache_cli.ml is installed as a mustache binary in the user PATH. This is a natural choice that is easy for users to guess, but it has the downside of conflicting with the binary name chosen by... any other Mustache implementation. For example, the original implementation in Ruby provides the mustache(1) command that is easy to find over the net. If you run gem install mustache, it installs a Ruby library and also a mustache binary. (When trying to compare the behavior of the two tools, I would keep uninstalling and reinstalling the Ruby tool to avoid name conflicts.)

Should one consider changing the name of the OCaml tool to avoid naming conflicts with other implementations? I'm not saying that the name should change, but I'm creating this issue to get an idea of what people (and in particular @rgrinberg) think.

Naming conflicts are maybe not much of an issue, given that most users are going to use just one tool anyway. But some systems/distributions want to avoid conflicts globally; if someone someday decided to package ocaml-mustache for Debian, then the naming clash would be an annoying issue to deal with.

My only idea for an alternative name would be mustacho, which sounds somewhat like a contraction of "mustache in OCaml", and is an alternative spelling for "mustache" in some languages (in English it exists, but "mustachio" is more commonly used; it exists in some variants of Spanish but "mostacho" is more common, etc.). (I'm afraid that camels don't have mustaches; search for this on the web led me to the Mustache Contest at the Pushkar Camel Fair, which does not bring any clear name suggestion.)

Please show example how to use this library

I am a newbie to Ocaml. After 2 hours searching source code and figure out types, I still have no idea how to use this library to render simple template.

Please add more example in README.md to guide some newbie like me. We are not as smart as you are.

command-line output

Hi,

Currently -- (i.e. with

> george@calliope:~$ opam list | grep mustache
> mustache                      3.1.0       Mustache logic-less templates in OCaml 

)

if I do

$ mustache foo.json bar.template

then as specified in mustache_cli.ml the result is to first print_endline the content of foo.json, followed by the output of apply_mustache.

Outputting the json at the top feels a bit like a debugging step that wasn't removed, though maybe there is some reason you want to see this bit in the output? Anyhow, I happened to find a use for the tool and noticed this property. (The extra "json" actually gave me a motive to learn to use sed to snip the stuff at the top of the output -- somehow I'd always avoided sed til now! )

Anyhow, I thought I'd report the matter.

Thanks, and all the best,
george

Parsing errors on invalid templates are uncaught, unclear, and unhelpful

A minimal example:

let () =
  ignore (Mustache.of_string "{{.name}}")

Which crashes with

Fatal error: exception Mustache_parser.MenhirBasics.Error

A first-level fix would be to catch this error and raise something a bit more informative (e.g., Invalid_mustache_template). A better fix would be to identify the location of the syntax error.

I'm happy to help with the first part, at least, if that would be welcome.

Online HTML docs

It would be nice to put the ocamldoc-generated docs of the API online. For that we need a make doc target, and I don't know how to do that with jbuilder.

Nested sections fail to parse

I came across this while trying to write the following template fragment:

{{#scripts}}
<script src="{{& src}}" {{#charset}}charset="{{& charset}}">{{/charset}}</script>
{{/scripts}}

Attempting to parse this resulted in the following exception:

Mustache_types.Invalid_template("Mismatched section charset with scripts")

Example JSON:

{
  "scripts" : [
    {
      "src" : "d3.js",
      "charset" : "utf-8"
     }
  ] 
}

Invalid param "not an object" when using {{#tests}} on a non-object

Hello,

I was trying to use Mustache to render a string only when it is non-null. This should essentially be the following template:

Hello
{{#key}}- The key exists: {{key}}{{/key}}
Good bye

It works as expected with the Ruby implementation of Mustache in both cases. It does not however work with the OCaml implementation in the case where key exists. I get Mustache_types.Invalid_param("str. not an object").

My guess is that Mustache is happy with the test {{#key}} and puts the JSON for key in the context. When later rendering {{key}} it fails because the context is not an object. It's actually a bit more than a guess:

In render_fmt, when rendering sections, we have:

| context -> render' indent s.contents (add_context context js)

where add_context is:

let add_context ctx js =
match (ctx, js) with
| `O ctx_o, `O js_o -> `O (ctx_o @ js_o)
| _, _ -> ctx
in

which means that when ctx is not an object, the context becomes this non-object. And when we try to render something afterwards, we have a failure. I would rather have something like:

    let add_context ctx js =
      match (ctx, js) with
      | `O ctx_o, `O js_o -> `O (ctx_o @ js_o)
      | _, `O _ -> js
      | _, _ -> ctx
    in

Does that seem reasonable to you?

a new Mustache release?

It would be nice if we had a new Mustache release; currently I write code with mustache pinned on master, but this makes distributing the resulting software awkward.

In an ideal world, we would have found time to solve #61, #66 and maybe even #40 before the next release.
In practice we are all very busy, and having a release in the current state would be strictly more convenient (for me) than no release at all.

(If someone, presumably @rgrinberg, is willing to take care of the release, and has a small amount of time to look at some of those issues, I would suggest maybe trying to merge #66 (package split), using mustache-ocaml (my best suggestion in #61) as the binary name, squashing those two issues at once. Then we can discuss #40 on its own terms, independently of the release schedule; it sounds like more work.)

Documentation generation is broken

See output:

$ make doc
dune build @doc
File "lib/mustache.mli", line 79, character 4 to line 80, character 65:
Warning: '{| try ignore (Mustache.of_string "{{!") with Mustache.Parse_error err ->
    Format.eprintf "%a@." Mustache.pp_template_parse_error err |}': bad markup.
Suggestion: did you mean '{!| try ignore (Mustache.of_string "{{!") with Mustache.Parse_error err ->
    Format.eprintf "%a@." Mustache.pp_template_parse_error err |}' or '[| try ignore (Mustache.of_string "{{!") with Mustache.Parse_error err ->
    Format.eprintf "%a@." Mustache.pp_template_parse_error err |]'?

The delimiters for the ocaml code here are not expected by odoc-parser, {[ and ]} should be used instead

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.