当前位置:

常州PHP培训学校推荐

常州PHP培训课程推荐

常州培训新闻内容

[ 2017-07-27 09:50:43 ] 点击:562 常州PHP培训

技术分享:PHALCON中配置Smarty模板引擎






Phalcon是PHP的一种全栈框架,从运行性能、设计思想上,非常先进。
 
Phalcon内置了view的模板引擎:phtml 和volt。Phalcon当然也可以使用我们习惯的smarty模板引擎。
 
这里以 smarty3.x 为例,详细介绍 phalcon 接入smarty的过程. smarty2.x的方式也是一样。
 
1、下载好 smarty3.x,并确认路径已经添加到  php.ini,确认以下代码可以正常运行
<?php
include_once("Smarty.class.php");  //使用smarty3 引擎
?>
 
2、确认phalcon的libs库路径添加到 php.ini,确认以下代码可以正常运行
<?php
include_once("Phalcon/Mvc/View/Engine/Smarty.php");
?>
 
3、编辑 app/config/config.php,补充以下代码
<?php
return new \Phalcon\Config(array(
    .....  //其它配置参数
         'smarty' => array(
                   'compileDir' => __DIR__ . '/../../app/cache/smarty/compile',
                   'configDir'  => __DIR__ . '/../../app/cache/smarty/config',
                   'pluginsDir' => __DIR__ . '/../../app/cache/smarty/plugins',
                   'cacheDir'   => __DIR__ . '/../../app/cache/smarty/cache',
                   'leftDelimiter'   => '{.',
                   'rightDelimiter'   => '.}',
                   'debugging'   => true,
         ),
 
));
?>
此处smarty的界定符自定义修改为 {.  xxxxx  .},以避免html中的javascript,css等冲突。
 
4、在项目中创建对应的目录,(linux需设定目录的写权限)
/project
  /app
    /cache
      /smarty
        /compile    #smarty模板编译目录
        /config     #smarty配置目录
        /plugins    #smarty自定义插件目录
        /cache     #smarty缓存文件目录
 
 
5、配置services注入服务di
 
$di->set('view', function() use ($config) {
 
         $view = new View();
 
         $view->setViewsDir($config->application->viewsDir);
 
         $view->registerEngines(array(
                   '.volt' => function($view, $di) use ($config) {
 
                            $volt = new VoltEngine($view, $di);
 
                            $volt->setOptions(array(
                                     'compiledPath' => $config->application->cacheDir,
                                     'compiledSeparator' => '_',
                            ));
                            return $volt;
                   },
                   '.phtml' => 'Phalcon\Mvc\View\Engine\Php', // Generate Template files uses PHP itself as the template engine
 
                   '.tpl' => function($view, $di) use ($config) {
 
                            $smarty = new SmartyEngine($view, $di);
 
                            $smarty->setOptions([
                                     'template_dir'           => $view->getViewsDir(),
                                     'compile_dir'             => $config->smarty->compileDir,
                                     'error_reporting'      => error_reporting() ^ E_NOTICE,
                                     'escape_html'           => true,
                                     '_file_perms'             => 0666,
                                     '_dir_perms'              => 0777,
                                     'force_compile'                  => false,
                                     'compile_check'                 => true,
                                     'caching'                     => false,
                                     'debugging'                         => $config->smarty->debugging,
                                     'config_dir'                          => $config->smarty->configDir,
                                     'plugins_dir'                        => $config->smarty->pluginsDir,
                                     'cache_dir'                              => $config->smarty->cacheDir,
                                     'left_delimiter'                            => $config->smarty->leftDelimiter,
                                     'right_delimiter'                          => $config->smarty->rightDelimiter,
                            ]);
 
                            return $smarty;
                   }
         ));
 
         return $view;
}, true);
 
 
phalcon支持多模板引擎的特性,所以以上配置可以使用phtml,volt,smarty混用模板。phalcon根据模板的后缀名自动选择引擎。开发人员只需要专注于业务逻辑,无需考虑系统框架的问题。

匿名评论

学途最新培训新闻