How to Add PHP & Javascript Code to Smarty Template (Serendipity, Pligg)

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
    {php}include('/home//vavai/vavai.net/myphpfile.php');{/php}
    

    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 :
    {literal}<script type="text/javascript">// <![CDATA[
    initializetabcontent("tags_comments")
    // ]]></script>{/literal}
    

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.

Related Entries

2 Responses for “How to Add PHP & Javascript Code to Smarty Template (Serendipity, Pligg)”

  1. [...] How to Add PHP & Javascript Code to Smarty Template (Serendipity … [...]

  2. Grady Bekele says:

    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

About Vavai

Masim Vavai Sugianto Masim Vavai Sugianto, Indonesian, male, 32 years old, born and live in Bekasi-West Java, a small town near Jakarta – main city of Indonesia – since 17 May 1976. Founder of Indonesian openSUSE and Zimbra Community, an adventure, travelling and book lover.



I live in a tropical country, Indonesia that only has two seasons, dry season and rainy season. I love the dry season with bright sunshine and rare rain…There is a joke about the seasons in Indonesia. Indonesia is known as a country with so many season, ie : durian season, mango season, married season and much more...



ContactI'm currently working as an IT support for a small company based on Jakarta. My career has led me to specialize in Sysadmin, networking and software implementation with current focus on Linux and open source area. I have experience on MRP, ERP, Gemba Kaizen, Just in Time, Six Sigma and TQC/TQM. Please use my contact page if you wish to contact me.
Log in - BlogNews Theme by Gabfire themes