dsrjs86444 2012-12-18 04:47
浏览 136
已采纳

如何在Phalcon \ Mvc \ View中转义html

I found that view variables in phalcon could be escaped by Phalcon\Escaper: http://docs.phalconphp.com/en/latest/api/Phalcon_Escaper.html

For example, in Zend, there is a way to call view helpers from view:

// view context
$this->escape($data); // calls View\Helper\Escape
$this->url($params); // calls url view helper
// etc

Is there a way to get such view helpers without creating new object every time? My current idea is to make some BaseView class, extended from Phalcon\Mvc\View and to define some often-used methods there, that will use cached objects.. but i am not sure that it is the best way:

class BaseView extends Phalcon\Mvc\View
{
    // cached helper objects
    $helpers = [];

    // view helper call
    public function url($params)
    { 
       if (!$this->helpers['url']) {
           $this->helpers['url'] = new Phalcon\Mvc\Url();
       }
       return $this->helpers['url']->get($params);
    }
}
  • 写回答

1条回答 默认 最新

  • doulu8537 2012-12-18 05:39
    关注

    You can access the services in the services container (DI) using $this in a view:

    <?php 
        echo $this->escaper->escape('<h1>Hello</h1>'); //Access Phalcon\Escaper 
    ?>
    
    <?php 
        echo $this->url->get('posts/index'); //Access Phalcon\Mvc\Url
    ?>
    

    Just use the name that was registered in the services container.If you are using Phalcon\DI\FactoryDefault, here's a list of registered services by default: http://docs.phalconphp.com/en/latest/reference/di.html#service-name-conventions

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题