yaf 如何与 smarty 结合

发布于 2021-11-24 01:03:48 字数 145 浏览 694 评论 6

@iDev_周晶 你好,想跟你请教个问题:

刚接触yaf, 请教如何与smarty 模版引擎结合

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

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

发布评论

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

评论(6

泛泛之交 2021-11-26 15:38:44

前几天简单总结过 http://jingwentian.com/t-126 有问题留言

终止放荡 2021-11-26 15:33:15

考虑一下,我目前的环境还真是必须用smarty不可

少女净妖师 2021-11-26 14:57:04

为什么要用smarty呢?效率并不高。

无法言说的痛 2021-11-26 14:52:10

多谢了

滥情空心 2021-11-26 06:15:14

yaf可以跟各种模板引擎结合使用(Twig、Smarty等),方式都一样,简单易用分为以下几步:

首先bootstrap.php中你需要调用Yaf_Dispatcher 的setView方法,将当前请求的view设置为你喜欢的Smarty。前提是你有一个Smarty的适配器可以在Yaf和Smarty之间适配你需要Smarty做的事儿。当然这个适配器必须实现了Yaf_View_Interface这个接口(比如:class Twig_Adapter implements Yaf_View_Interface)。具体看下面的代码:

    public function _initView(Yaf_Dispatcher $dispatcher) {

        $view= new Smarty_Adapter(null, Yaf_Registry::get("config")->get("smarty"));

        Yaf_Dispatcher::getInstance()->setView($view);
    }

在上面你会看到一个Smarty_Adapter的类,没错,这就是我上面说的那个适配器,你需要写这么一个适配器,把你需要view做的功能实现在里面,yaf通过他们来操作smarty的特性。下面给你一个Smarty_Adapter的demo(按名称应该知道他应该是Smarty目录下的一个叫Adapter.php的文件)。

require_once "Smarty.class.php";

class Smarty_Adapter implements Yaf_View_Interface
{
    /**
     * Smarty object
     * @var  Smarty
     */
    public $_smarty;

    /**
     * Constructor
     *
     * @param string $tmplPath
     * @param array $extraParams
     * @return  void
     */
    public function __construct($tmplPath = null, $extraParams = array()) {
        $this->_smarty = new Smarty;

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

        foreach ($extraParams as $key => $value) {
            $this->_smarty->$key = $value;
        }
    }

    /**
     * Return the template engine object
     *
     * @return  Smarty
     */
    public function getEngine() {
        return $this->_smarty;
    }

    /**
     * Set the path to the templates
     *
     * @param string $path The directory to set as the path.
     * @return  void
     */
    public function setScriptPath($path)
    {
        if (is_readable($path)) {
            $this->_smarty->template_dir = $path;
            return;
        }

        throw new Exception('Invalid path provided');
    }

    /**
     * Retrieve the current template directory
     *
     * @return  string
     */
    public function getScriptPath()
    {
        return $this->_smarty->template_dir;
    }

    /**
     * Alias for setScriptPath
     *
     * @param string $path
     * @param string $prefix Unused
     * @return  void
     */
    public function setBasePath($path, $prefix = 'Zend_View')
    {
        return $this->setScriptPath($path);
    }

    /**
     * Alias for setScriptPath
     *
     * @param string $path
     * @param string $prefix Unused
     * @return  void
     */
    public function addBasePath($path, $prefix = 'Zend_View')
    {
        return $this->setScriptPath($path);
    }

    /**
     * Assign a variable to the template
     *
     * @param string $key The variable name.
     * @param mixed $val The variable value.
     * @return  void
     */
    public function __set($key, $val)
    {
        $this->_smarty->assign($key, $val);
    }

    /**
     * Allows testing with empty() and isset() to work
     *
     * @param string $key
     * @return  boolean
     */
    public function __isset($key)
    {
        return (null !== $this->_smarty->get_template_vars($key));
    }

    /**
     * Allows unset() on object properties to work
     *
     * @param string $key
     * @return  void
     */
    public function __unset($key)
    {
        $this->_smarty->clear_assign($key);
    }

    /**
     * Assign variables to the template
     *
     * Allows setting a specific key to the specified value, OR passing
     * an array of key => value pairs to set en masse.
     *
     * @see  __set()
     * @param string|array $spec The assignment strategy to use (key or
     * array of key => value pairs)
     * @param mixed $value (Optional) If assigning a named variable,
     * use this as the value.
     * @return  void
     */
    public function assign($spec, $value = null) {
        if (is_array($spec)) {
            $this->_smarty->assign($spec);
            return;
        }

        $this->_smarty->assign($spec, $value);
    }

    /**
     * Clear all assigned variables
     *
     * Clears all variables assigned to Zend_View either via
     * {@link  assign()} or property overloading
     * ({@link  __get()}/{@link  __set()}).
     *
     * @return  void
     */
    public function clearVars() {
        $this->_smarty->clear_all_assign();
    }

    /**
     * Processes a template and returns the output.
     *
     * @param string $name The template to process.
     * @return  string The output.
     */
    public function render($name, $value = NULL) {
        return $this->_smarty->fetch($name);
    }

    public function display($name, $value = NULL) {
        echo $this->_smarty->fetch($name);
    }

}

然后就是相关的Smarty的配置

$view= new Smarty_Adapter(null, Yaf_Registry::get("config")->get("smarty"));

你需要在配置文件中smarty的那节指明Smarty相关的配置

smarty.template_dir = APP_ROOT "/application/views/"
smarty.compile_dir = APP_ROOT "/cache/template_c/"
smarty.config_dir = APP_ROOT "/conf/"

下面给出一个Twig模板引擎的demo,为什么要给出一个Twig的demo,因为Twig的类加载机制跟Yaf本身的类加载机制一样,而Smarty则有自己的类加载机制。编译模板的时候Twig比Smarty更胜一筹,运行的效率方面,网上的测评是Smarty稍好,不过都在毫秒级以下,who care呢?我就是喜欢Twig,喜欢Yaf。

下面按顺序给出Twig的配置,Bootstrap和Twig_Adapter

[twig]
twig.cache=true
twig.autoescape=true
twig.debug=true

    public function _initView(Yaf_Dispatcher $dispatcher) {
        $view = new Twig_Adapter(ROOT_DIR . "/admin/views/", Yaf_Registry::get("config")->get("twig")->toArray());
        $dispatcher->setView($view);
    }

class Twig_Adapter implements Yaf_View_Interface {

    /**
     * assigned vars
     * @var  array
     */
    protected $_assigned = array();

    /**
     * twig environment
     * @var  Twig_Environment
     */
    protected $_twig;
    
    /**
     * @var  Twig_Loader_Filesystem
     */
    protected $_loader;

    /**
     * class constructor
     *
     * @param string $templatePath
     * @param array $envOptions options to set on the environment
     * @return  void
     */
    public function __construct($templatePath=null, $envOptions=array()) {
        $this->_loader = new Twig_Loader_Filesystem($templatePath);
        $envOptions +=  array('debug' => true,);//for debug
        $this->_twig = new Twig_Environment($this->_loader, $envOptions);
        //for debug
        $this->_twig->addExtension(new Twig_Extension_Debug());
    }

    public function addFunction($name, Twig_FunctionInterface $function) {
        $this->_twig->addFunction($name, $function);
    }

    public function addGlobal($name, $value) {
        $this->_twig->addGlobal($name, $value);
    }

    /**
     * Set the template loader
     *
     * @param Twig_LoaderInterface $loader
     * @return  void
     */
    public function setLoader(Twig_LoaderInterface $loader) {
        $this->_twig->setLoader($loader);
    }

    /**
     * Get the template loader
     *
     * @return  Twig_LoaderInterface
     */
    public function getLoader() {
        return $this->_loader;
    }

    /**
     * Get the twig environment
     * 
     * @return  Twig_Environment
     */
    public function getEngine() {
        return $this->_twig;
    }

    /**
     * Set the path to the templates
     *
     * @param string $path The directory to set as the path.
     * @return  void
     */
    public function setScriptPath($paths) {
        $this->_loader->addPath($paths);
    }

    /**
     * add the path to the templates
     *
     * @param string $path The directory to set as the path.
     * @return  void
     */
    public function addScriptPath($path) {
        $this->_loader->addPath($path);
    }

    /**
     * Retrieve the current template directory
     *
     * @return  string
     */
    public function getScriptPath() {
        return $this->_loader->getPaths();
    }

    /**
     * No basepath support on twig, therefore alias for "setScriptPath()"
     *
     * @see  setScriptPath()
     * @param string $path
     * @param string $prefix Unused
     * @return  void
     */
    public function setBasePath($path, $prefix = 'Zend_View') {
        return $this->setScriptPath($path);
    }

    /**
     * No basepath support on twig, therefore alias for "setScriptPath()"
     *
     * @see  setScriptPath()
     * @param string $path
     * @param string $prefix Unused
     * @return  void
     */
    public function addBasePath($path, $prefix = 'Zend_View') {
        return $this->setScriptPath($path);
    }

    /**
     * Assign a variable to the template
     *
     * @param string $key The variable name.
     * @param mixed $val The variable value.
     * @return  void
     */
    public function __set($key, $val) {
        $this->assign($key, $val);
    }

    /**
     * Allows testing with empty() and isset() to work
     *
     * @param string $key
     * @return  boolean
     */
    public function __isset($key) {
        return isset($this->_assigned[$key]);
    }

    /**
     * Allows unset() on object properties to work
     *
     * @param string $key
     * @return  void
     */
    public function __unset($key) {
        unset($this->_assigned[$key]);
    }

    /**
     * Assign variables to the template
     *
     * Allows setting a specific key to the specified value, OR passing
     * an array of key => value pairs to set en masse.
     *
     * @see  __set()
     * @param string|array $spec The assignment strategy to use (key or
     * array of key => value pairs)
     * @param mixed $value (Optional) If assigning a named variable,
     * use this as the value.
     * @return  void
     */
    public function assign($spec, $value = null) {
        if (is_array($spec)) {
            $this->_assigned = array_merge($this->_assigned, $spec);
        }

        $this->_assigned[$spec] = $value;
    }

    /**
     * Clear all assigned variables
     *
     * Clears all variables assigned to Zend_View either via
     * {@link  assign()} or property overloading
     * ({@link  __get()}/{@link  __set()}).
     *
     * @return  void
     */
    public function clearVars() {
        $this->_assigned = array();
    }

    /**
     * Processes a template and returns the output.
     *
     * @param string $name The template to process.
     * @return  string The output.
     */
    public function render($name, $valor = NULL) {
        $template = $this->_twig->loadTemplate($name);
        return $template->render($this->_assigned);
    }

    public function display($nombre, $valor = NULL) {
        $template = $this->_twig->loadTemplate($name);
        echo $template->render($this->_assigned);
    }

}

希望这些对你有帮助,欢迎多多交流。

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