|
Loading a snippet via a scriptInstead of dragging it into the content directly, it is possible, and often very useful, to load a snippet dynamically. Create a script (see Writing your own scripts) and in the code use the following function:
Remote snippets are retrieved in the same way, except that the file extension should be This function will insert the snippet in the content at any position where the script's selector is encountered. For more examples, see loadhtml(). Make sure that the file name is exactly the same as the file in the Snippets folder. If the file name isn’t correct, the snippet will not appear in the template. Loading part of a snippetWhen a snippet contains a part that can be identified by a selector, that selector can be used to load that part of the snippet into a template. In script, use the following code:
See loadhtml() for more information about this function. Loading a snippet, depending on the value of a data fieldTo load a snippet depending on the value of a data field, you have to add a condition to the script. ExampleThe following script evaluates if the value of the LANGUAGE field in the record is ‘En’. If so, the snippet is added to the content.
Another example is given in a how-to; see Load a snippet based on a data field value. Loading part of a snippet, based on the value of a data fieldWhen a snippet contains a part that can be identified by a selector, that selector can be used to load that part of the snippet into a template. It is possible to do this, based on the value of the data field. This is easiest when the selector matches the value of a data field. ExampleThe following script reads the value of the LANGUAGE field in the record and uses that value as the selector in the function loadhtml(). If the snippet contains an HTML element with this ID (for example, <p ID=”En”>), that HTML element will be added to the content:
Another example is given in the following how-to: Using a selector to load part of a snippet. See also: Designer Script API. An easy way to group content in a snippet is putting each part in a container and giving that container an ID, for example:
This script loads the paragraph of the example (<p>), but not the container itself (<div>). Load a snippet and insert variable data into itThe following script loads part of a snippet based on the value of a field, and then finds/replaces text by the value of a field before inserting the content into the document.
|
|