Importing content
Load HTML, scripts and stylesheets on demand with IMPORT() and the ui-import element.
On this page
An application does not have to load everything at once. IMPORT() downloads HTML, scripts and stylesheets when you ask for them, and the <ui-import> element does the same from the markup.
Importing HTML
IMPORT(url, [target], [callback], [insert], [preparator]) downloads a file and adds it to the page.
| Argument | Type | Description |
|---|---|---|
url | String or Array | The address of the file, or a list of addresses. |
target | String, jQuery or Element | Where the content goes. The default is body. |
callback | Function | Runs when the import is finished and receives a status. |
insert | Boolean | false replaces the content of the target. Any other value appends to it. |
preparator | Function | Receives the downloaded text and returns the text to insert. |
<div id="box"><i>Loading</i></div>
<script>
IMPORT('/parts/hello.html', '#box', function(status) {
console.log(status);
});
</script>If /parts/hello.html contains <b>Hello from the server</b>, the status is 1 and the target contains <i>Loading</i><b>Hello from the server</b>. The status is 0 when nothing was imported: the response was empty, or the file was skipped by one of the conditions described below.
You can leave target out and pass the callback as the second argument.
Replacing and changing the content
Pass false to replace what is in the target, and a function to change the text ahead of insertion:
IMPORT('/parts/hello.html', '#box', function() {
console.log($('#box').html());
}, false, function(response) {
return response.replace('Hello', 'Welcome');
});The target now holds only <b>Welcome from the server</b>.
What happens with the content
<style>blocks move to the<head>.<script>blocks run.- Components, bindings and plugins in the content are compiled.
- Text marked with
@(...)is translated, see Localization.
Importing scripts and stylesheets
The extension decides how a file is loaded. .js adds a <script> element and .css adds a <link> element to the <head>, and the target is not used. If the address has no suitable extension, add one at the end, separated by a space:
IMPORT('/assets/chart.js', function() {
// The script is ready
});
IMPORT('/assets/theme.css');
IMPORT('/download?file=chart .js');The callback of a .js or .css file runs only when the file loads. It does not run when the download fails.
Importing a file once
The prefix once (or !) imports a file a single time. Further imports of the same address do nothing and call the callback with the status 0:
IMPORT('once /parts/hello.html', '#box', function(status) {
console.log(status); // 1
IMPORT('once /parts/hello.html', '#box', function(status) {
console.log(status); // 0
});
});Importing a file conditionally
A path in angle brackets skips the import when the value of the path is truthy. Add () to call a function and use its result. This is the usual way to load a library only when it is missing:
IMPORT('<window.Chart> /assets/chart.js', function(status) {
// status is 0 when window.Chart already exists
});Importing several files
An array imports the files one after another. The callback runs after the last one and receives no status:
IMPORT(['/parts/hello.html', '/parts/widget.html'], '#box', function() {
// Both files are in #box
});Caching a response
IMPORTCACHE(url, expire, [target], [callback], [insert], [preparator]) works like IMPORT() and keeps the downloaded HTML for the given time:
IMPORTCACHE('/parts/hello.html', '5 minutes', '#box');The ui-import element
<ui-import> imports a file when the element appears on the page. The content replaces whatever the element contains:
<ui-import config="url:/parts/widget.html;path:orders;id:w1"></ui-import>The most used settings of the config attribute:
| Setting | Description |
|---|---|
url | The address of the file. |
path | The value of the ~PATH~ placeholder. |
id | The value of the ~ID~ placeholder. |
replace | true replaces the element itself with the content. |
target | A selector of the element that receives the content. |
make | The path of a function that receives (response, element, path) and returns the text to insert. |
init | The path of a function that runs after the import and receives the element. |
cache | How long to keep the response, for example 5 minutes. |
reevaluate | true keeps scripts and styles when the same address is imported again. |
<ui-import config="url:/parts/hello.html;make:prepare;init:ready"></ui-import>
<script>
function prepare(response, element, path) {
return response.replace('server', 'browser');
}
function ready(element) {
console.log(element.text()); // Hello from the browser
}
</script>The first import of an address runs its scripts and styles. Next imports of the same address remove them from the content, so one file placed on a page three times does not run its script three times. Use reevaluate:true when every copy has to run its script. The ui-import reference describes every setting.
Reusing a file with placeholders
Placeholders let one file serve several places. <ui-import> replaces ~PATH~ with path and ~ID~ with id. PLUGIN(function( in the file gets the path as the plugin name:
<!-- /parts/plugin.html -->
<ui-plugin path="~PATH~">
<ui-bind path="?.title" config="text"></ui-bind>
</ui-plugin>
<script>
PLUGIN(function(exports) {
exports.set('title', 'Plugin ' + exports.name);
});
</script><ui-import config="url:/parts/plugin.html;path:orders"></ui-import>
<ui-import config="url:/parts/plugin.html;path:users;reevaluate:true"></ui-import>The page now runs the plugins orders and users, showing Plugin orders and Plugin users. See Plugins.
Importing from a component
A component imports into its own element with self.import(url, [callback], [insert], [preparator]). See Creating components.