【问题标题】:Extending custom config array in Codeigniter within a view file在视图文件中扩展 Codeigniter 中的自定义配置数组
【发布时间】: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


【解决方案1】:

为什么不在控制器中设置配置变量? 视图不适用于逻辑。

【讨论】:

  • 我想把它放在一个视图文件中,我可以在一个地方定义所有常见的东西,如“page_name”、h1 标题、别名等......它们不是动态的,但是由我定义。您认为这不是最佳做法,对吧?
  • 这绝对不是最佳实践。最佳实践是在控制器中定义这些变量。 MVC 方法旨在将逻辑与视图区分开来,但您正在尝试将它们结合起来。
【解决方案2】:

你不应该在视图中这样做,设置数据应该从控制器完成。 视图应该只处理视图逻辑,而不是设置逻辑本身...... 然后,当一切准备就绪并准备就绪时,您将控制器中的变量传递到视图中。

【讨论】:

    【解决方案3】:

    好的,我明白了:

    这是我应该在views/home.php 中插入的内容:

    $this->config->set_item('sidebars',array(
      'recent',
      'rss'
    ));
    

    还是谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 2012-11-08
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多