Skip to content

Extensibility Templates

Mark Vincent edited this page May 24, 2019 · 12 revisions

ASB Logo

This document is designed to explain the concepts of using templates with an Advanced Sidebox addon module. For further details, view the entire list of extensibility documents: Extensibility Docs

Using Templates

Templates are essential to making your module more versatile for admins. The templates defined by your plugin are created in the Advanced Sidebox template group tracked for each theme and will allow each user to customize your side box's output for their needs.

To use templates, simply define them in your module's _info() function and then use them in the _get_content() function to defined content:

function asb_example_info()
{
	return array(
		'title' => 'Template Example',
		[...]
		'installData' => array(
			'templates' => array(
				array(
					'title' => 'asb_example_template_name',
					'template' => <<<EOF
					<div class="trow1">Some content.</div>
					<div class="trow2">{\$aVariable}</div>
	EOF
				),
			),
		),
	);
}

function asb_example_get_content($settings)
{
	global $templates;
	$aVariable = 'some value';
	eval("\$content = \"{$templates->get('asb_example_template_name')}\";");
	return $content;
}

The above example is a very simple picture of what templates can do. For a slightly clearer picture, look here: Simple Template Example

Escaping Template Content

When using templates in ASB modules, it is necessary to escape a couple characters in the HTML. For instance, the following HTML markup would need to be escaped in order for the module to correctly install and use the template.

	<div class="trow1">{$mybb->user['username']}</div>

Variables

The first is is the $. Since the template HTML is evaluated by PHP when it is returned to the calling function, it is necessary to escape all $ characters that preceded a string of word characters with a \ (backslash) because, if not, PHP will insert the value of that variable, if it exists, into the HTML string instead of waiting until the appropriate time to evaluate the variable.

	<div>{\$mybb->user['username']}</div>

Even unintentional usage of the $ character will result in errored HTML output. For example, <span>Hi $money man!</span> becomes, <span>Hi man!</span> when evaluated by PHP unless of course the variable $money is defined. In that case, if it is a printable value, that value will be inserted in the place of the $money variable.

Single Quotes

And also, since the template will be stored in the database, all ' (single quote) characters must be escaped with a \ (backslash) as well.

	<div>{\$mybb->user[\'username\']}</div>

Updating Module Templates

It is important to note that when developing addon modules, you must increment the version of the module in order for template changes to take effect. When in a testing environment, set the debugMode property of your module to true, to update templates for every change.

Next Topic

Using AJAX Refresh