PlanetPress How-To: Simple Dynamic Contents Script
This tip is especially useful when doing dynamic Web Contexts, however it’s great if you have multilingual contents.
Step 1: Article Element
When you drag an existing snippet on your page and insert it as a Shared Content, it will simply create an article element pointing to the script source file. Whenever output is generated (in any context), PlanetPress Connect replaces the article tag with the contents of the snippet. But, you can also just add this element wherever you want directly in the source code:
Step 2: Identifying the Element
In order for a script to be able to interact with the article element, it must be identified. Generally, a simple ID should suffice:
<article id=”body_contents” source=”snippets/invitation-en.html”></article>
Step 3: Creating the script
The script that makes the magic work is a single line, so it’s really straightforward. Create a new Script (not a text script or otherwise) where the selector targets the article element, in this case simple #body_contents
The script below calls a dynamic snippet that’s based on a language tag. It assumes there are snippets named “contents-fr.html”, “contents-en.html”, etc and a record field called “language” with the value being “fr”, “en”, etc.
results.loadhtml(“snippets/contents-” + record.fields.language + “.html”);
Easy enough, right? I love simple stuff. But what about external snippets somewhere on the drive? That’s just as simple, you just need the full path to the file on the drive!
results.loadhtml(“file:///d:/projectfiles/campain01/snippets/contents-” + record.fields.language + “.html”);
Aaaand bingo, you’re done! Personally, I love to do this for web templates where the snippet is called using a variable coming from PlanetPress Workflow – I’ll link to a sample file as soon as I can on the PlanetPress Connect Demos platform!
Learn more about PlanetPress Connect on our e-learning portal.