最简单的php模板引擎----raintpl

官网: https://github.com/feulf/rain...

特点

  1. 对于前端设计师来说,比较简单,只有10个tag, {$variable}, {#constant#}, {include}, {loop}, {if}, { comment }, {noparse}, {function}
  2. 对于后台程序员来说,也很简单,只有5个方法*用来加载和渲染模板

====针对前端设计师====


    <!DOCTYPE html>
    <html>
    <head>
    <title>{$title}</title>
    </head>
    <body>
        {$content}
    </body>
    </html>

变量的写法{$变量名},模板文件存在各自对应的主题文件夹中

tags

  • {$variable}
  • {#constant#}
  • {include="template"}
  • {if="expression"}
  • {loop="$array"}
  • {function="function name"}
  • { comment } or {ignore}
  • {noparse}
  • {autoescape="off"}

01:变量{$variable}

{$name}, you are very good!!  //直接使用变量
{$array.key}                  //数组的键
{$object->element}            //对象的元素
{$a=1}
{$b=2}
{$sum=$a+$b}
{$title|cut:10} // Hello Wor...
{$title|touppercase|cut:7} // HELLO W...

02:常量{#constant#}

{#URL#}               //输出www.sina.com.cn

03:导入其它模板{include="template"}

    <html>
    <body>
    {include="header"}
    here goes the content of the website
    {include="footer"}
    </body>
    
    
    {include="$page"}  //可以动态导入模板,记住变量$page是放在双引号中,另外要记住的是有一个等号
    
    // 不用等号
    {include $control->loadModel('ui')->getEffectViewFile('default', 'common', 'header')}

04:条件{if="expression"}

    {if="$age<18"}
         小孩
    {else}
         成年人
    {/if}
    
    
    {if(!defined("IN_CMS"))}
        {die()}
    {/if}
    

05:循环{loop="$var"}

    <ul>
    {loop="$weekday"}
        <li>{$key}, {$value}</li>
    {/loop}
    </ul>
    
    //输出
    <ul>
        <li>0, Sunday</li>
        <li>1, Monday</li>
        ...
        <li>6, Saturday</li>
    </ul>
    
    //在{loop}和{/loop}之间可用的变量和语句
    {$key}
    {$value}
    {$counter}
    {break}
    {continue}

06:函数{function="function name"}

这个会执行php函数,并打印出结果,它也可以传入字符串,数字和变量作为参数
{function="date('%Y')"}

//结果
2018

07:注释{* comment *} 或 {ignore}

在{*和*}之间的内容会忽略,也可以用 {ignore}和 {/ignore}:
你好, {* {$name} ,怎么了? *} 昨晚没睡好?

    //输出
    你好,昨晚没睡好?

    //使用ignore
    this is a test
    {ignore}
    the {$var} inside the ignore
    {/ignore}
    not to much important.
    //输出
    this is a test
    not to much important.

08:原样输出不编译{noparse}

{noparse}this is the {$title}{/noparse}
//结果输出
this is the {$title}

09:{autoescape="off"}

{autoescape="off"}{$safe_variable}{/autoescape}
<script>console.log('this is safe')</script>
&lt;script&gt;console.log('this is safe')&lt;/script&gt;

====针对PHP程序员====


使用composer或直接导入raintpl类文件

第一步:配置Tpl::configure( $config )

$tplDir = "tpl/";
$cacheDir = "tmp/";
$baseUrl = null;
$tplExt = "html";
$pathReplace = true;
$pathReplaceList = array();
// $blackList = array('\$this', 'raintpl::', 'self::', '_SESSION', '_SERVER', '_ENV', 'exec', 'unlink', 'rmdir');
$blackList = array();
$outputFunctions = ',a,var_dump,var_export,printf,print,print_r,';
$echoFunctions = array('html', '');
$checkTemplateUpdate = true;
$phpEnabled = false;
$debug = false;
$rootDir = '';

批量配置

$config = array(
    "tpl_dir"=> "vendor/rain/raintpl/templates/test/",
    "cache_dir"=> "vendor/rain/raintpl/cache/"
);
Tpl::configure( $config );

单个配置

Tpl::configure("tpl_dir", "vendor/rain/raintpl/templates/test/");
Tpl::configure("cache_dir", "vendor/rain/raintpl/cache/");

第二步:初始化new、赋值assign、渲染draw

    $t = new Tpl;
    $t->assign('title','Hello!');
    $t->draw('test');

====自定义插件====

参考:https://github.com/feulf/rain...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值