I manages various website and some of them are running on Serendipity blog engine or a Digg-looks-alike, Pligg. Both engine use Smarty framework based on PHP code. Although Smarty built from PHP engine, it has different style in code writing than another CMS based on PHP like WordPress. Smarty using their own tags that will be parsed on their engine, so adding Javascript or PHP code can not be directly written in the related tpl file (an extension for Smarty template).
If you wish to add a Javascript code like Google Adsense or a PHP code into Smarty template, use the following tips :

  1. Open your related tpl file and use {php} and {/php} tags to include the PHP file
    [code language=’php’]
    {php}include(‘/home//vavai/vavai.net/myphpfile.php’);{/php}
    [/code]
    Please note that I would prefer to use absolute path and not the relative path instead.
  2. Open your related tpl file and use the {literal} and {/literal} tags to add the Javascript code. Place your Javascript code inside literal tags as shown on the following example :
    [code language=’php’]
    {literal}{/literal}
    [/code]

On the sample #1, I would prefer to include the php file rather than write the PHP code directly on Smarty file. It will make a code a lot easier to manage, clean, easy to understand and we have a flexible way to add or remove the related file.

4 thoughts on “How to Add PHP & Javascript Code to Smarty Template (Serendipity, Pligg)”
  1. Hi, I came across this blog post while searching for help with JavaScript. I have recently changed browsers from Google Chrome to IE. Now I seem to have a problem with loading JavaScript. Every time I go on a page that requires Javascript, my computer does not load and I get a “runtime error javascript.JSException: Unknown name”. I can’t seem to find out how to fix it. Any help is very appreciated! Thanks

  2. Hi, I came across this blog post while searching for help with JavaScript. I have recently changed browsers from Google Chrome to IE. Now I seem to have a problem with loading JavaScript. Every time I go on a page that requires Javascript, my computer does not load and I get a “runtime error javascript.JSException: Unknown name”. I can’t seem to find out how to fix it. Any help is very appreciated! Thanks

Leave a Reply to Grady Bekele Cancel reply

Your email address will not be published.