简介
如何注册字段
可以在主题 ./templates
下的任何模板文件内注册,也可以在主题 ./includes
下被模板引入的任何模块文件内注册,模块内注册的字段将成为该模板文件的所属字段。
模板字段注册后,在被使用时由程序自动添加进所属的数据库模型表内。
注意:需要注册的字段不应该放在 DG::init()
条件内部,否则程序无法创建需要注册的字段
支持的控件类型
控件名称 | 类型值 | 备注 | |
---|---|---|---|
文本字段 | text | 可设置多语言文本,默认启用多语言 | |
编辑器 | editor | 可设置为 basic 和 full 两种类型 | |
图片字段 | image | 可拓展为多图上传,默认为单图上传 | |
文件字段 | file | 可拓展为多文件上传,默认为单文件上传 | |
视频字段 | video | 可拓展为多视频上传,默认为单视频上传 | |
开关字段 | switch | ||
单选字段 | radio | ||
多选字段 | checkbox | ||
组字段 | group |
字段注册方法
Template::field(Array $config)
$config
参数基本属性
$config = [
'name' => String, // 字段,例:custom_site_main_logo
'label' => String, // 字段标识,例:网站主LOGO
'type' => String, // 字段控件类型,例:image
'option' => Array
];
字段注册示例
{{-- 注册字段 BEGIN --}}
<?php
Template::field(['name' => 'index_main_logo', 'type' => 'image', 'label' => '网站主LOGO', 'option' => []]);
Template::field(['name' => 'index_about_image', 'type' => 'image', 'label' => '首页公司简介图片', 'option' => []]);
Template::field(['name' => 'index_about_content', 'type' => 'editor', 'label' => '首页公司简介内容', 'option' => [ 'type' => 'basic' ]]);
Template::field(['name' => 'index_slogan', 'type' => 'text', 'label' => '首页标语', 'option' => []]);
Template::field(['name' => 'index_banner_items', 'type' => 'group', 'label' => '首页Banner图', 'option' => [
'limit' => 3,
'fields' => [
[ 'name' => 'pc_image', 'type' => 'image', 'label' => 'PC端 Banner图'],
[ 'name' => 'm_image', 'type' => 'image', 'label' => '移动端 Banner图'],
[ 'name' => 'url', 'type' => 'text', 'label' => 'Banner链接'],
]
]]);
?>
{{-- 注册字段 END --}}
文本字段配置
[
'name' => 'custom_text',
'type' => 'text',
'label' => '自定义文本',
'option' => []
]
编辑器字段配置
// 基础样式编辑器
[
'name' => 'custom_description',
'type' => 'editor',
'label' => '自定义描述',
'option' => [
'type' => 'basic'
]
]
// 完整样式编辑器
[
'name' => 'custom_content',
'type' => 'editor',
'label' => '自定义内容',
'option' => [
'type' => 'full'
]
]
图片字段配置
// 单图
[
'name' => 'custom_image',
'type' => 'image',
'label' => '自定义单图',
'option' => []
]
// 多图
[
'name' => 'custom_images',
'type' => 'image',
'label' => '自定义多图',
'option' => [
'limit' => 10, // 限制数量
'type' => 'multiple' // 启用多个
]
]
视频字段
// 单视频
[
'name' => 'custom_video',
'type' => 'video',
'label' => '单视频上传',
'option' => []
]
// 多视频
[
'name' => 'custom_videos',
'type' => 'video',
'label' => '多视频上传',
'option' => [
'limit' => 10, // 限制数量
'type' => 'multiple' // 启用多个
]
]
文件字段
// 单文件
[
'name' => 'custom_file',
'type' => 'file',
'label' => '单文件上传',
'option' => []
]
// 多文件
[
'name' => 'custom_files',
'type' => 'file',
'label' => '多文件上传',
'option' => [
'limit' => 10, // 限制数量
'type' => 'multiple' // 启用多个
]
]
组字段
[
'name' => 'custom_banner',
'type' => 'group',
'label' => '自定义Banner',
'option' => [
'limit' => 3,
'fields' => [
[ 'name' => 'image_pc', 'type' => 'image', 'label' => 'PC端Banner'],
[ 'name' => 'image_mobile', 'type' => 'image', 'label' => '手机端Banner'],
[ 'name' => 'text', 'type' => 'text', 'label' => 'Banner文本'],
]
]
]