Importing content
How to load scripts, stylesheets and HTML parts into a running page with IMPORT() and the ui-import element.
On this page
An application does not have to load everything at start. IMPORT() loads a script, a stylesheet or a part of a page when it is needed, and the <ui-import> element does the same from the markup.
IMPORT()
IMPORT(url, [target], [callback], [prepare]) decides what to do from the extension of the URL:
| Extension | Result |
|---|---|
.js | Adds a <script> to the <head>. |
.css | Adds a <link> to the <head>. |
| Anything else | Downloads the content as HTML and inserts it into target. |
| Argument | Description |
|---|---|
url | The address, with optional flags. |
target | A selector or an element for HTML content. The default is body. |
callback | A function called when the content is loaded. |
prepare | A function that receives the downloaded HTML and returns the HTML to insert. |
Scripts and stylesheets
IMPORT('/js/charts.js', function() {
console.log('script', window.CHARTS);
});
IMPORT('/css/charts.css', function() {
console.log('style loaded');
});The callback runs after the file has loaded. If a script or a stylesheet cannot be loaded, the library writes a warning to the console and the callback still runs.
When the URL has no extension, name the type after a space:
IMPORT('/assets/charts?v=2 .js', function() {
console.log('script loaded');
});Loading a file once
Each call to IMPORT() loads the file again. There are two ways to avoid that.
The @singleton flag loads a script or a stylesheet only once, however many times it is requested:
IMPORT('/js/charts.js @singleton', function() {
console.log('ready to use');
});A global variable in angle brackets skips the download when that variable already exists:
IMPORT('<CHARTS> /js/charts.js', function() {
console.log('CHARTS is available');
});For HTML, @singleton downloads the content once, and each call inserts the kept copy into its target.
HTML parts
Downloaded HTML is appended to the target. The @prepend flag inserts it at the beginning. <style> blocks move to the <head>, and <script src> tags are added to the <head> once.
// /parts/note.html contains:
// <style>.note { color: green; }</style><p class="note">Imported</p>
IMPORT('/parts/note.html', '#content', function() {
console.log($('#content').html());
});
IMPORT('/parts/note.html @prepend', '#side', function() {
console.log($('#side').html());
});<p>Existing</p><p class="note">Imported</p>
<p class="note">Imported</p><p>Existing</p>The prepare function changes the content before it is inserted:
// /parts/hello.html contains: <p>Hello NAME</p>
IMPORT('/parts/hello.html', '#third', function() {
console.log($('#third').html()); // <p>Hello Ann</p>
}, function(html) {
return html.replace('NAME', 'Ann');
});Placeholders in imported HTML
Imported HTML can use these placeholders:
| Placeholder | Replaced with |
|---|---|
@(text) | The translation of the text. See Localization. |
--name=value-- | Nothing. It defines a variable for the file. |
--name-- | The value of the variable defined in the file. |
~CDN~ | The value of DEF.cdn. |
DEF.cdn = 'https://cdn.example.com';
LOCALIZE(HASH('Welcome').toString(36) + ' : Vitajte');
// /parts/welcome.html contains:
// --color=red--<p style="color:--color--"><img src="~CDN~/logo.png" />@(Welcome)</p>
IMPORT('/parts/welcome.html', '#target', function() {
console.log($('#target').html());
});<p style="color:red"><img src="https://cdn.example.com/logo.png">Vitajte</p>The ui-import element
<ui-import> imports content into itself as soon as it appears in the page. The URL goes to the path attribute or to the url configuration key.
| Configuration | Description |
|---|---|
url | The address, when the path attribute is not used. |
target | A selector of the element that receives the content. The default is the element itself. |
path | The value for ~PATH~ and CLASS in the content. |
id | The value for ~ID~ in the content. |
init | The name of a function to call when the content is loaded. |
class | CSS classes to toggle on the target when the content is loaded. |
The library writes a warning with the URL to the console the first time it downloads the content of a <ui-import>.
Importing a plugin
With the path key, one file can be used for any plugin. <ui-plugin> receives the path and PLUGIN(function...) is registered under it:
<!-- /parts/invoice.html -->
<ui-plugin><b id="~ID~">~PATH~</b></ui-plugin>
<script>
PLUGIN(function(exports) {
exports.make = function() {
console.log('plugin', exports.name);
};
});
</script><ui-import path="/parts/invoice.html" config="path:invoice;id:title;init:FUNC.imported;class:ready"></ui-import>
<script>
FUNC.imported = function() {
console.log('init');
};
</script>plugin invoice
initThe element #title contains the text invoice, <ui-import> has the class ready, and the instance is available as PLUGINS.invoice. Read more about plugins in Plugins.
Knowing when content is imported
The global event import is emitted after each import with the URL and the target. For a script or a stylesheet, the second argument is the new element:
ON('import', function(url, target) {
console.log(url);
});Requests made for HTML
HTML is downloaded with AJAX(), so the request carries the language set in DEF.language, and a URL ending with .html also carries DEF.version. See Localization.