Welcome to Elgg's documentation
This is the place to find documentation on all aspects of Elgg. If you would like to contribute your own documentation please do; we want this to be a real community effort!
Templates and output fields (API)
From Elgg Documentation
Contents |
[edit] Creating new template keywords
Elgg uses keywords within two sets of curly brackets in order to insert dynamic functionality into templates. For example, {{url}} displays the full URL of your site; {{sitename}} is your site's name, while {{username}} is the username of the currently logged in user and {{name}} is their full name.
Plugins can create new keywords in order to inject extra functionality into templates. Here's how:
[edit] Hooking into template variables
In the plugin_init() function, set the following variable:
(string) $CFG->templates->variables_substitute[your_keyword][]
This should be the name of a function that will return some HTML. Remember to set $CFG to global at the beginning of plugin_init()!
For example:
function plugin_init() {
global $CFG;
(string) $CFG->templates->variables_substitute[your_keyword][] = 'function';
}
[edit] Writing your output function
Let's say you've set your function name to be plugin_your_keyword.
Template keywords can contain parameters, of the form {{keyword:param1:param2:...:paramn}}. These will be passed to your function, and therefore it needs to take one variable parameter, $vars. (Even if your keyword doesn't use parameters, your function will need to include this parameter.)
$vars is an array, where $vars[0] is the keyword itself and all elements $vars[1] through $vars[n] are the parameters.
The function needs to return HTML. A simple example is a function to return the current time when the keyword {{time}} is specified:
The plugin_init() portion:
function plugin_init() {
global $CFG;
$CFG->templates->variables_substitute['time'][] = (string) 'plugin_time';
}
And the function itself:
function plugin_time($vars) {
return (string) date("F j, Y, g:i a");
}
If you wanted, you could amend this to provide a GMT time when {{time:gmt}} is specified:
function plugin_time($vars) {
if (isset($vars[1]) && $vars[1] == "gmt") {
return (string) gmdate("F j, Y, g:i a");
} else {
return (string) date("F j, Y, g:i a");
}
}
Back to Development frontpage
Categories: API | Development | Theming

