Les Nightingill
Les Nightingill

Reputation: 6156

Can I use ractive templates standalone?

In order not to proliferate templating schemes through my app, I would like to use Ractive preparsed templates.

A preparsed template object is produced using Ractive.parse("template text").

How can this preparsed template be populated with variables at render time? I'm thinking of this kind of thing:

templ = Ractive.parse("<p>{{name}}</p>")
html = Ractive.renderTemplate(templ, {name : "Herbert"})  //=> "<p>Herbert</p>"

similar to how a template is compiled and rendered in underscorejs.

Thanks in advance

Upvotes: 1

Views: 139

Answers (1)

Rich Harris
Rich Harris

Reputation: 29605

You can use a parsed template in the same way you'd use a string template - it just skips the parsing step:

var parsedTemplate = Ractive.parse("<p>{{name}}</p>");

var ractive = new Ractive({
  template: parsedTemplate,
  data: { name: "Herbert" }
});

var html = ractive.toHTML();

Upvotes: 2

Related Questions