模板语法

DigoodCMS-V6 模板主题语法使用最新的 Laravel7 Blade 编写开发,所以直接使用 blade模板语法即可,当然同样也支持原生的 php 语法,但并不建议使用,关于 blade 模板语法我们可以通过 《Laravel 中文文档 - Blade 模板》 学习

模板编写注意事项

唯一标识符

所有 blade 文件都必须要声明一个 唯一标识符,以便系统可以识别该文件,该识别符通过 DG::key(__FILE__) 生成即可,一般会在该文件的头部加入以下代码:

  1. {{-- 唯一标识符 BEGIN --}}
  2. <?php $DGK = DG::key(__FILE__); ?>
  3. {{-- 唯一标识符 END --}}

渲染主体 DG::init()

所有用于前端渲染所用的代码都应该被包含在 DG::class 类初始化完毕的条件内,例如下列代码所示:
注意:模块声明、字段声明、语言包声明、唯一标识符声明、需要在 DG::init() 条件外部进行,否则将不会生效

正确示例:

  1. {{-- 渲染主体 BEGIN --}}
  2. @if(DG::init())
  3. <?php $listData = ['这一条列表内容 -- 1', '这一条列表内容 -- 2']; ?>
  4. {{ var_dump($listData); }}
  5. <ul>
  6. <?php foreach($listData as $item): ?>
  7. <li>{{ $item }}<li>
  8. <?php endforeach; ?>
  9. </ul>
  10. @endif
  11. {{-- 渲染主体 END --}}

原因:变量声明和输出代码正确被包含在 DG::init() 条件内

错误示例:

  1. <?php $listData = ['这一条列表内容 -- 1', '这一条列表内容 -- 2']; ?>
  2. {{ var_dump($listData); }}
  3. {{-- 渲染主体 BEGIN --}}
  4. @if(DG::init())
  5. <ul>
  6. <?php foreach($listData as $item): ?>
  7. <li>{{ $item }}<li>
  8. <?php endforeach; ?>
  9. </ul>
  10. @endif
  11. {{-- 渲染主体 END --}}

原因:变量声明或输出代码没有正确包含在 DG::init() 条件内

模块声明

按照规范要求,我们需要将 templates 目录下所有模板按照区域分成一个个独立的模块存放于 includes 目录下,并且在该模板文件内声明拆分出来的模块,用于实现各个模块的开关和排序等功能。

例1:将首页模板 index.blade.php 拆分为 header、banner、hot-product、about、footer、然后声明

  1. {{-- 模块声明 BEGIN --}}
  2. <?php
  3. ## 页头
  4. Template::include(['name' => 'header', 'file' => 'includes.common.header', 'label' => '页面头部', 'option' => [], 'type' => 'common']);
  5. ## 中间部分
  6. Template::include(['name' => 'banner', 'file' => 'includes.index.banner', 'label' => '轮播图', 'option' => []]);
  7. Template::include(['name' => 'hot-product', 'file' => 'includes.index.feature', 'label' => '热门产品', 'option' => []]);
  8. Template::include(['name' => 'about', 'file' => 'includes.index.about', 'label' => '公司介绍', 'option' => []]);
  9. ## 页尾
  10. Template::include(['name' => 'footer', 'file' => 'includes.common.footer', 'label' => '页面尾部', 'option' => [], 'type' => 'common']);
  11. ?>
  12. {{-- 模块声明 END --}}

例2:将产品列表 product-list.blade.php 拆分为 header、breadcrumbs、product、footer、然后声明

  1. {{-- 模块声明 BEGIN --}}
  2. <?php
  3. ## 页头
  4. Template::include(['name' => 'header', 'file' => 'includes.common.header', 'label' => '页面头部', 'option' => [], 'type' => 'common']);
  5. ## 中间部分
  6. Template::include(['name' => 'breadcrumbs', 'file' => 'includes.common.breadcrumbs', 'label' => '面包屑导航', 'option' => []]);
  7. Template::include(['name' => 'list', 'file' => 'includes.category.product', 'label' => '产品列表', 'option' => []]);
  8. ## 页尾
  9. Template::include(['name' => 'footer', 'file' => 'includes.common.footer', 'label' => '页面尾部', 'option' => [], 'type' => 'common']);
  10. ?>
  11. {{-- 模块声明 END --}}

Template::include 参数

数组方式传入

参数名 参数类型 注释 示例
name string 唯一标识
file string 文件路径(基于主题根目录)
label string 模块别名
option array 配置项
type string 类型(common)