janl / mustache.js

Minimal templating with {{mustaches}} in JavaScript

Home Page:https://mustache.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can a function could be called from json in mustache CLI?

ph1-xyz opened this issue · comments

commented

Hi!
I'm trying to execute from mustacheCLI a function residing in the dataView.json. This brings me an error, saying that JSON cannot contain functions...

How can one use mustache's functionality to execute functions from CLI?

Thanks

Hi there!

The error you're describing is a JSON limitation; functions cannot be serialised into JSON.

So in #717 we allowed our CLI to be given a .js file as the view, instead of a .json file. If we use the test files (./test/_files/cli_js_view_with_function.js & ./test/_files/cli_js_view_with_function.mustache) we got in this repo as an example, you could use the CLI to render the output:

$ mustache cli_js_view_with_function.js cli_js_view_with_function.mustache

Does that make sense?

commented

Thank you very much, I'm going to try immediately.

I also commented here explaining a bit what I was trying to do, hope what you just pointed out solves it!

Edit: It surely works. At the moment it behaves weirdly in my code, but it must be me. Anyways, this is a great improvement for the library, it could be worth to be mentioned in the Read me!

Thanks for confirming it works as intended!

Good suggestion mentioning the fact that .js could also be used in addition to .json, feel free to open a PR with an improvement 😄

Closing this for now as your question has been answered.