免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2002 | 回复: 2
打印 上一主题 下一主题

[框架] Zend Framework: Using Smarty as template engine(zt) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-05-23 13:46 |只看该作者 |倒序浏览
Zend Framework’s View class has very bad capability for extending. It contains template variables but does not allow to access them, it has array with different pathes (templates, filters), but does not allow to add another type or access them. Therefor only way to use Smarty with Zend Framework is to abandon Zend_View and manipulate Smarty object directly.

First we need to create Smarty object. I do it in index.php after including Zend.php:


  1. <?php

  2. require('Zend.php');

  3. include 'smarty/Smarty.class.php';
  4. $smarty = new Smarty();
  5. $smarty->debugging = false;
  6. $smarty->force_compile = true;
  7. $smarty->caching = false;
  8. $smarty->compile_check = true;
  9. $smarty->cache_lifetime = -1;
  10. $smarty->template_dir = 'resources/templates';
  11. $smarty->compile_dir = 'resources/templates_c';
  12. $smarty->plugins_dir = array(
  13.   SMARTY_DIR . 'plugins',
  14.   'resources/plugins');

  15. ?>
复制代码


I don’t like global variables therefor I’ve added Smarty object into Zend Framework’s registry:


  1. <?php

  2. Zend::register('smarty', $smarty);

  3. ?>
复制代码


Using is pretty simple. Just initialize Smarty variables in your Controller class and display template:


  1. <?php

  2. class IndexController extends Zend_Controller_Action
  3. {
  4.   function index()
  5.   {
  6.     $smarty = Zend::registry('smarty');
  7.     $smarty->assign('title', 'Test');
  8.     $smarty->display('index.tpl');
  9.   }

  10.   function noRoute()
  11.   {
  12.     $smarty = Zend::registry('smarty');
  13.     $smarty->display('error404.tpl');
  14.   }
  15. }

  16. ?>
复制代码


As you can see Smarty integration with Zend Framework is very simple task. Zend_View has ability to use your own filters and helper functions, but with Smarty you don’t need them because Smarty has its own plugins, filters, modifiers. Just forget about Zend_View and use best template engine for PHP in the world!

[ 本帖最后由 HonestQiao 于 2006-5-23 13:47 编辑 ]

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
2 [报告]
发表于 2006-05-23 13:47 |只看该作者
框架和模版系统结合,好处很多。

但是也有很多不足之处,框架本身的很多有用的特性会丢失的。

论坛徽章:
0
3 [报告]
发表于 2006-05-23 14:00 |只看该作者
原帖由 z33 于 2006-5-23 13:46 发表
Zend Framework’s View class has very bad capability for extending. It contains template variables but does not allow to access them, it has array with different pathes (templates, filters), but do ...

懒得用框架,我是一懒人
懒得用smarty,我是一懒人
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP