AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
要使用布局中的继承功能,需要用到四个特殊的标签 ~~~ {%extend@tpl_name%} :声明本模板继承自tpl_name模板,本标签不支持多层嵌套 {%block@block_name%} :父模板中用此标签定义一个占位符,可以被第一层子模板中定义的对应的block_name中的块状内容替换 {%block@block_name%}与{%end%}:第一层子模板中定义的块状内容,用于替换父模板中相应block_name的占位标签 {%contend@tpl_name%} :声明本模板内容会被全部复制到tpl_name对应的模板,本标签支持多层模板嵌套 {%contend%}:父模板中一个给子模板全部内容替换的占位符 ~~~ 如果觉得太绕,直接看实例就明白了 1. 模板main.php,是一个最基本的布局 ~~~ <!DOCTYPE html> <html> <head> <meta charset="utf-8"> {%block@title%} </head> <body> <header> <h1>头部</h1> </header> {%content%} <footer class="footer"> <div>页脚</div> </footer> {%block@java%} </body> </html> ~~~ 2. 模板blog.php,是一个中间层布局 ~~~ {%content@main%} <div id="content"> <div id="siderbar"> $this->inser('sidebar'); </div> <div id="post"> {%block@post%} </div> </div> ~~~ 3. 模板siderbar.php,是一个用于插入其他模板的公用siderbar模板 ~~~ <div class="wiget"> <h3>最新文章</h3> <ul> <li>xxxx</li> <li>xxxx</li> <li>xxxx</li> </ul> </div> ~~~ 4. 模板post.php,最终实现页 ~~~ {%extend@blog%} {%block@title%} <title><?=$site_title?></title> <meta name="keywords" content="<?=$site_keywords?>"> <meta name="description" content="<?=$site_description?>"> {%end%} {%block@post%} <h2><?=$title?></h2> <div class="post-content"> <?=$content?> </div> {%end%} {%block@java%} <script type="text/javascript"> //写你的JS代码 </script> {%end%} ~~~ 准备好上面的模板后,现在我们只要用fetch()方法渲染最终实现的post.php $this->fetch('post'); 就可以得到最后的合并编译结果,大概如下面这样: ~~~ <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title><?=$site_title?></title> <meta name="keywords" content="<?=$site_keywords?>"> <meta name="description" content="<?=$site_description?>"> </head> <body> <header> <h1>头部</h1> </header> <div id="content"> <div id="siderbar"> <div class="wiget"> <h3>最新文章</h3> <ul> <li>xxxx</li> <li>xxxx</li> <li>xxxx</li> </ul> </div> </div> <div id="post"> <h2><?=$title?></h2> <div class="post-content"> <?=$content?> </div> </div> </div> <footer class="footer"> <div>页脚</div> </footer> <script type="text/javascript"> //写你的JS代码 </script> </body> </html> ~~~