How do I create a function that is globally available?
Right now, the application is structured as follows:
- A distinct template for each kind of request (e.g. distinct templates for a book, an author or a publisher), e.g.:
- Each of these templates is decorated with common elements from a macro in page.ftl, e.g.:
[#ftl]
[#macro decorate]
<html>
<head>
<!-- some stuff here -->
</head>
<body>
<header><!-- more stuff here --></header>
<div id="main-content">[#nested /]</div>
<footer><!-- more stuff here --></footer>
</body>
</html>
[/#macro]
So, book.ftl would look something like:
[#ftl]
[#include page.ftl p]
[@p.decorate]
<h1>Book: The Bible</h1>
<dl>
<dt>Author:</dt>
<dd>God</dd>
</dl>
[#-- HERE'S THE IMPORTANT BIT --]
[@myFunctionHere('The Bible') /]
[#-- I ALSO NEED TO BE ABLE TO CALL myFunction IN INCLUDED PAGES TOO --]
[#import "_partial.ftl" /]
[/@p.decorate]
I would like to create a global function that would be defined and included once and available everywhere (in book.ftl and others, plus any templates it happens to import/include).
How would I go about this, preferably without it’s own namespace?
Just to ensure that it’s clear:
#include-d templates share name-space with the including template, so if you want that, just don’t use#import. The point of#importis exactly that the templates don’t share name-space. If you want those separate name-spaces, yet you want to share some macros/functions, then the templates could still#importeach-other.If you really want some functions/macros to become global, then after you have defined them, you can copy them into the global name-space like this:
After this you can issue
[@myMacro /]everywhere.