Mustache base / underline pattern causing an error on the # pound / hash symbol?

I am using a template underline pattern with mustache formatting patterns.

I have already successfully used it elsewhere in the project, but this is the first time I'm using mustache cycle list templates to populate a template that throws an error that I'm a little puzzled with. Error in chrome:

"Uncaught SyntaxError: Unexpected token ILLEGAL" 

and indicates the underline of the template function in backtrace, which is pretty useless, but in firebug I get a more useful error, for example:

enter image description here

The assumption that the hash symbol "#" is a problem, which makes sense, since I know that the mustache works fine, as many other parts of the project use it well, this is also the first time I use hash sybol in my templates. This seems like a problem with either the loop function or the interpolation / pattern settings for underlining.

Here is the corresponding snippet of my template:

 <div class="thumblist thumblistleft" id="currentprojectslist"> <div class="thumb-list-header"> <h2>current projects</h2> </div> <div class="thumb-list-area"> <ol> {{#worklist}} <!----- LOOK HERE ---> {{#current}} <li><a>{{title}}</a></li> {{/current}} {{/worklist}} </ol> </div> </div> 

and here is a sample JSON (which checks everything fine)

 {blah blah blah lot in here before,"worklist":[{"thumb":"img/project-s.jpg","id":"340","title":"Test Project One","desc":"big load of content here","current":true}], and so on....} 

First I used this example for reference: http://mustache.github.com/#demo

NOW HERO WHERE I THINK THAT THE PROBLEM MAY BE:

underscore.js suggests using this before rendering the mustache pattern:

 _.templateSettings = { evaluate : /\{\[([\s\S]+?)\]\}/g, interpolate : /\{\{([\s\S]+?)\}\}/g }; 

and

 interpolate : /\{\{(.+?)\}\}/g 

Also just an interpolation operator, ive tried both. However, my knowledge of regex is really poor, and do I have the feeling that it cannot place a hash? Anyway ... I'm completely at a standstill. Can anyone help me here?

Is it even possible to go in cycles? looking at the source of the underscore, I'm not sure: http://documentcloud.github.com/underscore/docs/underscore.html#section-120

Many thanks

+6
source share
3 answers

I am posting to everyone someone who is facing this issue. After a lot of search queries to no avail, I looked at the source of underscore.js with a thin jagged comb, and basically you need to either use the syntax of the underscore patterns, or write ugly functional processors in your JSON, or include the mustache.js file in your source and cause

 Mustache.render(mytemplate,mymodel) 

and underline

 _.template(..) function 

Annoying, but still, I hope this helps someone

+7
source

Today I ran into this problem. It seems that the problem is that Underscore makes the templates: escape, interpolate, and then evaluate. Therefore, you need to explicitly ignore any matches for {{# in your interpolation regex:

 _.templateSettings = { evaluate: /\{\{#([\s\S]+?)\}\}/g, // {{# console.log("blah") }} interpolate: /\{\{[^#\{]([\s\S]+?)[^\}]\}\}/g, // {{ title }} escape: /\{\{\{([\s\S]+?)\}\}\}/g, // {{{ title }}} } 

Actually, this does not work like a mustache: in the Underscore template there are no correct blocks, so there is no need for a closing block {{/}} . You just need to match your claims, as with standard Underscore templates.

+23
source

I don’t use the # symbol, but I got a similar error as soon as I tried to use the triple mustache {{{name}}} for captive values ​​with the setting:

 interpolate : /\{\{\{(.+?)\}\}\}/g, escape : /\{\{(.+?)\}\}/g, 

If for this reason you came here, make the following settings

 interpolate : /\{\{\{(\s*\w+?\s*)\}\}\}/g, escape : /\{\{(\s*\w+?\s*)\}\}(?!\})/g, 

I tried the Max format, but it didn’t work for me, because I have a combination of double and triple mustache expressions, and while the triple expression works fine, it stripped the symbol from each end of the variable name in double mustache {{title}} led to an underscore in searches for a variable named itl

+1
source

Source: https://habr.com/ru/post/906848/


All Articles