DedeCMS动态模板引擎

织梦教程 0 来源:老K模板网 收藏

核心文件​

include/dedetemplate.class.php

功能描述​

用于非核心模块的动态页面或列表页的模板解析, 如:member/content_list.php ,通常是在 datalistcp.class.php 中使用,这个类在动态运行的情况下,由于本身是把模板编译成PHP的,因此性能上会优级于旧的解析类,这个方法将在未来版本中作为通用的方式。

使用方法​

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>php
$tpl = new DedeTemplate(
 模板对象实例名称,通常是'tpl',
 [模板存放目录(生成缓存时会存放在这个目录),
 include语法默认引用目录]
);

通常情况下参数二和参数三是不必要的,如:

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>php
$tpl = new  DedeTemplate('tpl');

如果在类文件中调用,应该加上设置:

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>php
$this->tpl->SetObject($this);

在一些块调用中默认将使用当前类的成员函数。

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>php
$tpl->LoadTemplate(模板的物理路径);

如果模板中带有 {dede:config name='' value=''/} 可以在载入模板后,通过 $tpl->GetConfig($name) 获得这些变量的值。

显示页面或保存页面为文件

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>php
$tpl->Display();
<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>php
$tpl->SaveTo(/*物理绝对路径的文件名*/);

标记语法​

标记通用特性​

短标记​

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:tagname.name /}

等同于

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:tagname name='' /}

块标记​

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:tagname}
 循环代码...
{/dede:tagname}

标记的具体语法及对应的PHP代码​

配置变量​

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:config name='' value='' /}

配置变量可以在载入模板后通过 $tpl->GetConfig($name) 获得,仅作为配置,不在模板中显示。

短标记​

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:global.name /}

外部变量,等同于 <?php echo $name; ?>

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:var.name /}

var数组,等同于 <?php echo $_vars['name']; ?>

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:field.name /}

field数组,等同于 <?php echo $fields['name']; ?>

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:cfg.name/}

系统配置变量,等同于 <?php echo $cfg_name; ?>

考虑到大多数情况下都会在函数或类中调用模板,因此 $_vars 、$fields 数组必须声明为 global 数组,否则模板引擎无法获得它的值从而导致产生错误。

自由调用块标记​

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{tag:blockname bind='GetArcList'  bindtype='class'}
 循环代码
{/tag:blockname}
必要属性:​
  • bind - 数据源来源函数
  • bindtype - 函数类型,默认是 class 可选为 sub
  • rstype - 返回结果类型,默认是 array ,可选项为 string 。

自定义函数格式必须为 function(array $atts,object $refObj, array $fields); 在没有指定 bind 绑定的函数的情况下,默认指向 MakePublicTag($atts,$tpl->refObj,$fields) 统一管理。

固定块标记​

datalist​

从绑定类成员函数 GetArcList 中获取数组并输出

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:datalist}
 循环代码
{/dede:datalist}

遍历一个二维数组,数据源是固定的,只适用用类调用。等同于

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{tag:blockname bind='GetArcList' bindtype='class' rstype='arrayu'}
 循环代码
{/tag:blockname}
label​

从绑定函数中获取字符串值并输出。等同于

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{tag:blockname bind='func' bindtype='sub' rstype='string'/}
pagelist​

从绑定类成员函数 GetPageList 中获取字符串值并输出。等同于

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{tag:blockname bind='GetPageList' bindtype='class' rstype='string'/}
include​
<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:include file='文件路径'/}

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:include filename='文件路径'/}
php​
<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:php php代码... /}

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:php}
 php代码...
{/dede:php}
If​

仅支持 if ,else ,else 直接用 {else} 表示,但不支持 {else if} 这样的语法 ,一般建议模板中不要使用太复杂的条件语法,如果确实有需要,可以直接使用 php 语法。

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:if 条件}
 a-block
{else}
 b-block
{/dede:if}

条件中允许使用 var.name 、global.name 、 field.name 、cfg.name 表示相应的变量。

如:

<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:if field.id>10 }
 ......
{/dede:if}
遍历一个 array数组​
<button title="Copy Code" class="copy" style="touch-action: manipulation; border: 1px solid var(--vp-code-copy-code-border-color); padding: 0px; background-image: var(--vp-icon-copy); direction: ltr; position: absolute; top: 12px; right: 12px; z-index: 3; border-radius: 4px; width: 40px; height: 40px; opacity: 0; background-position: 50% center; background-size: 20px; background-repeat: no-repeat; transition: border-color 0.25s, background-color 0.25s, opacity 0.25s;"></button>html
{dede:array.name}
 {dede:key/} = {dede:value/}
{/dede:array}

各种语法的具体编译后的代码,可查看 dedetemplate.class.php 中的 function CompilerOneTag(&$cTag) 函数。


免责声明:1.凡本站注明“来源:XXX(非老K模板网)”的作品,均转载自其它媒体,所载的文章、图片、音频视频文件等资料的版权归版权所有人所有,如有侵权,请联系laokcms#126.com处理;2.凡本站转载内容仅代表作者本人观点,与老K模板网无关。
0 条评论

网友留言