我正在使用 codeigniter 2.x。config/template.php
我需要的是从视图文件内部将值插入到数组中/views/home.php
。
我创建了一个自定义配置文件application/config/template.php
:
$config['site_name'] = "Sitename";
$config['site_lang'] = "En-en";
$config['page_name'] = "Pagename";
$config['css_page'] = "default";
$config['alias'] = "";
$config['head_meta'] = array(
'description' => 'description',
'keywords' => 'meta, keywords',
'stylesheets' => array(
'template.css'
),
'scripts' => array(
'jquery.js',
'template.js'
),
'charset' => 'UTF-8'
);
$config['sidebars'] = array();
然后,我将application/views/template/template.php
其用作我的主要 HTML 布局,一开始我包含一个文件,该文件application/views/template/includes/inc-tpl-cfg.php
将我的模板配置全球化到一个带有数组的文件中,这样我就可以更容易地访问它们。这是那的内容inc-tpl-cfg.php
:
<?php
// No direct acces to this file
if (!defined('BASEPATH')) exit('No direct script access allowed');
/* Template configuration needs to be defined to make them accessible in the whole template */
$cfg_template = array(
'sitename' => $this->config->item('site_name'),
'sitelang' => $this->config->item('site_lang'),
'pagename' => $this->config->item('page_name'),
'csspage' => $this->config->item('css_page'),
'charset' => $this->config->item('charset','head_meta'),
'description' => $this->config->item('description','head_meta'),
'keywords' => $this->config->item('keywords','head_meta'),
'stylesheets' => $this->config->item('stylesheets','head_meta'),
'scripts' => $this->config->item('scripts','head_meta'),
'sidebars' => $this->config->item('sidebars')
);
/* Template variables */
$cfg_assetsUrl = base_url() . 'assets';
// If pagename exists than concatenate it with a sitename, else output only sitename
if(!empty($cfg_template['pagename'])){
$title = $cfg_template['pagename'] . ' - ' . $cfg_template['sitename'];
}else{
$title = $cfg_template['sitename'];
}
我的主要模板布局中的一部分是带有侧边栏的块:
<div id="tpl-sidebar">
<?php foreach($cfg_template['sidebars'] as $sidebar):?>
<?php $this->load->view('modules/'. $sidebar);?>
<?php endforeach ;?>
</div>
最后,它将 a 加载application/views/home.php
到内部的特定div
块中applications/views/template/template.php
。这是一个/views/home.php
:
<?php
// No direct acces to this file
if (!defined('BASEPATH')) exit('No direct script access allowed');
// Page configuration
$this->config->set_item('page_name','Homepage');
$this->config->set_item('css_page','home');
$this->config->set_item('alias','home');
?>
<p>
WELCOME BLABLABLA
</p>
</h3>
有一个部分我可以定义/覆盖默认值,config/template.php
并为每个视图使用特定值。所以我的问题是,如何$config[sidebar]
通过插入一些新项目来扩展此视图文件中的数组,例如:recent.php
等rss.php
...?
抱歉,代码很大。
提前致谢。