hogan.js icon indicating copy to clipboard operation
hogan.js copied to clipboard

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

Open arekinath opened this issue 10 years ago • 2 comments

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\"}"

arekinath avatar Nov 04 '15 21:11 arekinath

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.

Badestrand avatar Jun 13 '16 17:06 Badestrand

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.

CLAassistant avatar Jul 18 '19 15:07 CLAassistant