Spanish Total.js
English
Documentation For AI agents
Contents

Importing content

Load HTML, scripts and stylesheets on demand with IMPORT() and the ui-import element.

.md GitHub
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.

ArgumentTypeDescription
urlString or ArrayThe address of the file, or a list of addresses.
targetString, jQuery or ElementWhere the content goes. The default is body.
callbackFunctionRuns when the import is finished and receives a status.
insertBooleanfalse replaces the content of the target. Any other value appends to it.
preparatorFunctionReceives 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:

SettingDescription
urlThe address of the file.
pathThe value of the ~PATH~ placeholder.
idThe value of the ~ID~ placeholder.
replacetrue replaces the element itself with the content.
targetA selector of the element that receives the content.
makeThe path of a function that receives (response, element, path) and returns the text to insert.
initThe path of a function that runs after the import and receives the element.
cacheHow long to keep the response, for example 5 minutes.
reevaluatetrue 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.

Made in Europe