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

{{{foo}}} when foo is a plain JS object should return JSON #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arekinath
Copy link

While it's not made clear in the spec, it seems most other implementations of mustache (and certainly the original Ruby one) spit out a nested object/hash/dict as some parseable form for their host language.

Example:

require 'mustache'
Mustache.render("hi {{{foo}}}", {:foo => {:bar => 123, :test => "abc"}})
# => "hi {:bar=>123, :test=>\"abc\"}"

Currently in this case on Hogan.js, we just spit out [object Object], which is not very useful:

var Hogan = require('hogan.js');
var tpl = Hogan.compile('hi {{{foo}}}');
tpl.render({foo: {bar: 123, test: 'abc'}})
// => "hi [object Object]"

This patch amends coerceToString to return JSON when we're asked to output a nested plain JS object (ie, one without its own toString implementation), so the output for the case above changes to "hi {\"bar\": 123, \"test\": \"abc\"}"

@Badestrand
Copy link

Badestrand commented Jun 13, 2016

Why is this not merged yet? The errors for the checks are all for the npm install and not for that two lines of trivial code.
edit: I stand corrected. Nonetheless it would be great if the fixed code could be merged some time soon.

@@ -319,6 +319,8 @@ var Hogan = {};
hChars = /[&<>\"\']/;

function coerceToString(val) {
if (typeof (val) === 'object' && val.toString === Object.prototype.toString)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need check for null.

@CLAassistant
Copy link

CLAassistant commented Jul 18, 2019

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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

Successfully merging this pull request may close these issues.

4 participants