### 使用原生PHP作为模板语言
>[info]推荐:[流程控制的替代语法](http://php.net/manual/zh/control-structures.alternative-syntax.php)
Smarty 的 if
~~~
{if $name == 'vip'}
你好Vip
{elseif $name == 'normal'}
你好普通用户
{else}
你好游客
{/if}
~~~
PHP 的 if
~~~
你好Vip
你好普通用户
你好游客
~~~
PHP遍历`$_SERVER`
~~~
<?php foreach ($_SERVER as $key => $value) : ?>
= $key ?> 的值为:=$value?>
~~~
这儿的演示只是我个人的写法,大家有别的方法可以随意
我们可以在公共(模块)控制器中定义一个存放模板目录路径的常量或是变量(也可以在配置文件里设置,通过`cofing()`函数调用),比如:
~~~
/**
* 前台公共控制器,继承控制器基类
*/
namespace app\home\controller;
class Common
{
//模板目录
public $tpl_dir;
public function __construct()
{
$this->tpl_dir = APP_PATH . 'home/view/default/';
}
}
~~~
>[info]提示:在模板开发时需要指定模板位置时,需要灵活运用几个内置的框架路径常量与URL常量,具体请参考[内置常量](http://www.kancloud.cn/yunfei_z/framework/201721)
接着在子控制器中继承公共控制器,直接需要调用模板文件的地方include就行
~~~
/**
* 首页控制器,继承公共控制器类
*/
namespace app\home\controller;
class Index extends Common
{
public function index()
{
//定义一些常量
include $this->tpl_dir . 'index/index.php';
}
}
~~~