【发布时间】:2012-08-12 15:20:00
【问题描述】:
我正在使用 codeigniter 2.x。我需要的是从视图文件/views/home.php 的内部将一个值插入到config/template.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>
最后,它将application/views/home.php 加载到applications/views/template/template.php 内的特定div 块中。这是/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 等...?
抱歉,代码很大。
提前致谢。
【问题讨论】:
-
是什么让您相信这是可能的?只是为了四处逛逛还是你真的做了一些研究?
-
@hakra 对愚蠢的问题感到抱歉,但我真的对这个问题感到困惑,我已经用谷歌搜索了类似
php array insert values等的东西......但就是无法得到它。你觉得不可能吗?还是只是将我指向正确的方向,以便我可以继续寻找一些线索?谢谢
标签: php multidimensional-array codeigniter-2