twitter / hogan.js

A compiler for the Mustache templating language
http://twitter.github.io/hogan.js
Apache License 2.0
5.14k stars 431 forks source link

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

Open arekinath opened 8 years ago

arekinath commented 8 years ago

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 commented 8 years ago

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.

CLAassistant commented 5 years ago

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.