javascript - Handlebars templates, access context variables with a string key -
let's have handlebars template this:
<script id="entry-template" type="text/x-handlebars-template"> <div class="entry"> <h1>{{i18n.title}}</h1> </div> </script>
what this:
var source = $("#entry-template").html(); var template = handlebars.compile(source); var context = {"title.t":"my new post"} console.log(template(context));
when try parse in context above string key, hanslebars acts key not available. understand normaly structure should this:
var context = { i18n:{ title:'test' } }
but because i'll 118n strings external source, easier use them keys. ah , yes, know projects i18n.js, actual question is: can use string-key in context object dots in , if yes, how can access them handlebars template?
no cannot use keys dots in them. because part of syntax. if use dots how access children of element? instead of dots, use underscore or dash. it's standard people respect because doesn't introduce bugs , other weird behaviour.
Comments
Post a Comment