
Presenting the meta_tag plugin for the Smarty templating engine!
Click here to download block.meta_tag.txt
Click here to download outputfilter.meta_tag.txt
This plugin allows you to change the page title and meta tags from any template used to generate page output.
Purpose
Typical web page display scenario:
Web page “Home” consists of Smarty templates:
- header.tpl – generic page header, HTML declaration, <HEAD> & <BODY> tags etc.
- navigation.tpl - site navigation.
- home.tpl – the main content for a given page, in this example “home”.
- footer.tpl – the page footer – closing </HEAD> & </BODY> tags, javascript tracking etc.
In this example, the page title and meta data would be in header.tpl, whether as Smarty variables:
<title>{$page_title}</title>
…or hard-coded values:
<title>Hello world!</title>
Using the meta_tag plugin, title and meta tag values can be updated from any other template used to generate the page.
The following example contained in footer.tpl would add “Product description” to the page title:
{meta_tag type=’title’ method=’add’}Product description{/meta_tag}
The following example contained in home.tpl would replace the page title with the text “Important business report”:
{meta_tag type=’title’ method=’replace’}Important business report{/meta_tag}
The following example contained in footer.tpl would add the text “jacket,blue,leather” to the meta keywords:
{meta_tag type=’keywords’ method=’add’}jacket,blue,leather{/meta_tag}
So… what’s the point?
Often when you’re displaying dynamic data across several templates it isn’t practical to customise the page title and meta information using Smarty assign(). Also,the logic used in templates processed after the page header might contain information which would be useful to reference in the header.
Caveat
It’s preferable to call load_filter just once per output, BEFORE the instruction to display() or fetch():
$smarty->load_filter(’output’, ‘meta_tag’);
$smarty->display(”template.tpl”);
In a scenario where multiple templates are used, they should all be contained within one container template, to ensure the output filter isn’t called repeatedly. In the above example, template.tpl might be:
{include file=’generic/header.tpl}
{include file=’generic/navigation.tpl}
{* load main page body – filename is contained within var $template*}
{include file=’`$template`}
{include file=’generic/footer.tpl}
Installation:
Click here to download block.meta_tag.txt
Click here to download outputfilter.meta_tag.txt
Change the file extension on both files to .php, then copy them to your Smarty plugins directory (smarty/plugins). Detailed usage instructions can be found in the comment block of each file.







