<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Apelog &#187; template</title>
	<atom:link href="http://blog.apecell.com/tag/template/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.apecell.com</link>
	<description>Apecell's blog by Design</description>
	<lastBuildDate>Tue, 22 Jun 2010 07:08:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>TemplateLiteは割り当てたオブジェクトにアクセスできない</title>
		<link>http://blog.apecell.com/2008/01/01/id/29</link>
		<comments>http://blog.apecell.com/2008/01/01/id/29#comments</comments>
		<pubDate>Mon, 31 Dec 2007 18:00:00 +0000</pubDate>
		<dc:creator>design</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[Program]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://blog.apecell.com/1970/01/01/29</guid>
		<description><![CDATA[Smarty互換でSmartyより軽量なTemplateLiteを使うに書いた後適度に使っていたところ、Smartyと決定的な差異を発見。

TemplateLiteはオブジェクトにアクセスできない。はじめからオブジェクトをアサインしないような作りにしておけば問題無いものの、既にSmartyでオブジェクトをアサインして使っている場合、テンプレートエンジンをそのまま差し替えるわけにはいかないので注意。
例えば、Zend_Validateのエラー結果を纏めて以下のようなクラスに渡したオブジェクトをアサインしているとする。

&#60;?php
/**
 * @package Sample
 */
class Sample_Validate_Result {
    /**
     * @var array
     */
    private $_errors;

    /**
     * @param array $errors
     */
    public function __construct(array $errors) {
   [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.apecell.com/archives/318974.html" target="_blank">Smarty互換でSmartyより軽量なTemplateLiteを使う</a><br />に書いた後適度に使っていたところ、Smartyと決定的な差異を発見。</p>
</p>
<p>TemplateLiteはオブジェクトにアクセスできない。はじめからオブジェクトをアサインしないような作りにしておけば問題無いものの、既にSmartyでオブジェクトをアサインして使っている場合、テンプレートエンジンをそのまま差し替えるわけにはいかないので注意。</p>
<p>例えば、Zend_Validateのエラー結果を纏めて以下のようなクラスに渡したオブジェクトをアサインしているとする。</p>
<pre name="code" class="php">
&lt;?php
/**
 * @package Sample
 */
class Sample_Validate_Result {
    /**
     * @var array
     */
    private $_errors;

    /**
     * @param array $errors
     */
    public function __construct(array $errors) {
        $this-&gt;_errors = $errors;
    }

    /**
     * エラーの有無を返す
     *
     * @param string $name
     * @return mixed
     */
    public function __get($name) {
        if (array_search($name, $this-&gt;_errors) !== false) {
            return true;
        } elseif (!array_key_exists($name, $this-&gt;_errors)) {
            return false;
        } else {
            return new Sample_Validate_Result($this-&gt;_errors[$name]);
        }
    }
}
</pre>
<p>その状態でテンプレートからアサインしたオブジェクトのメンバ変数にアクセスしようとTemplateLiteの場合、evalに失敗してエラー。</p>
<pre name="code" class="php">
&lt;?php
// Validatorから帰ってくるエラー例
$errorArray = array(
    'nickname'    =&gt; array('isEmpty'),
    'mailaddress' =&gt; array('tooLong', 'invalid')
);

// コントローラー内でエラーオブジェクトをアサイン
$this-&gt;view-&gt;errors = new Sample_Validate_Result($errorsArray);

// Smartyならこれができる
{ if $errors-&gt;nickname-&gt;isEmpty }
</pre>
<p>完全互換だと思っていて差し替えが後になってしまうと、影響範囲も広がってしまうので記憶の片隅にでも。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apecell.com/2008/01/01/id/29/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smarty互換でSmartyより軽量なTemplateLiteを使う</title>
		<link>http://blog.apecell.com/2007/10/19/id/18</link>
		<comments>http://blog.apecell.com/2007/10/19/id/18#comments</comments>
		<pubDate>Thu, 18 Oct 2007 15:00:00 +0000</pubDate>
		<dc:creator>design</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[Program]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://blog.apecell.com/1970/01/01/18</guid>
		<description><![CDATA[SmartyはPHPのテンプレートエンジンでは最も有名で利用者も多いが重い。
TemplateLiteはLGPLライセンスのSmarty互換テンプレートエンジンでSmartyより早いとのこと。PHP5.1.1でSmartyとのベンチマーク比較を見る限りテンプレートで色々やってると結構差が出そう。
公式に書かれている、「TemplateLiteを使うべき7つの理由（意訳）」

Smartyよりコンパイルが早い
Smartyより表示が速い
Smartyと比較してメモリ使用量が少ない
Smartyの機能のほとんどをサポート
Smartyの代替となる
各リリースに合わせて機能追加される
活発な開発プロジェクト

6.の「各リリース」というのはSmartyに合わせてという事かな。気になるなら原文参照。
使い方もSmartyと同じなのでSmartyの重さが気になっているなら丁度良い代替になりそう。ZendFrameworkのViewとして使う場合も、Smartyの部分をTemplateLiteに置き換えればそのまま動作する。
単純なページだとそこまで差が出ないので、純粋にincludeのコストが気になる場合はSimplateなどの PHP Extention なテンプレートエンジンを使った方が良い。
以下ZendFrameworkのViewRenderer用クラス。ZendFrameworkの別テンプレートエンジン使用方法はZend FrameworkのViewをSmartyに変更するにはを参照。

&#60;?php
require_once 'Zend/View/Interface.php';
require_once 'class.template.php';

class Sample_View_TemplateLite implements Zend_View_Interface
{
    /**
     * TemplateLite object
     * @var Template_Lite
     */
    protected $_templateLite;

    /**
     * コンストラクタ
     *
   [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://smarty.php.net/" target="_blank">Smarty</a>はPHPのテンプレートエンジンでは最も有名で利用者も多いが重い。</p>
<p><a href="http://templatelite.sourceforge.net/" target="_blank">TemplateLite</a>はLGPLライセンスのSmarty互換テンプレートエンジンでSmartyより早いとのこと。PHP5.1.1で<a href="http://templatelite.sourceforge.net/images/templatelite_speed.gif" target="_blank">Smartyとのベンチマーク比較</a>を見る限りテンプレートで色々やってると結構差が出そう。</p>
<p>公式に書かれている、「TemplateLiteを使うべき7つの理由（意訳）」</p>
<ol>
<li>Smartyよりコンパイルが早い</li>
<li>Smartyより表示が速い</li>
<li>Smartyと比較してメモリ使用量が少ない</li>
<li>Smartyの機能のほとんどをサポート</li>
<li>Smartyの代替となる</li>
<li>各リリースに合わせて機能追加される</li>
<li>活発な開発プロジェクト</li>
</ol>
<p>6.の「各リリース」というのはSmartyに合わせてという事かな。気になるなら原文参照。</p>
<p>使い方もSmartyと同じなのでSmartyの重さが気になっているなら丁度良い代替になりそう。ZendFrameworkのViewとして使う場合も、Smartyの部分をTemplateLiteに置き換えればそのまま動作する。</p>
<p>単純なページだとそこまで差が出ないので、<em>純粋にincludeのコストが気になる場合</em>はSimplateなどの PHP Extention なテンプレートエンジンを使った方が良い。</p>
<p>以下ZendFrameworkのViewRenderer用クラス。ZendFrameworkの別テンプレートエンジン使用方法は<a href="http://blog.apecell.com/archives/264496.html" target="_blank">Zend FrameworkのViewをSmartyに変更するには</a>を参照。</p>
<pre name="code" class="php">
&lt;?php
require_once 'Zend/View/Interface.php';
require_once 'class.template.php';

class Sample_View_TemplateLite implements Zend_View_Interface
{
    /**
     * TemplateLite object
     * @var Template_Lite
     */
    protected $_templateLite;

    /**
     * コンストラクタ
     *
     * @param string $tmplPath
     * @param array $extraParams
     * @return void
     */
    public function __construct($tmplPath = null, $extraParams = array())
    {
        $this-&gt;_templateLite = new Template_Lite;

        if (null !== $tmplPath) {
            $this-&gt;setScriptPath($tmplPath);
        }

        foreach ($extraParams as $key =&gt; $value) {
            $this-&gt;_templateLite-&gt;$key = $value;
        }
    }

    /**
     * テンプレートエンジンオブジェクトを返します
     *
     * @return Template_Lite
     */
    public function getEngine()
    {
        return $this-&gt;_templateLite;
    }

    /**
     * テンプレートへのパスを設定します
     *
     * @param string $path パスとして設定するディレクトリ
     * @return void
     */
    public function setScriptPath($path)
    {
        if (is_readable($path)) {
            $this-&gt;_templateLite-&gt;template_dir = $path;
            return;
        }

        throw new Exception('無効なパスが指定されました');
    }

    /**
     * 現在のテンプレートディレクトリを取得します
     *
     * @return string
     */
    public function getScriptPaths()
    {
        return $this-&gt;_templateLite-&gt;template_dir;
    }

    /**
     * setScriptPath へのエイリアス
     *
     * @param string $path
     * @param string $prefix Unused
     * @return void
     */
    public function setBasePath($path, $prefix = 'Zend_View')
    {
        return $this-&gt;setScriptPath($path);
    }

    /**
     * setScriptPath へのエイリアス
     *
     * @param string $path
     * @param string $prefix Unused
     * @return void
     */
    public function addBasePath($path, $prefix = 'Zend_View')
    {
        return $this-&gt;setScriptPath($path);
    }

    /**
     * 変数をテンプレートに代入します
     *
     * @param string $key 変数名
     * @param mixed $val 変数の値
     * @return void
     */
    public function __set($key, $val)
    {
        $this-&gt;_templateLite-&gt;assign($key, $val);
    }

    /**
     * 代入された変数を取得します
     *
     * @param string $key 変数名
     * @return mixed 変数の値
     */
    public function __get($key)
    {
        return $this-&gt;_templateLite-&gt;get_template_vars($key);
    }

    /**
     * empty() や isset() のテストが動作するようにします
     *
     * @param string $key
     * @return boolean
     */
    public function __isset($key)
    {
        return (null !== $this-&gt;_templateLite-&gt;get_template_vars($key));
    }

    /**
     * オブジェクトのプロパティに対して unset() が動作するようにします
     *
     * @param string $key
     * @return void
     */
    public function __unset($key)
    {
        $this-&gt;_templateLite-&gt;clear_assign($key);
    }

    /**
     * 変数をテンプレートに代入します
     *
     * 指定したキーを指定した値に設定します。あるいは、
     * キー =&gt; 値 形式の配列で一括設定します
     *
     * @see __set()
     * @param string|array $spec 使用する代入方式 (キー、あるいは キー =&gt; 値 の配列)
     * @param mixed $value (オプション) 名前を指定して代入する場合は、ここで値を指定します
     * @return void
     */
    public function assign($spec, $value = null)
    {
        if (is_array($spec)) {
            $this-&gt;_templateLite-&gt;assign($spec);
            return;
        }

        $this-&gt;_templateLite-&gt;assign($spec, $value);
    }

    /**
     * 代入済みのすべての変数を削除します
     *
     * Zend_View に {@link assign()} やプロパティ
     * ({@link __get()}/{@link __set()}) で代入された変数をすべて削除します
     *
     * @return void
     */
    public function clearVars()
    {
        $this-&gt;_templateLite-&gt;clear_all_assign();
    }

    /**
     * テンプレートを処理し、結果を出力します
     *
     * @param string $name 処理するテンプレート
     * @return string 出力結果
     */
    public function render($name)
    {
        return $this-&gt;_templateLite-&gt;fetch($name);
    }
}
</pre>
<p>そのまんまですが。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apecell.com/2007/10/19/id/18/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zend FrameworkのViewをSmartyに変更するには</title>
		<link>http://blog.apecell.com/2007/09/26/id/10</link>
		<comments>http://blog.apecell.com/2007/09/26/id/10#comments</comments>
		<pubDate>Tue, 25 Sep 2007 15:00:00 +0000</pubDate>
		<dc:creator>design</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://blog.apecell.com/1970/01/01/10</guid>
		<description><![CDATA[
標準のZend Viewより使い慣れたSmartyを使いたかったので差し替えてみることに。
Zend Framework: Document &#8211; 35.3.2. 別のテンプレートシステムの使用の項目だけだとSmartyは呼び出せても実際どうやってControllerに組み込むの？となってしまう。
とりあえずControllerのViewを差し替えてみるかと思いZend_Controller_Actionを継承してinit()でSmartyを差し替えた。

&#60;?php
class Sample_Controller_Action extends Zend_Controller_Action {
    /**
     * @var Zend_Config
     */
    protected $_config;

    public function init() {
        // コンフィグの読み込み
        [...]]]></description>
			<content:encoded><![CDATA[<div class="section">
<p>標準のZend Viewより使い慣れたSmartyを使いたかったので差し替えてみることに。</p>
<p><a href="http://framework.zend.com/manual/ja/zend.view.scripts.html#zend.view.scripts.templates" target="_blank">Zend Framework: Document &#8211; 35.3.2. 別のテンプレートシステムの使用</a>の項目だけだとSmartyは呼び出せても実際どうやってControllerに組み込むの？となってしまう。</p>
<p>とりあえずControllerのViewを差し替えてみるかと思いZend_Controller_Actionを継承してinit()でSmartyを差し替えた。</p>
<pre name="code" class="php">
&lt;?php
class Sample_Controller_Action extends Zend_Controller_Action {
    /**
     * @var Zend_Config
     */
    protected $_config;

    public function init() {
        // コンフィグの読み込み
        $this-&gt;_config = Zend_Registry::get('config');

        $extraParams = array(
            'compile_dir' =&gt; $config-&gt;smarty-&gt;compile_dir,
        );
        $this-&gt;view = new Sample_View_Smarty(null, $extraParams);
        $this-&gt;viewSuffix = 'tpl';
    }
}
</pre>
<p>が、上記だと上手くいかなかったわけで。</p>
<p>続いてActionHelperのViewRendererとしてSmartyを登録する方法で挑戦。</p>
<pre name="class" class="php">
<?php
$config = new Zend_Config_Ini('/path/to/config.ini', 'production');

$extraParams = array(
    'compile_dir' => $config->smarty->compile_dir,
);

// マニュアルに書かれているSmarty用View
$view = new Sample_View_Smarty(null, $extraParams);

$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$viewRenderer->setViewBasePathSpec($config->smarty->template_dir)
    ->setViewScriptPathSpec(':controller/:action.:suffix')
    ->setViewScriptPathNoControllerSpec(':action.:suffix')
    ->setViewSuffix('tpl');

Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
</pre>
<p>上記方法で変更出来た。</p>
<p>これは継承したinit()では書かず、Controllerが呼ばれる前に書いた（Zend_Controller_Front::run()の前）。複数のテンプレートエンジン使い分ける事はほとんど無いと思われるのでこれで大丈夫かなと。</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.apecell.com/2007/09/26/id/10/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
