handlebars.js - Variables containing "." character in Handlebars? -
i using handlebars templates. if want insert variable in hbs file, add {{var_name}}
.
the issue is, have large object keys taking formats similar to:
{ "stats.name":"john", "stats.performance.day":123, "stats.performance.month":4567, "company":"my llc" }
if try add these handlebars file {{company}}, {{stats.name}}, {{stats.performance.day}}, {{stats.performance.month}}
{{company}}
displayed. other values come out blank, assume because "." special character in handlebars.
my question is, there way override this, or need iterate through object , change every "." "_" or before passing handlebars make work?
i recommend renaming properties. dot notation 1 of 2 ways access object properties in javascript. typical code like: obj.prop
. if object's keys not valid identifiers, must use bracket notation: obj['my-prop']
. since dot property accessor, unusual see 1 in property name. if come across code written obj['stats.performance.date']
, assume mistake , obj.stats.performance.date
intended.
with said, handlebars does support referencing properties not valid identifiers. called "segment-literal-notation", , simple wrapping square brackets around identifier:
{{[stats.performance.day]}}
the documentation states that:
javascript-style strings, " , ', may used vs. [ pairs.
so following alternatives valid:
{{"stats.performance.day"}} {{'stats.performance.day'}}
Comments
Post a Comment