模板语法
DigoodCMS-V6 模板主题语法使用最新的 Laravel7 Blade 编写开发,所以直接使用 blade模板语法即可,当然同样也支持原生的 php 语法,但并不建议使用,关于 blade 模板语法我们可以通过 《Laravel 中文文档 - Blade 模板》 学习
模板编写注意事项
唯一标识符
所有 blade 文件都必须要声明一个 唯一标识符,以便系统可以识别该文件,该识别符通过 DG::key(__FILE__) 生成即可,一般会在该文件的头部加入以下代码:
{{-- 唯一标识符 BEGIN --}}<?php $DGK = DG::key(__FILE__); ?>{{-- 唯一标识符 END --}}
渲染主体 DG::init()
所有用于前端渲染所用的代码都应该被包含在 DG::class 类初始化完毕的条件内,例如下列代码所示:
注意:模块声明、字段声明、语言包声明、唯一标识符声明、需要在 DG::init() 条件外部进行,否则将不会生效
正确示例:
{{-- 渲染主体 BEGIN --}}@if(DG::init())<?php $listData = ['这一条列表内容 -- 1', '这一条列表内容 -- 2']; ?>{{ var_dump($listData); }}<ul><?php foreach($listData as $item): ?><li>{{ $item }}<li><?php endforeach; ?></ul>@endif{{-- 渲染主体 END --}}
原因:变量声明和输出代码正确被包含在 DG::init() 条件内
错误示例:
<?php $listData = ['这一条列表内容 -- 1', '这一条列表内容 -- 2']; ?>{{ var_dump($listData); }}{{-- 渲染主体 BEGIN --}}@if(DG::init())<ul><?php foreach($listData as $item): ?><li>{{ $item }}<li><?php endforeach; ?></ul>@endif{{-- 渲染主体 END --}}
原因:变量声明或输出代码没有正确包含在 DG::init() 条件内
模块声明
按照规范要求,我们需要将 templates 目录下所有模板按照区域分成一个个独立的模块存放于 includes 目录下,并且在该模板文件内声明拆分出来的模块,用于实现各个模块的开关和排序等功能。
例1:将首页模板 index.blade.php 拆分为 header、banner、hot-product、about、footer、然后声明
{{-- 模块声明 BEGIN --}}<?php## 页头Template::include(['name' => 'header', 'file' => 'includes.common.header', 'label' => '页面头部', 'option' => [], 'type' => 'common']);## 中间部分Template::include(['name' => 'banner', 'file' => 'includes.index.banner', 'label' => '轮播图', 'option' => []]);Template::include(['name' => 'hot-product', 'file' => 'includes.index.feature', 'label' => '热门产品', 'option' => []]);Template::include(['name' => 'about', 'file' => 'includes.index.about', 'label' => '公司介绍', 'option' => []]);## 页尾Template::include(['name' => 'footer', 'file' => 'includes.common.footer', 'label' => '页面尾部', 'option' => [], 'type' => 'common']);?>{{-- 模块声明 END --}}
例2:将产品列表 product-list.blade.php 拆分为 header、breadcrumbs、product、footer、然后声明
{{-- 模块声明 BEGIN --}}<?php## 页头Template::include(['name' => 'header', 'file' => 'includes.common.header', 'label' => '页面头部', 'option' => [], 'type' => 'common']);## 中间部分Template::include(['name' => 'breadcrumbs', 'file' => 'includes.common.breadcrumbs', 'label' => '面包屑导航', 'option' => []]);Template::include(['name' => 'list', 'file' => 'includes.category.product', 'label' => '产品列表', 'option' => []]);## 页尾Template::include(['name' => 'footer', 'file' => 'includes.common.footer', 'label' => '页面尾部', 'option' => [], 'type' => 'common']);?>{{-- 模块声明 END --}}
Template::include 参数
数组方式传入
| 参数名 | 参数类型 | 注释 | 示例 |
|---|---|---|---|
| name | string | 唯一标识 | |
| file | string | 文件路径(基于主题根目录) | |
| label | string | 模块别名 | |
| option | array | 配置项 | |
| type | string | 类型(common) |
