简介

如何注册字段

可以在主题 ./templates 下的任何模板文件内注册,也可以在主题 ./includes 下被模板引入的任何模块文件内注册,模块内注册的字段将成为该模板文件的所属字段。
模板字段注册后,在被使用时由程序自动添加进所属的数据库模型表内。
注意:需要注册的字段不应该放在 DG::init() 条件内部,否则程序无法创建需要注册的字段

支持的控件类型

控件名称 类型值 备注
文本字段 text 可设置多语言文本,默认启用多语言
编辑器 editor 可设置为 basic 和 full 两种类型
图片字段 image 可拓展为多图上传,默认为单图上传
文件字段 file 可拓展为多文件上传,默认为单文件上传
视频字段 video 可拓展为多视频上传,默认为单视频上传
开关字段 switch
单选字段 radio
多选字段 checkbox
组字段 group

字段注册方法

Template::field(Array $config)

$config 参数基本属性

  1. $config = [
  2. 'name' => String, // 字段,例:custom_site_main_logo
  3. 'label' => String, // 字段标识,例:网站主LOGO
  4. 'type' => String, // 字段控件类型,例:image
  5. 'option' => Array
  6. ];

字段注册示例

  1. {{-- 注册字段 BEGIN --}}
  2. <?php
  3. Template::field(['name' => 'index_main_logo', 'type' => 'image', 'label' => '网站主LOGO', 'option' => []]);
  4. Template::field(['name' => 'index_about_image', 'type' => 'image', 'label' => '首页公司简介图片', 'option' => []]);
  5. Template::field(['name' => 'index_about_content', 'type' => 'editor', 'label' => '首页公司简介内容', 'option' => [ 'type' => 'basic' ]]);
  6. Template::field(['name' => 'index_slogan', 'type' => 'text', 'label' => '首页标语', 'option' => []]);
  7. Template::field(['name' => 'index_banner_items', 'type' => 'group', 'label' => '首页Banner图', 'option' => [
  8. 'limit' => 3,
  9. 'fields' => [
  10. [ 'name' => 'pc_image', 'type' => 'image', 'label' => 'PC端 Banner图'],
  11. [ 'name' => 'm_image', 'type' => 'image', 'label' => '移动端 Banner图'],
  12. [ 'name' => 'url', 'type' => 'text', 'label' => 'Banner链接'],
  13. ]
  14. ]]);
  15. ?>
  16. {{-- 注册字段 END --}}

文本字段配置

  1. [
  2. 'name' => 'custom_text',
  3. 'type' => 'text',
  4. 'label' => '自定义文本',
  5. 'option' => []
  6. ]

编辑器字段配置

  1. // 基础样式编辑器
  2. [
  3. 'name' => 'custom_description',
  4. 'type' => 'editor',
  5. 'label' => '自定义描述',
  6. 'option' => [
  7. 'type' => 'basic'
  8. ]
  9. ]
  10. // 完整样式编辑器
  11. [
  12. 'name' => 'custom_content',
  13. 'type' => 'editor',
  14. 'label' => '自定义内容',
  15. 'option' => [
  16. 'type' => 'full'
  17. ]
  18. ]

图片字段配置

  1. // 单图
  2. [
  3. 'name' => 'custom_image',
  4. 'type' => 'image',
  5. 'label' => '自定义单图',
  6. 'option' => []
  7. ]
  8. // 多图
  9. [
  10. 'name' => 'custom_images',
  11. 'type' => 'image',
  12. 'label' => '自定义多图',
  13. 'option' => [
  14. 'limit' => 10, // 限制数量
  15. 'type' => 'multiple' // 启用多个
  16. ]
  17. ]

视频字段

  1. // 单视频
  2. [
  3. 'name' => 'custom_video',
  4. 'type' => 'video',
  5. 'label' => '单视频上传',
  6. 'option' => []
  7. ]
  8. // 多视频
  9. [
  10. 'name' => 'custom_videos',
  11. 'type' => 'video',
  12. 'label' => '多视频上传',
  13. 'option' => [
  14. 'limit' => 10, // 限制数量
  15. 'type' => 'multiple' // 启用多个
  16. ]
  17. ]

文件字段

  1. // 单文件
  2. [
  3. 'name' => 'custom_file',
  4. 'type' => 'file',
  5. 'label' => '单文件上传',
  6. 'option' => []
  7. ]
  8. // 多文件
  9. [
  10. 'name' => 'custom_files',
  11. 'type' => 'file',
  12. 'label' => '多文件上传',
  13. 'option' => [
  14. 'limit' => 10, // 限制数量
  15. 'type' => 'multiple' // 启用多个
  16. ]
  17. ]

组字段

  1. [
  2. 'name' => 'custom_banner',
  3. 'type' => 'group',
  4. 'label' => '自定义Banner',
  5. 'option' => [
  6. 'limit' => 3,
  7. 'fields' => [
  8. [ 'name' => 'image_pc', 'type' => 'image', 'label' => 'PC端Banner'],
  9. [ 'name' => 'image_mobile', 'type' => 'image', 'label' => '手机端Banner'],
  10. [ 'name' => 'text', 'type' => 'text', 'label' => 'Banner文本'],
  11. ]
  12. ]
  13. ]

开关字段

单选框

多选框