render

  • function
can.view.render  

Render a template.

can.view.render(template[, callback])

Parameters

  1. view {String | Object}

    The path of the view template or a view object.

  2. callback {function()}Optional

    A function executed after the template has been processed.

Returns

{function() | can.Deferred}

A renderer function to be called with data and helpers or a Deferred that resolves to a renderer function.

can.view.render(template, data[, [helpers,] callback])

Parameters

  1. view {String | Object}

    The path of the view template or a view object.

  2. data {Object}Optional

    The data to populate the template with.

  3. helpers {Object<String,function()>}Optional

    Helper methods referenced in the template.

  4. callback {function()}Optional

    A function executed after the template has been processed.

  5. nodelist {NodeList}

    parent nodelist to register partial template contents with

Returns

{String | can.Deferred}

The template with interpolated data in string form or a Deferred that resolves to the template with interpolated data.

can.view.render(view, [data], [helpers], callback) returns the rendered markup produced by the corresponding template engine as String. If you pass a deferred object in as data, render returns a deferred resolving to the rendered markup.

can.view.render is commonly used for sub-templates.

Example

welcome.ejs looks like:

<h1>Hello <%= hello %></h1>

Render it to a string like:

can.view.render("welcome.ejs",{hello: "world"})
  //-> <h1>Hello world</h1>

Use as a Subtemplate

If you have a template like:

<ul>
  <% list(items, function(item){ %>
    <%== can.view.render("item.ejs",item) %>
  <% }) %>
</ul>

Using renderer functions

If you only pass the view path, `can.view will return a renderer function that can be called with the data to render:

var renderer = can.view.render("welcome.ejs");
// Do some more things
renderer({hello: "world"}) // -> Document Fragment