Asked  7 Months ago    Answers:  5   Viewed   133 times

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 person shows it running in the JS Fiddle, but I can't get it to work. (I've included the necessary libraries in JS Fiddle, i.e. underscore, backbone and jQuery)

var V = Backbone.View.extend({
  el:'body',
  render: function () {
  	var data = { lat: -27, lon: 153 };
    this.$el.html(_.template('<%= lat %> <%= lon%>', data));
    return this;
  }
});

var v = new V();

v.render();
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://backbonejs.org/backbone-min.js"></script>

 Answers

40

You used to be able to parse and fill in an Underscore template in one go like this:

var html = _.template(template_string, data);

But as of Underscore 1.7.0, the second argument to _.template contains template options:

template _.template(templateString, [settings])

Compiles JavaScript templates into functions that can be evaluated for rendering. [...] The settings argument should be a hash containing any _.templateSettings that should be overridden.

You have to compile the template using _.template and then execute the returned function to get your filled in template:

var tmpl = _.template(template_string);
var html = tmpl(data);

// or as a one-liner, note where all the parentheses are
var html = _.template(template_string)(data);

In your case, it would look something like this:

var V = Backbone.View.extend({
  el:'body',
  render: function () {
    var data = { lat: -27, lon: 153 };
    var tmpl = _.template('<%= lat %> <%= lon %>');
    this.$el.html(tmpl(data));
    return this;
  }
});

var v = new V();

v.render();
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
Tuesday, June 1, 2021
 
SJain
answered 7 Months ago
100

Underscore _.template function takes a template string as argument (and optionally a settings object) and returns a new pre-compiled template function which takes an object as an argument.

This object is the data used within the template:

// creates a template function
var templateFunc = _.template("<span><%= name %></span>");

// render the template using the passed data
templateFunc({ name: "Émile" }); // <span>Émile</span>

By default, template places the values from your data in the local scope via the with statement. However, you can specify a single variable name with the variable setting.

_.template("Using 'with': <%= data.answer %>", {variable: 'data'})({answer: 'no'});

model.toJSON() returns a shallow copy or the attributes hash of the model.

To achieve the equivalent of the above example:

var model = new Backbone.Model({ name: "Émile" });
templateFunc(model.toJSON()); // <span>Émile</span>

For Underscore.js before v1.7, the template function signature was a little different:

_.template(templateString, [data], [settings]) 

If a data object was passed, it didn't returned a function, but returned the rendered template string directly.

_.template('This is <%= val %>.', { val: "deprecated" });
// This is deprecated.
Sunday, June 20, 2021
 
Novalirium
answered 6 Months ago
50

You need to take the pointers by reference:

template <> 
void Max(char*& a,char*& b,char*& c) 

That said, it would be better not to use an explicit specialization and instead just overload the function:

void Max(char* a, char* b, char* c)

It's almost always a bad idea to specialize function templates. For more, see Herb Sutter's "Why Not Specialize Function Templates?"

Friday, August 6, 2021
 
Blundell
answered 4 Months ago
36

Your todoData is (presumably) a string:

var todoData = model.get('title');

but a compiled Underscore template wants a key/value object as its argument:

When you evaluate a template function, pass in a data object that has properties corresponding to the template's free variables.

Looks like you have a title global variable or window property that is a <div> DOM object or you would get a ReferenceError complaining about an unknown title variable instead of a stringified DOM object.

In any case, the fix is pretty easy: give the template function what it wants:

$('#todo-list').append(compileTemplate({ title: todoData }));

or the common Backbone approach:

$('#todo-list').append(compileTemplate(model.toJSON()));

There are cases where the model will have optional attributes that the templates need to access. In such cases, you might have:

<%= pancakes %>

in the template but sometimes toJSON will give you:

{ title: 'x' }

and other times you'll get:

{ title: 'x', pancakes: 11 }

In such case you need to "un-optionalize" the optional attributes in your toJSON: toJSON should supply everything. If you have attributes that are optional then toJSON should ensure that it returns them with undefined or null values.

Saturday, August 21, 2021
 
Lawrence Taur
answered 4 Months ago
55

Your problem is that "global" variables in python are not actually global, but rather local to the module in which they were defined in. Thus, you cannot reference globals decalred in the main.py file in the classes.py file.

There are three possible solutions:

  1. (not recomended) Move the declarations of screen and background to the classes.py file, which would put them seemingly out of place.

  2. (also not recommended) Add a from main import screen, background to the classes.py file, which creates circular import problems, forcing the main.py module to do its from classes import * after defining screen and background

  3. Add a third display.py or screen.py file, where the screen and background variabled are defined and import it from main.py and classes.py

The mouse_pos variable is totally unnecessary and misused:

  1. You could just use pygame.mouse.get_pos() directly in the classes.py file, but
  2. Using pygame.mouse.get_pos() itself is extranious because pygame.MOUSEBUTTONDOWN events have a pos attribute pointing to the position of the mouse when the event was fired.
Tuesday, August 31, 2021
 
Santi
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share