PHP code example of hehex / hehep-htemplate

1. Go to this page and download the library: Download hehex/hehep-htemplate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hehex / hehep-htemplate example snippets



$conf = [
    // 上下文对象
    'context'=>[],//phpweb
    'filters'=>[
        'h'=>'hehe'
    ],
    // 模板文件根路径
    'tplPath'=>'@app@/view/',
    // 系统标签,默认自动加载sys 表达式,默认标签,书写时无需写入前缀,比如<css href="xxx" />
    'sysTags'=>['sys'],
    // 自定义标签,书写时必须写入前缀,比如 标签名称html,则css 标签的书写规则为: <html:css href="xxx" />
    'customTags'=>['html'],
    // 模板文件扩展名
    'suffix' => 'html',
    // 缓存文件扩展名
    'cacheSuffix' => 'php',
    // 是否开启模板缓存,开启后,会自动缓存代码,加快模板解析的速度
    'onCache' => true,
    // 模板缓存文件目录,模板编译的python代码缓存在文件中,缓存文件存储此目录中
    'cachePath' => '/home/hehe/www/cache/htpl/',
    // 资源地址,比如js,css,img,默认提供static(静态资源路径),res(外部资源,比如上传的文件) 字典key
    'urls'=>[
        'static'=>'http://eduhome.xuewei.cn/'
    ],
    // 模板缓存有效期,单位秒,0 表示无有效期
    'timeout' => 5,
    // 表达式起始符
    'expStart' => '{{',
    // 表达式结束符
    'expEnd' => '}}',
    // 结束表达式的结束符比如/ 则完整表达式为{/for} 或end,{endfor}
    'expEndEof' => '/',
    // 是否启用标签规则,开启后,匹配表达式 <import name="eduhome.name.yong" />
    'onTag' => true,
    // 标签起始符
    'tagStart' => '<',
    // 标签结束符
    'tagEnd' => '>',
    // 结束标签结束符号
    'tagEndEof' => '/',
];


use htemplate\TemplateManager;

$config = [];
$htemplate = new TemplateManager($config);
// 加载(user/add)模板,返回模板内容
$data = [
    'msg'=>'hello world'
];
$html = $htemplate->fetch("user/index",$data);


namespace admin\htemplate\filters;

class HeheFilter
{
    public static function date_filter($format,$datetime)
    {
        return date($format,strtotime($datetime));
    }
}


$config = [
    'filters'=>[
        // ‘过滤器方法前缀’=>'过滤器类路径'
        'he_'=>'\admin\htemplate\filters\HeheFilter'
   ];

// 方式一
$htemplate = new htemplate\TemplateManager($config);

// 方式二
$htemplate = new htemplate\TemplateManager();
$htemplate->addFilters(['he_'=>'\admin\htemplate\filters\HeheFilter']);


namespace admin\htemplate\contexts;

class PhpwebContext
{
    public function handle()
    {
        return [
            '_request'=>&$_REQUEST,
            '_get'=>&$_GET,
            '_post'=>&$_POST,
            '_server'=>&$_SERVER,
            '_cookie'=>&$_COOKIE,
            '_session'=>&$_SESSION,
            '_files'=>&$_FILES,
        ];
    }
}


$config = [
    // 上下文
    'context'=>['admin\htemplate\contexts\PhpwebContext'],
];

// 方式1
$htemplate = new htemplate\TemplateManager($config);

// 方式2
$htemplate->addContext('admin\htemplate\contexts\PhpwebContext');

namespace admin\htemplate\tags;

use htemplate\base\BaseTag;
use htemplate\base\TagExpression;
use htemplate\nodes\BaseNode;

class HtmlTag extends BaseTag
{
    // 定义标签
    protected $tags = [
        // 表单select
        ['name'=>"select", 'close'=>false,'onTag'=>true],
        // 不解析内容标签
        ['name'=>"pass", 'close'=>true,'onTag'=>true],# 标签库
    ];
    
    // 非闭合标签
    public function select_tag(TagExpression $tagExp,$node_attrs,BaseNode $mainNode)
    {
        $attrs = $this->buildTagAttrs($node_attrs);

        list($html_attrs,$he_attrs) = $this->spiltAttrs($attrs);

        $he_name = $he_attrs['he-name'];
        $he_text = isset($he_attrs['he-text']) ? $he_attrs['he-text'] : '';
        $he_selected = isset($he_attrs['he-selected']) ? $he_attrs['he-selected'] : '';
        if (empty($he_selected)) {
            $he_ctx = '$_request';
            if (!empty($he_attrs['he-ctx'])) {
                $he_ctx = $he_ctx['he-ctx'];
            }

            $var_name = $he_ctx . '.' . $html_attrs['name'];
            $he_selected = $this->buildVarName($var_name);
        }

        $html_attrs = $this->attrHtml($html_attrs);

        $html = '<select {attrs}>
               <option value="">{text}</option>
                foreach({options} as $_key=>$_name):

$config = [
    // 上下文
    'customTags'=>['html'=>'\admin\htemplate\tags\HtmlTag'],
];

// 方式1
$htemplate = new htemplate\TemplateManager($config);

// 方式2
$htemplate->addCustomTags(['html'=>'\admin\htemplate\tags\HtmlTag']);

// 标签作为系统标签
$htemplate->addSysTags(['\admin\htemplate\tags\HtmlTag']);


在模板页面里默认可以使用的数据,比如$_REQUEST,$_SESSION,$_COOKIES,等等,
模板加载时,会自动注入对应的上下文数据至模板页面
目前只支持phpweb(传统的web php),后续可扩展swoole 上下文,或workerman上下文
html

{{php}} $name=2;{{/php}}

html
<!--输出$dictlist 数组 键值为1 的值,如果键值不存在,则输出"无"-->
<dict name="$dictlist" keys="1" glue="," defualt="无"/>

<dict name="$dictlist" keys="1,5" glue="," defualt="无"/>

html
<h1>
{{:he_xss($name)}}
</h1>
html
<h1>
{{:he_safe($name)}}
</h1>
html
<h1>
{{:he_dict($dicts,$dict_key)}}
{{:he_dict($dicts,$dict_key)}}
</h1>