ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
安装:(需要composer,可查看我的笔记了解使用) `composer require twig/twig:~1.0 ` 配置: ~~~ twig_start.php $twigpath='/template';//模版路径,自己定义 $loader = new Twig_Loader_Filesystem($twigpath); $twig = new Twig_Environment($loader, array( 'cache' => $twigpath.'/compiled',//缓存php文件保存地址 'debug'=>true,//开启debug随时更新缓存文件 )); ~~~ 拍页面 ~~~ $rander = ['order'=>$order,'team'=>$team]; echo $twig->render('order_info.html', $rander); //这样就可以在模版order_info.html页面中通过{{order}}使用$order了 ~~~ #问题 ####在OPcache或APC启用的情况下,刷新已修改的模板 在使用opcache.validate_timestamps 设为 0的OPcache,或apc.stat设为0的APC,并且Twig 缓存被启用的情况下,清除模板缓存并不会更新缓存。 为了解决这个问题,需要强行让Twig将字节码缓存无效化,调试阶段将debug开启可避免此问题: ~~~ $twig = new Twig_Environment($loader, array( 'cache' => new Twig_Cache_Filesystem('/some/cache/path', Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION), // ... )); ~~~ ####模版可以被继承,被引用,这块儿自己看文档处理吧。本文只是基础的使用说明。