can.view
Utilities for loading, processing, rendering, and live-updating of templates.
can.view( idOrUrl, data[, helpers] )
Loads a template, renders it with data and helper functions and returns the HTML of the template within a documentFragment.
var frag = can.view(
"/contact.ejs",
{first: "Justin", last: "Meyer"},
{
fullName: function(first, last){
return first +" "+ last
}
});
document.getElementById('contacts').appendChild(frag)
Parameters
-
idOrUrl
{String | Object}
The URL of a template or the id of a template embedded in a script tag or an object containing a
url
property for the URL to load and anengine
property for the view engine (mustache
orejs
) if it can't be infered from the file extensions or script tag type. -
data
{Object}
Data to render the template with.
-
helpers
{Object<String,function()>}
An object of named local helper functions.
Returns
{documentFragment}
The rendered result of the template converted to html elements within a documentFragment.
can.view( idOrUrl )
Registers or loads a template and returns a renderer function that can be used to
render the template with data
and helpers
.
var renderer = can.view("/contact.ejs");
var frag = renderer(
{first: "Justin", last: "Meyer"},
{
fullName: function(first, last){
return first +" "+ last
}
})
document.getElementById('contacts').appendChild(frag)
Parameters
-
idOrUrl
{String}
The URL of a template or the id of a template embedded in a script tag.
Returns
{renderer(data, helpers)}
A renderer function that can render the template into a documentFragment.
Use
can.view( idOrUrl, data, helpers )
loads template content from an element, a url or a string, renders it with data, and converts it to a documentFragment so it can be easily and efficiently inserted into the DOM.This code:
Loads the template a 'mytemplate.ejs'. It might look like:
Renders it with {message: 'hello world'}, resulting in a documentFragment that contains:
Inserts the result into the foo element. Foo might look like:
Loading Templates
can.view
can load templates from a url or from a script.Loading templates from a script tag
To load from a script tag, create a script tag with:
For example:
Render with this template like:
Notice we passed the id of the element we want to render.
Loading templates from a url
To load from a url, simply pass the location of the template to
can.view
. The location of the template needs an extension that matches the type of template:Note: If you are using RequireJS, the URL will be relative to its
baseUrl
.Creating templates from strings
Create a template for a given id programmatically using
can.view.<engine>(id, template)
:It is also possible to get a nameless renderer function when creating a template from a string:
Supported Template Engines
CanJS supports the following live template languages:
can.ejs EmbeddedJS
can.mustache Mustache
Rendering to strings and sub-templates
To render to a string, use
can.view.render(idOrUrl, data)
like:To convert that rendered string into a live documentFragment, use frag.
To render a can.ejs sub-template within another template, use render like:
Asynchronous Loading
By default, retrieving templates is done synchronously. This is fine because [StealJS] packages view templates with your JS download.
However, some people might not be using StealJS or want to delay loading templates until necessary. If you have the need, you can provide a callback parameter like:
The callback function will be called with the result of the rendered template.
Deferreds
If you pass deferreds to can.view it will wait until all deferreds resolve before rendering the view. This makes it a one-liner to make a request and use the result to render a template.
The following makes a request for todos in parallel with the todos.ejs template. Once todos and template have been loaded, it with render the view with the todos.