thinkcmf5 模板版变量的加载过程 和 新增网站配置项怎么全局使用
1、模板全局配置是怎么加载的
在 HomeBaseController.php 的 fech方法
$more = $this->getThemeFileMore($template);
用
echo ThemeModel::getLastSql(); 输出sql语句 :SELECT `more` FROM `cmf_theme_file` WHERE `theme` = 'w0s' AND ( `is_public` = 1 OR `file` = 'portal/index' ) 获取了声明公共配置和当前模板文件的模板配置。
可见,不管当前theme下那个模板文件配置,只要is_public=1,就可以加载vars 和 widgets 。 全局配置里的变量和控件就是这样加载的。。。。。
getThemeFileMore函数源码:

/** * 获取模板文件变量 * @param string $file * @param string $theme * @return array */ private function getThemeFileMore($file, $theme = "") { //TODO 增加缓存 $theme = empty($theme) ? cmf_get_current_theme() : $theme; // 调试模式下自动更新模板 if (APP_DEBUG) { $themeModel = new ThemeModel(); $themeModel->updateTheme($theme); } $themePath = config('cmf_theme_path'); $file = str_replace('\\', '/', $file); $file = str_replace('//', '/', $file); $file = str_replace(['.html', '.php', $themePath . $theme . "/"], '', $file); $files = Db::name('theme_file')->field('more')->where(['theme' => $theme])->where(function ($query) use ($file) { $query->where(['is_public' => 1])->whereOr(['file' => $file]); })->select(); echo ThemeModel::getLastSql(); $vars = []; $widgets = []; foreach ($files as $file) { $oldMore = json_decode($file['more'], true); if (!empty($oldMore['vars'])) { foreach ($oldMore['vars'] as $varName => $var) { $vars[$varName] = $var['value']; } } if (!empty($oldMore['widgets'])) { foreach ($oldMore['widgets'] as $widgetName => $widget) { $widgetVars = []; if (!empty($widget['vars'])) { foreach ($widget['vars'] as $varName => $var) { $widgetVars[$varName] = $var['value']; } } $widget['vars'] = $widgetVars; $widgets[$widgetName] = $widget; } } } return ['vars' => $vars, 'widgets' => $widgets]; }
2、新增网站配置项怎么全局使用
目前cmf5的后台配置并没有网站设置里并没有包含:公司名称,地址,电话,手机,在线客服等公司信息,两种方式可以实现这些配置项:
一是在声明了is_public=1的模板里增加模板变量(例如:config.json,head模板,foot模板)。 优点:无需修改源程序,升级不用担心覆盖,缺点:这些信息属于网站信息,应该独立于模板之外,否则换模板的话还需要重新设置。
二是在后台网站设置里增加新的配置项目。无须担心换模板,需要修改控制器基类,升级就会被覆盖,每次升级都需要修改一下。
这里使用第二种方法
app\admin\controller 新增ComSettingController.php

namespace app\admin\controller; use cmf\controller\AdminBaseController; use think\Validate; class ComSettingController extends AdminBaseController { /** * 公司信息配置 * @adminMenu( * 'name' => '公司信息配置', * 'parent' => 'admin/Setting/default', * 'display'=> true, * 'hasView'=> true, * 'order' => 10, * 'icon' => '', * 'remark' => '公司信息配置', * 'param' => '' * ) */ public function index() { $comSetting = cmf_get_option('com_setting'); $this->assign($comSetting); return $this->fetch(); } /** * 公司信息配置 * @adminMenu( * 'name' => '公司信息配置提交保存', * 'parent' => 'index', * 'display'=> false, * 'hasView'=> false, * 'order' => 10000, * 'icon' => '', * 'remark' => '公司信息配置提交保存', * 'param' => '' * ) */ public function indexPost() { $post = array_map('trim', $this->request->param()); //这里判断数据有效 cmf_set_option('com_setting', $post); $this->success("保存成功!"); } }
后台菜单增加对应菜单
这样就新增了 com_setting配置项
怎么使用???
(1)在对应controller里增加 例如:indexController,只能在当前模板使用
$comSetting = cmf_get_option('com_setting');
$this->assign(‘com_info’.$comSetting);
这样就可以在模板index.html 使用 com_info.company 来显示公司名称
(2)要想全局使用,需要加到controller的基类 HomeBaseController里,在simplewind\cmf\controller目录下
public function _initialize() { // 监听home_init hook('home_init'); parent::_initialize(); $siteInfo = cmf_get_site_info(); View::share('site_info', $siteInfo); $comSetting = cmf_get_option('com_setting'); View::share('com_info', $comSetting); }
这样就可以在任何模板里使用 com_info.company
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!