Smarty (a PHP template engine)

[1] Introduction (ref. official site:http://www.smarty.net )

Smarty is a template engine for PHP. More specifically, it facilitates a manageable way to separate application logic and content from its presentation.

Some of Smarty’s features:
• It is extremely fast.
• It is efficient since the PHP parser does the dirty work.
• No template parsing overhead, only compiles once.
• It is smart about recompiling only the template files that have changed.
• You can easily create your own custom functions and variable modifiers, so the template language is extremely extensible.
• Configurable template {delimiter} tag syntax, so you can use {$foo}, {{$foo}}, <!–{$foo}–>, etc.
• The {if}..{elseif}..{else}..{/if} constructs are passed to the PHP parser, so the {if…} expression syntax can be as simple or as complex an evaluation as you like.
• Allows unlimited nesting of sections, if’s etc.
• It is possible to embed PHP code right in your template files, although this may not be needed (nor recommended) since the engine is so customizable.
• Built-in caching support
• Arbitrary template sources
• Custom cache handling functions
• Plugin architecture

[2] Installation

Smarty tarball has been unpacked to:
/usr/local/lib/Smarty-v.e.r/ for *nix machines
d:\EasyPHP\php\Smarty-v.e.r\ for the windows enviroment.

ps. Add the library path to the php.ini file

;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;
; *nix: “/path1:/path2”
include_path = “.:/usr/share/php:/usr/local/lib/Smarty-v.e.r/libs/”
; Windows: “\path1;\path2”
include_path = “.;d:\php\includes;d:\EasyPHP\php\Smarty-v.e.r\libs\”

ps. restart httpd

[3] File structure

/www/example/  (http://locahost/example/)

templates/
index.tpl (template file)
templates_c/   (make it be writable)
configs/
cache/
(make it be writable)
index.php  (php file)

ps. Permissions and making directories writable
chown nobody:nobody /web/www.example.com/guestbook/templates_c/
chmod 777 /web/www.example.com/guestbook/templates_c/
chown nobody:nobody /web/www.example.com/guestbook/cache/
chmod 777 /web/www.example.com/guestbook/cache/

ps. chmod 777 runs correctly. (original ref. is 770)

[4] Example

/www/example/main.php

<?php
require_once(‘Smarty.class.php’);

class Smarty_example extends Smarty{
public function __construct(){
parent::__construct();

$this->template_dir=’D:\\EasyPHP3\\www\\example\\templates\\’;
$this->compile_dir=’D:\\EasyPHP3\\www\\example\\templates_c\\’;
$this->config_dir=’D:\\EasyPHP3\\www\\example\\configs\\’;
$this->cache_dir=’D:\\EasyPHP3\\www\\example\\cache\\’;

$this->caching = true;

}
}
?>

/www/example/templates/index.tpl

<!DOCTYPE HTML PUBLIC ‘-//W3C//DTD HTML 4.01 Transitional//EN’>
<html>
<head>
<meta http-equiv=’content-type’ content=’text/html; charset=UTF-8′>

<title>{$title}</title>
</head>
<body>
<p>{$text}</p>
<hr>
<p>Output by <a href=’http://www.smarty.net/’>Smarty</a></p>
</body>
</html>

/www/example/index.php

<?php
require_once(‘main.php’);
$smarty = new Smarty_example();

$smarty->assign(‘title’,’第一個Smarty網頁’);
$smarty->assign(‘text’,’利用樣板引擎輸出’);
$smarty->display(‘example.tpl’);
?>

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • MySpace
  • Plurk
  • RSS
  • Google Buzz
  • Live
本篇發表於 Smarty。將永久鏈結加入書籤。

Smarty (a PHP template engine) 有 1 則回應

  1. kristen stewart 說道:

    hi there great little blog ya have there 😉 I use the identical theme on my own website however for whatever weird factor it looks to stream faster on this blog despite the fact that this blog is carrying alot more content material. Are you currently utilizing any plugins or widgets which quicken it up? Do you think you could possibly show the programs so I can use these in my own webpage so twilight new moon users could watch twilight new moon online trailers and films easier I’d personally be ever so grateful – many thanks ahead of time 🙂

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *