Smarty 模板引擎和 Gzip 编码

发布于 2024-10-10 01:54:27 字数 1511 浏览 0 评论 0原文

我对 smarty、Zend 和 gzip 编码有一点问题,我扩展了 smarty 类

//This method i call in one front controller plugin
$this->getResponse()->setHeader('Content-Encoding' , 'gzip');

View extends Zend_View_Abstract implements Zend_View_Interface {

   public $_smarty;

   public function __construct(){

      $this->_smarty = new Smarty();
      //Hire i have some smarty options paths and etc.
      //------------------
      //I register this object to smarty template
      $this->_smarty->registerObject('Smarty', $this);

      //You can see this pulugin at this address
      //http://smarty.incutio.com/?page=GZipPlugin
      $this->_smarty->loadFilter('output', 'gzip');

   }


   public function Fetch($tpl){
      retutn $this->_smarty->fetch($tpl);
   }

   //Zend call this method after loaded controller and display active controller tpl
   public function Render($tpl){
      retutn $this->_smarty->display($tpl);
   }

   public function Header($params, &$smarty){
      $this->_smarty->display('header.tpl');
   }


}

好吧... 在我的 index.tpl 中,我调用函数 {Site->header} 我的浏览器 chrome 抛出错误:

Server error.

The website encountered an error while retrieving http://site.dev. It may be down for maintenance or configured incorrectly.

我尝试使用 fetch 加载:

echo $this->_smarty->fetch('header.tpl');

但当我删除输出填充程序站点正在运行时,我遇到了相同的错误。

如果有人可以帮助我,我将不胜感激。 抱歉,如果我的英语不是很好。 提前致谢。

I've a bit problem with smarty, Zend and gzip encoding, I extends the smarty class

//This method i call in one front controller plugin
$this->getResponse()->setHeader('Content-Encoding' , 'gzip');

View extends Zend_View_Abstract implements Zend_View_Interface {

   public $_smarty;

   public function __construct(){

      $this->_smarty = new Smarty();
      //Hire i have some smarty options paths and etc.
      //------------------
      //I register this object to smarty template
      $this->_smarty->registerObject('Smarty', $this);

      //You can see this pulugin at this address
      //http://smarty.incutio.com/?page=GZipPlugin
      $this->_smarty->loadFilter('output', 'gzip');

   }


   public function Fetch($tpl){
      retutn $this->_smarty->fetch($tpl);
   }

   //Zend call this method after loaded controller and display active controller tpl
   public function Render($tpl){
      retutn $this->_smarty->display($tpl);
   }

   public function Header($params, &$smarty){
      $this->_smarty->display('header.tpl');
   }


}

Ok...
in my index.tpl i call the function {Site->header}
and my browser chrome throw the error:

Server error.

The website encountered an error while retrieving http://site.dev. It may be down for maintenance or configured incorrectly.

i've tried to load with fetch like:

echo $this->_smarty->fetch('header.tpl');

but i have the same error, when i remove the out put fillter site is running.

If anyone can help me I would been greatly appreciate it.
Sorry if my english is not very good.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必在意 2024-10-17 01:54:27

我同意 mfonda 的观点,不要使用 Smarty。

我在需要时使用此插件类来压缩我的整个身体响应:

class Lib_Controller_Plugin_Gzip extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopShutdown()
    {
        $content = $this->getResponse()->getBody();

        $content = preg_replace(
                    array('/(\x20{2,})/',   '/\t/', '/\n\r/'),
                    array(' ',      ' ',    ' '),
                    $content
                );

        if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === FALSE) 
            $this->getResponse()->setBody($content);
        else 
        {
            header('Content-Encoding: gzip');
            $this->getResponse()->setBody(gzencode($content, 9));
        }
    }
}

请注意使用 dispatchLoopShutdown 因为 这篇文章

该类改编自 this我使用 Google 发现的帖子

I agree with mfonda, don't use Smarty.

I use use this plugin class to Gzip my entire body response when needed:

class Lib_Controller_Plugin_Gzip extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopShutdown()
    {
        $content = $this->getResponse()->getBody();

        $content = preg_replace(
                    array('/(\x20{2,})/',   '/\t/', '/\n\r/'),
                    array(' ',      ' ',    ' '),
                    $content
                );

        if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === FALSE) 
            $this->getResponse()->setBody($content);
        else 
        {
            header('Content-Encoding: gzip');
            $this->getResponse()->setBody(gzencode($content, 9));
        }
    }
}

Note the use of dispatchLoopShutdown because of this post.

The class was adapted from this post I found by using Google

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文