Home > php > PHP templating with Smarty

PHP templating with Smarty

I found using Smarty in PHP the most effective way to avoid spaghetti codes (codes that mix programming logic and html output that rely heavily on string concatenation), especially in the following two use cases
1) Generate html output in a structured, organized way
Let’s look at an example:

cat test.php

    require_once '/usr/share/php/smarty3/Smarty.class.php';
    // stock path installed with apt-get install smarty3 in ubuntu, you might need to adjust the require_once path
    // if you install smarty in other distros or through other means
    $smarty=new Smarty();
    $smarty->assign('label', 'my favorite tools');
    $smarty->assign('friends', array('node.js', 'underscore.js', 'backbone.js', 'Smarty', 'swift mailer', 'express'));

cat templates/layout.tpl

        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>{block name=title}{/block}</title>
        <link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" charset="utf-8">
        {block name=morecss}{/block}
    {block name=body}{/block}   
    <script type="text/javascript" src="path/to/jquery.min.js" charset="utf-8"></script>
    {block name=morejs}{/block} 

cat templates/test.tpl

{extends file='layout.tpl'}
{block name=title}{$label|capitalize}{/block}
{block name=morejs}
	<script type="text/javascript" src="path/to/my.main.js" charset="utf-8"></script>
{block name=body}
Say hello to {$label}:
	{foreach $friends as $friend}

2) Prepare html (or plain text) email message to send out
cat email.php

    require_once '/usr/share/php/smarty3/Smarty.class.php';
    $smarty=new Smarty();
    $smarty->assign('url', 'http://a.link.to.reset/your/password');
    // send_email_to($someaddress, $somesuject, $message);
    // you can echo it out as well
    // echo $message;

cat templates/email/master.tpl

		<div id="content" style="width:400px;margin:40px auto"> 
			{block name=body}{/block}

cat templates/email/forget.tpl

{extends file='email/master.tpl'}
{block name=body}
Dear User,
Here's the url to reset your password:<br />
<a href="{$url}">{$url}</a>
Blah Blah

Notes on directory structure:
1) in the above examples, the file/folder structure is as follows (generated through this little powerful tool called tree, installed via ‘apt-get install tree‘)

2) folders cache and templates_c are given the following ownership/perimssion:
ls -ld cache templates_c/
drwxr-xr-x 2 www-data www-data 4096 2012-07-03 18:23 cache/
drwxr-xr-x 2 www-data www-data 4096 2012-07-03 22:35 templates_c/

Categories: php
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: