Navigation

Views

Search
 

Toolbox

Smarty Plugins

From Documentation

Smarty Plugins allow to include code or snippets throughout your site. Any plugins that you create are global to your site and will allow you to use the same code in all pages. Most of the documentation below is found at the smarty website.

How Plugins Work

Plugins are always loaded on demand. Only the specific modifiers, functions, resources, etc invoked in the templates scripts will be loaded. Moreover, each plugin is loaded only once, even if you have several different instances of Smarty running within the same request.

Naming Conventions

Plugin files and functions must follow a very specific naming convention in order to be located by Smarty.

The plugin files must be named as follows:

type.name.php

Where type is one of these plugin types:

   * function
   * modifier
   * block
   * compiler
   * prefilter
   * postfilter
   * outputfilter
   * resource
   * insert

And name should be a valid identifier (letters, numbers, and underscores only).

Some examples: function.html_select_date.php, resource.db.php, modifier.spacify.php.

The plugin functions inside the plugin files must be named as follows:

smarty_type_name()

The meanings of type and name are the same as before.

Smarty will output appropriate error messages if the plugin file it needs is not found, or if the file or the plugin function are named improperly.

An Example Plugin

To better explain how plugins work we will give you an example to go by. This example is used to pull the latest member and display it on your site.

First think of a name. In this case we will name it newest_member. So from the documentation above we create our function:

smarty_function_newest_member($params, &$smarty)
{
 global $db;
    $sSQL="SELECT id,username FROM ".PREFIX."users ORDER BY joindate DESC";
    $result=$db->query($sSQL);
    $rs=$result->fetch();
    $newest=$rs['username'];
    $newestid=$rs['id'];
    $output="Please welcome our newest member";
    $output.="<a href='viewmember.php?member=".$newestid."'>$newest."</a>";
    return $output;
}

Then save the file as function.newest_member.php and upload it to the includes/classes/smarty/plugins directory.

Finally in your template file where you want this information displayed add:

{newest_member}

External Documentation