使用Smarty作为你的模板引擎

概述

由于Malaney J. Hill的工作我们将Smarty模板引擎集成到Seagull中。下载最新Smarty包,解压并安装到eagull/lib/other/目录下,删掉版本号,你会得到

/path/to/seagull/lib/other/Smarty

然后在模板引擎下拉框中选择Smarty。这将使用Smarty主题的模板:

/path/to/seagull/www/smarty

另一个方法,很容易实现,就是使用pearified.com版本的Smarty(http://pearified.com/index.php?package=Smarty)。使用它将更容易更新和维护。这样做将要求修改Smarty的include路径

/path/to/seagull/lib/SGL/HtmlSmartyRendererStrategy.php

或在Seagull 0.6.0中

/path/to/seagull/lib/SGL/HtmlRenderer/SmartyStrategy.php

技巧

如果你想获得完整的错误信息,将文件lib/SGL/HtmlRenderer/SmartyStrategy.php中的下列两行注释掉:

SGL::setNoticeBehaviour(SGL_NOTICES_DISABLED);
SGL::setNoticeBehaviour(SGL_NOTICES_ENABLED);

不改变默认的呈现器使用Smarty

它将是非常有用的,如果:

  • 你现在有一个使用Smarty应用程序并想将它转化成Seagull的模块而不将默认的模板引擎修改为Smarty
  • 你想使你的模板使用Smarty因为你觉得使用Smarty比默认模板引擎更舒服

1. 确认在www/themes/default目录中存在这个文件masterSmarty.html,它包含:

<flexy:include src="header.html" />
<flexy:include src="banner.html" />
<flexy:include src="blocksLeft.html" />

<div id="content">
{outputBody(#smarty#)}
</div>

<flexy:include src="blocksRight.html" />
<flexy:include src="footer.html" />

2. 将这行放到你的模块管理类的构造函数中

$this→masterTemplate = 'masterSmarty.html';

3. 将Smarty模板放到你的模块目录中

/path/to/seagull/smarty/yourmodule/

4. 你平常一样在你的模块管理类中选择模板

$output->template = 'yourtemplate.html';

5. 输出对象中的变量如

$output->yourVariable = 'Hello World!';

在Smarty模块中可以这么调用:

{$result->yourVariable}

输出对象的方法可以这样调用

{$result->makeUrl("youraction","yourmanager", "yourmodule")}

就这些了,现在你可以在你的模块中使用Smarty模板而不修改默认的模板主题。

直接给Smarty赋值

当转移一个现在的使用Smarty的应用程序到Seagull,直接将要输出的值赋给Smarty实例比赋给Seagull的输出对象节省了我很多时间。

$smarty->assign('yourVariable', 'Hello Wold!');

这是使用Smarty的应用程序输出的常用方式。所以不管是模板还是代码片断都要改变以便在Seagull管理类中使用。

要这么做你必须有权限访问Smarty实例。你可以SGL_Smarty类获得一个实例。SGL_Smarty是Smarty输出呈现策略的一部分。

require_once SGL_LIB_DIR . '/SGL/HtmlRenderer/SmartyStrategy.php';
$smarty = & SGL_Smarty::singleton();

我不敢确定这是否破坏了Seagull的概念特性。但是对于移植现在应用程序它确实会有很大帮助。对新开发的应用程序我建议使用Seagull的输出对象。在使用Smarty时两种输出方法可以同时使用。

参考资料

  • Smarty文档
  • trunk/etc/flexy2SmartyRunner.php
  • trunk/etc/Flexy2Smarty.php
 
howto/templates/smarty.txt · 最后更改: 2010/05/30 00:21 (外部编辑)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2