zoukankan      html  css  js  c++  java
  • yaf twig配置

    1.安装 TWIG
    composer require twig/twig
    2.COMPOSER自动加载的引用
    修改 BOOTSTRAP.PHP 增加
    public function _initAutoload() {
    require __DIR__ . "/../vendor/autoload.php";

    }
    3.新建ADAPTER
    任意命名 需要实现Yaf_View_Interface接口

    class Template_Adapter implements Yaf_View_Interface
    {


    private $vars;
    private $path_view;

    private $twig;
    private $loader;
    private $extraParams;

    public function __construct($tmplPath = null, $extraParams = array())
    {
    if ($tmplPath) {
    $this->path_view = $tmplPath;
    }
    $this->extraParams=$extraParams;

    }

    private function f()
    {
    if (!$this->loader) {
    $this->loader = new Twig_Loader_Filesystem($this->path_view);
    $this->twig = new Twig_Environment( $this->loader , $this->extraParams);
    }



    }

    public function assign($spec, $value = null)
    {

    if (is_array($spec)) {
    $this->vars = $spec;
    return;
    }

    $this->vars[$spec] = $value;

    }

    public function display($tpl, $tpl_vars = null)
    {
    if (!is_null($tpl_vars) && is_array($tpl_vars)) {
    $this->vars = $tpl_vars;
    }
    $this->f();
    echo $this->twig->render($tpl,$this->vars);


    }


    public function render($tpl, $tpl_vars = null)
    {
    $this->display($tpl, $tpl_vars);
    }


    public function setScriptPath($template_dir)
    {
    if ($template_dir) {
    $this->path_view = $template_dir;
    }

    }

    public function getScriptPath()
    {
    return $this->path_view;
    }


    }


    4.BOOTSTARP中增加TWIG引用



    public function _initView(Yaf_Dispatcher $dispatcher){

    $dispatcher->autoRender(false);


    $ve= new Template_Adapter(APPLICATION_PATH.'/view/',
    [
    'path_cache' => APPLICATION_PATH.'/viewcache/'
    ]);*/


    require 'Template_Adapter.php';
    $ve= new Template_Adapter(APPLICATION_PATH.'/view/',[
    'debug' => false,
    'charset' => 'UTF-8',
    'base_template_class' => 'Twig_Template',
    'strict_variables' => false,
    'autoescape' => 'html',
    'cache' => APPLICATION_PATH.'/viewcache/',
    'auto_reload' => null,
    'optimizations' => -1]);
    Yaf_Dispatcher::getInstance()->setView($ve);
    }

    5.控制器中调用
    $this->getView()->display('a.html');
  • 相关阅读:
    BugReport-仿微信app
    成长、责任、和公司的关系
    团队如何做决定
    课堂练习
    课堂练习
    NABCD model
    课堂练习
    日程管理测试用例
    测试
    Bug报告
  • 原文地址:https://www.cnblogs.com/tttlan/p/6663761.html
Copyright © 2011-2022 走看看