Carolv

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.

Related Posts

4 thoughts on “Smarty SEO plugin: meta_tag

  1. Thanks for sharing…I think is a great plugin…
    but I’m just having a little problem trying to implement it.

    Do I need to rename “block.meta_taq.php” to “function.block.meta_tag” and then create a plugin sub-directory in order for it to work?

  2. This is a really great plugin and accomplished exactly what I was looking for. I love how flexible it can be with either adding to existing meta tags or replacing them outright. I have the misfortune of working on a project that does it’s smarty processing in the exact opposite way that you recommend (one template that includes all others and a singe call to display), so I had to do a bit of hackery to get it to work. Basically, turn on output buffering with ob_start() before doing anything else, and specify a callback function as the first parameter. Then use that callback function to apply the filter to the page that all the other display() calls assembled. Here’s a snippet:

    display function once
    * for each template fragment (as opposed to a single call after everything has
    * been assembled) we cannot just use the normal way of assigning an output filter
    * to our smarty object. Instead, we will capture the content from the output buffer
    * just-in-time and call the filter ourselves.
    */
    function substitute_meta_tags($content)
    {
    include_once(SMARTY_DIR.”plugins/outputfilter.meta_tag.php”);
    // Call the output filter manually. Send in a dummy smarty object to catch any errors.
    return smarty_outputfilter_meta_tag($content, MySmarty::i());
    }
    ob_end_flush();

Comments are closed.