Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print JSON data structures as a value using CLI #31

Closed
metmajer opened this issue Jan 27, 2019 · 5 comments
Closed

Print JSON data structures as a value using CLI #31

metmajer opened this issue Jan 27, 2019 · 5 comments

Comments

@metmajer
Copy link

Hi, and thanks for maintaining this great project! I am trying to find a way to present random JSON data structures in an HTML file that is rendered using the mustache CLI.

Given the following example:

["a", "b"]

...I'd like to see the following ]results:

["a", "b"]

...but get:

[a b]

in the resulting document. Any ideas on how to achieve this goal? Thanks!

@cbroglie
Copy link
Owner

Hi @metmajer, to make sure I understand the ask, can you please share a small example program that demonstrates the issue?

@metmajer
Copy link
Author

metmajer commented Jan 29, 2019

Hi @cbroglie, and thanks for your reponse. Sure, here's a reproducible example:

File: data.js

{
    "data": {
        "a": [],
        "b": ["foo", "bar"],
        "c": "baz"
    }
}

File: index.html.tmpl

<h1>Data</h1>

<p>
{{{ data }}}
</p>

Finally, mustache data.js index.html.tmpl yields:

<h1>Data</h1>

<p>
map[a:[] b:[foo bar] c:baz]
</p>

Expected:

<h1>Data</h1>

<p>
{ "a": [], "b", ["foo", "bar"], "c": "baz" }
</p>

Obviously, {{ data }} yields a string representation of the internal Golang data structure. But this not expected, if the input data is JSON. I am using v1.0.1.

@cbroglie
Copy link
Owner

AFAIK there is nothing in the mustache spec that says it should automatically output JSON here. If you want to output JSON, then you should call json.Marshal on the data before passing it to mustache.

@metmajer
Copy link
Author

Agreed, the spec doesn't say so. But as I stated in the title, I am using the CLI, and not a controller implementation for the moment.

If you're accepting JSON as a data input format, as a user I'd expect you to support this in the output as well -- at least when I want to dump a nested data structure.

@cbroglie
Copy link
Owner

No, mustache implementations output the default string representation of the given data, which varies by language (e.g. the JS version will output [object Object]). It's your job to fit the template to the data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants