联系电话:020-28187900

引用和碎片模板标签使模板制作重复工作减少

发表时间:2015-01-12 阅读: 9199

K9模板标签本身已经支持引用标签。
碎片应用也有类似的标签,两者都是使用{#include file=""}进行引入文件。

K9引用标签

{#include file='include/top.php'}
  • file是文件路径,其值为相对于当前被应用的模板目录路径。
  • 被引用的文件也会被正常解析标签。

引用标签同时可以用 require 别名,效果同上:

{#require file='include/top.php'}

碎片应用标签

碎片应用作为模板制作的一项辅助功能,用于管理网站模板重用的代码。
这样做的好处十分明显,一方面减少重复编码的工作量,另一方面便于批量修改重用部分的代码。
碎片的内容支持纯HTML,纯PHP,也支持K9模板标签代码。

{#include file="block/bottom.php"}

碎片名称必须为以下字符构成:

  • 小写英文
  • 数字
  • 下划线

引用标签和碎片标签的区别

虽然两者都是使用{#include file=""}进行引入文件。
但碎片应用的标签采用双引号包含文件,并且支持在线编辑和增加备注,如下图所示:

碎片应用标签列表

引用标签应用举例

Example1是一个标准的K9模板框架:

Example1.php

<?php !defined('INC') && exit('Load failure!'); ?>
<!-- HTML模板代码开始 -->
<html>
<head>
<title>
<!-- 页面标题 -->
</title>
</head>
<body>
<div id="top">
<!-- 这里可以输出头部信息,如Logo等 -->
</div>
<div id="main">
<!-- 主体部分 -->
</div>
<div id="bottom">
<!-- 这里可以输出页面底部信息 -->
</div>
</body>
</html>
<!-- HTMl模板代码结束 -->

我们在该模板的目录新建文件夹命名为:include。

再在include文件夹新建两个php文件:

top.php

<?php !defined('INC') && exit('Load failure!'); ?>
<div id="top">
<!-- 这里可以输出头部信息,如Logo等 -->
</div>

bottom.php

<?php !defined('INC') && exit('Load failure!'); ?>
<div id="bottom">
<!-- 这里可以输出页面底部信息 -->
</div>

修改Example1.php的代码,称为Example2.php。

Example2.php

<?php !defined('INC') && exit('Load failure!'); ?>
<!-- HTML模板代码开始 -->
<html>
<head>
<title>
<!-- 页面标题 -->
</title>
</head>
<body>
{#include file='include/top.php'}
<div id="main">
<!-- 主体部分 -->
</div>
{#include file='include/bottom.php'}
</body>
</html>
<!-- HTMl模板代码结束 -->

Example2.php使用引用标签,实现和Example1.php完全一样的代码。

其中,{#include file='include/top.php'}可以使用以下代替:

  • {#require file='include/top.php'}
  • {#include file="block/top.php"}

有兴趣的同学可以自行实践。

 

转载请注明出处:唯众网络


相关教程 【更多】

唯众网络