Skip to Content | Skip to Navigation


dojo.cache

Project owner:James Burke
Available:since V1.4

A getter and setter method for storing the string content associated with the module and url arguments. It is a generic version of the functionality provided by dijit’s templatePath method.

Introduction

There are many times where you will want to inject some raw HTML into the DOM. However, it can be cumbersome to write the HTML as a string literal in JavaScript.

dojo.cache allows you to specify a path to a file that has the HTML, and dojo.cache will load it via a synchronous XMLHttpRequest (XHR) call. Because of this, the HTML file should live on the same domain as the web page that uses the JavaScript module that has the dojo.cache call.

The Dojo build system will inline the HTML as a string where the dojo.cache call happens, so it allows for better performance just by doing a build. Doing a build also allows the module to be used in xdomain loading scenarios.

Usage

dojo.cache is a Dojo Core module, not part of Dojo Base so you need to dojo.require(“dojo.cache”) to load it:

dojo.require("dojo.cache");

dojo.cache takes the following arguments:

dojo.cache(module, url, configValue);

Here is the definition of arguments:

Examples

This is the usual, most common use of the dojo.cache call:

1
2
dojo.require("dojo.cache");
var text = dojo.cache("my.module", "template.html");

If my/module/template.html contained the text "<div>Hello World</div>", then the text variable will have that value.

An example using the sanitize: true option:

1
2
dojo.require("dojo.cache");
var text = dojo.cache("my.module", "template.html", {sanitize: true});

If my/module/template.html contains "<html><body><h1>Hello</h1></body></html>", the text variable will contain just "<h1>Hello</h1>".

Example using an object that has like the previous example, but uses an object whose toString() method represents a file path:

1
2
dojo.require("dojo.cache");
var text = dojo.cache(new dojo._Url("my/module/template.html"), {sanitize: true});