Questions tagged [underscore.js-templating]

This tag is specifically for the templating system questions, with underscore.js's _.template function.

Underscore.js can be used as a templating system with the _.template function. Quoting from the docs,

Compiles JavaScript templates into functions that can be evaluated for rendering. Useful for rendering complicated bits of HTML from JSON data sources. Template functions can both interpolate variables, using <%= … %>, as well as execute arbitrary JavaScript code, with <% … %>. If you wish to interpolate a value, and have it be HTML-escaped, use <%- … %> When you evaluate a template function, pass in a data object that has properties corresponding to the template's free variables. If you're writing a one-off, you can pass the data object as the second parameter to template in order to render immediately instead of returning a template function. The settings argument should be a hash containing any _.templateSettings that should be overridden.

So, this tag is specifically for the questions pertinent to the templating, NOT the functional programming aspect of underscore.js.

265 questions
22
votes
1 answer

loops in underscore js template

Ok guys so I have this array of key pair values which I'm using as my model: var acs = [{'label':'input box'},{'label':'text area'}]; the rest of the code goes as follows var Action = Backbone.Model.extend({}); var action = new Action(acs); var…
climboid
  • 6,776
  • 14
  • 42
  • 68
15
votes
2 answers

Underscore templating: Can't get switch to work

I can't get a simple switch statement working in my underscore template. It's using the value of a variable called UserType which I've checked exists by displaying it with <%= UserType %>. Code coming up: <% switch(UserType) { %> <% case 13: %> …
a11y_guru
  • 220
  • 2
  • 8
14
votes
2 answers

Underscore template throwing variable not defined error

I've watched some videos on the topic of backbone js. This is an example straight from the video. It is from 2012, so I'm thinking backbone rules/library have changed, but I can't figure out why this does not work at the moment. In the video, the…
ra170
  • 3,483
  • 7
  • 35
  • 51
13
votes
1 answer

Underscore / Microtemplate Replace Line Breaks - Strange Behaviour

I'm using the Underscore template (which is based on John Resig's Microtemplate) and whenever I try to replace line breaks inside of it, I get strange behaviors. For example, if I have text like this: var message = 'Line1\r\n\r\nLine2'; I can…
Johnny Oshika
  • 45,610
  • 33
  • 151
  • 234
11
votes
2 answers

Underscore.js template rendering

I have this sample code to render simple unescapedHTML using underscore templating. var template = $(this.el).html(_.template(this.template, {'data': '<script>'})); $(this.parent).append(template); But when it try to render it, it caused an…
10
votes
1 answer

underscore interpolate settings

I'm trying to use handlebars style templating with underscore in backbone (with require.js). I have the following: _.templateSettings.interpolate = /\{\{(.+?)\}\}/g; In my templates I have set up a test:
<%= title %> | {{ title }}
I…
jamie holliday
  • 1,567
  • 8
  • 25
  • 37
9
votes
2 answers

Ways to interpolate template variables inside JavaScript objects

I'd like to do the following var obj = { animal: "${animal}" }; var res = magic(obj, {animal: "cat"}); // res => {animal: "cat"} magic is some function that does the dirty work. Obviously obj could be more complex with multiple keys, nested…
8
votes
1 answer

How to use an HTML minifier with underscore templates

I have some templates for my frontend code, like:
{{ title }}
{% if (content) { %} {{ content…
8
votes
1 answer

Issue with with 'use strict' and underscore.js

I've written an app using Yeoman and backbone.js. At the top of every js file I have specified 'use strict'; and when I run my grunt tasks jshint does not encounter any errors. I am able to build my app with grunt without issue however when I try…
7
votes
2 answers

How to get webpack2 and underscore-template loader + babel to work without getting "Module build failed: SyntaxError: 'with' in strict mode (5:0)"

I have a underscore template loader in my webpack2 config that is transpiled with babel. It fails at compile time because with is used in the code compiled code. Here is the relevant part in my loaders in webpack.config.js: I have this section under…
Sylwester
  • 44,544
  • 4
  • 42
  • 70
7
votes
1 answer

How to achieve deprecated CompositeView functionality in Marionette 3+?

As stated in the latest Marionette docs: CompositeView is deprecated. You should use the replaceElement option on Region.show and render a CollectionView into a region inside a View to achieve this functionality. I still can't understand how…
7
votes
3 answers

Underscore template Uncaught ReferenceError variable is not defined

I am trying to render a basic backbone view with an underscore template, but I keep getting the following error when attempting to render the template. Uncaught ReferenceError: amount is not defined here's the jsfiddle:…
7
votes
3 answers

combine html template files into one JS file

I have HTML template files (underscore template syntax) These files are saved in HTML format so they would be easy to edit (IDE syntax highlight) I don't want to fetch them with ajax, but rather combine them all and include them as ajs file. Using…
vsync
  • 87,559
  • 45
  • 247
  • 317
7
votes
0 answers

What is the difference between EJS and underscore.js' .template()

Both seem to be doing very similar things, in a similar way. Both let you embed javascript into the templates, and have the same 3 types of tags -unescaped replace, escaped replace, embed js-. Both have the ability to 'precomile' the template by…
DanC
  • 8,097
  • 9
  • 40
  • 64
7
votes
2 answers

underscore.js precompiled templates using

I want to use precompiled underscore.js templates. I use _.template().source and save result in file. But I don't understand, how to use this templates. Precompiled templates is strings, and I can't cast it to a function. I try to use eval, but it…
1
2 3
17 18