简体   繁体   中英

Escape variable name with Mustache.js

I have a csv file that I'm parsing to JSON and then sending to Mustache.js. The problem is that some of the variable names are not well formed, so I end up with objects that may look like this:

{
    'Num.': '1234',
    'Pass-through': 'yes'
}

So how would I use those variables in a Mustache template? I've tried a couple of different approaches.

<td>{{ Num. }}</td>
<td>{{ 'Num.' }}</td>
<td>{{ ['Num.'] }}</td>

But none of them work (I'd be more surprised if they did), and can't find anything in the documentation on how to escape a variable name.

Manual

overview: | Interpolation tags are used to integrate dynamic content into the template.

The tag's content MUST be a non-whitespace character sequence NOT containing the current closing delimiter.

This tag's content names the data to replace the tag. A single period ( . ) indicates that the item currently sitting atop the context stack should be used; otherwise, name resolution is as follows:

  1. Split the name on periods; the first part is the name to resolve, any remaining parts should be retained.
  2. Walk the context stack from top to bottom, finding the first context that is a) a hash containing the name as a key OR b) an object responding to a method with the given name.
  3. If the context is a hash, the data is the value associated with the name.
  4. If the context is an object, the data is the value returned by the method with the given name.
  5. If any name parts were retained in step 1, each should be resolved against a context stack containing only the result from the former resolution. If any part fails resolution, the result should be considered falsey, and should interpolate as the empty string.

Data should be coerced into a string (and escaped, if appropriate) before interpolation.

In short : you can't have element with name 'Num.' .

All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}. If that doesn't work you could use a function which simply returns it's input unescaped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM