What is a folder dictionary?
A folder dictionary is a javascript file called folderDictionary.js that defines a folderDictionary
variable. An example:
"use strict"; var folderDictionary = { myVar: 1 };
You can define javascript files called folderDictionary.js on any folder. When the maxFolderDictionaries configuration option is set to a value greater than zero ZPT-JS will preload this number of folder dictionaries. The first one is the file in the current folder, the second one is the file in the parent folder and so on.
Defining one or more folder dictionaries makes it easy to define default values for variables. You can overwrite the value of any of these values in the dictionary or in the dictionary extension: values in folder dictionaries have the lowest preference order.
When ZPT-JS search for a value in the folder directories it iterates through all folder dictionaries until it finds a value. ZPT-JS first tries with the folder dictionary in the same folder. If there is a match it returns this value; if not, ZPT-JS tries with the folder dictionary of the parent folder. It continues until it finds a matching value, the folder is the root of the web site or the number of checked folder dicionaries is equal to the maximum.
Examples
Let's define 2 folder dictionaries. The URL of the first one is at /folder/folderDictionary.js:
"use strict"; var folderDictionary = { var1: 1 };
The URL of the second one is at /folderDictionary.js:
"use strict"; var folderDictionary = { var1: 10, var2: 2 };
Our template is:
<div data-content="var1"> value of var1 </div> <div data-content="var2"> value of var2 </div>
And the ZPT-JS invokation:
"use strict"; var zpt = require( 'zpt' ); var dictionary = { ... }; zpt.run({ command: 'preload', root: document.body, dictionary: dictionary, maxFolderDictionaries: 5, callback: function(){ zpt.run(); [your code here] } });
Then, if the URL of our HTML file is /folder/myPage.html ZPT-JS will preload both folder directories. The value of var1 will be 1 because ZPT-JS iterates through all folder dictionaries until it finds a value, starting with the folder dictionary in the same folder. The value of var2 will be 2
If the URL of our HTML file is /myPage.html ZPT-JS will preload only /folderDictionary.js. The value of var1 will be 10 and the value of var2 will be 2
Folder dictionaries are useful to define default values for some variables such as footer macros. For example:
/reference/folderDictionary.js"use strict"; var folderDictionary = { 'footer-macro': 'referenceFooter@templates.html' };/folderDictionary.js
"use strict"; var folderDictionary = { 'footer-macro': 'generalFooter@templates.html' };
Our template is:
<div data-use-macro="footer-macro"> the footer </div>
So if the URL of our HTML file is /reference/myPage.html the footer will be 'referenceFooter@templates.html'; if the URL of our HTML file is /myPage.html the footer will be 'generalFooter@templates.html'.