Per-Page Meta Tags Using Page Properties

    Here's an example of how to use Dekiscript, Page Properties, and a global template to render page-specific meta tags.

    Goal

    In this example, we want to be able to render page-specific meta tags without requiring page authors to add dekiscript code (eg: meta.custom) in the page content.  Instead, we'd like to rely on specially-named page properties (ex: name: "meta-mytag", text: "mytext").  A template will then iterate over all page properties and call meta.custom() for each meta-* property.

     

    Meta Template

    Create a new page at /Template:Meta with the following content.  Make sure you use a formatted region and apply the Dekiscript style.

    var pg = wiki.getpage(num.cast(__request.args.pageid));
    foreach(var property in pg.properties) {
         if (string.startswith(property.name, "meta-")) {
            meta.custom(string.substr(property.name, 5), property.text);
        }
    }

    As you can see, this code is pretty simple.  We lookup the current page by id, iterate over the properties to find any property named meta-*, then render that tag using the meta.custom() dekiscript function.

    Global Template

    Create a global template at /Template:GlobalTemplate with the following content.   Make sure you use a formatted region and apply the Dekiscript style.

    wiki.template("Meta");

     

    Add GlobalTemplate to wgPreloadPages array

    In order for the GlobalTemplate to be invoked on each page load, you must add it to the $wgPreloadPages array in LocalSettings.php.  Add the following code in LocalSettings.php

    $wgPreloadPages = array('Template:GlobalTemplate');

     

    Conclusion

    To add a meta tag, just create a page property with a name like meta-mytag and specify it's value.  View the page source to ensure that the meta tag is being rendered properly.

     

    Improvements

    It should be possible to use jquery to inject a simple meta tag editor into each page, then modify properties with an AJAX call to the API.

    Tag page
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by