【问题标题】:CodeIgniter, including code from one view into another?CodeIgniter,包括从一个视图到另一个视图的代码?
【发布时间】:2011-10-06 23:48:40
【问题描述】:

我有一个有趣的问题。我目前有一个基本模板库,它为页眉和页脚模板呈现一堆模块,然后将我指定的视图夹在它们之间。
示例:

$this->load->view('header.php', $headerstuff);
$this->load->view($contentView);
$this->load->view('footer.php', $footerstuff);

问题是我需要将一些javascript(特定于每个内容视图)放入标题中。我一直在使用包含模板库中的 js 的 switch 语句来执行此操作。但这在 mvc 模型中毫无意义。

示例(我一直在做的),(在模板库中,上面的代码):

$headerstuff['js'] = '';
switch ($contentView)
{
    case 'main':
        $headerstuff['js'] = 'JAVASCRIPT INCLUDE CODE 1';
        break;
    case 'product':
        $headerstuff['js'] = 'JAVASCRIPT INCLUDE CODE 2';
        break;
}

不过,我想不出另一种方法来做到这一点。我想(理想情况下)将 js 存储在内容视图文件内的变量中,并(以某种方式)将其加载到标题视图中。不过老实说,我什至不认为这是可能的。

有没有人有比我目前的解决方案更好的方法?

谢谢,
最大

【问题讨论】:

  • 为什么不尝试将 JavaScript 加载到控制器中的变量中呢?然后你可以只检查标题视图中的 JS,而不是使用模板库中的开关。

标签: php codeigniter templates scope


【解决方案1】:

我为我的网站创建了一个帮助文件,我认为我们有一个类似的 MVC 模板布局:

资产助手:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('css'))
{
    function css($array) {
        // If the object passed is a string, convert it into an array
        if ( is_string($array) ) {
            $array = explode(" ", $array);
        }

        // Add additional CSS Files
        if ( isset($array) ) {
            foreach ( $array as $i => $file ) {
                // If it's not the first one add a tab character.
                if ( $i > 0 ) echo "\t";
                echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"". $file ."\">\n";
            }
        }
    }
}

if ( ! function_exists('js'))
{
    function js($array) {
        // If the object passed is a string, convert it into an array
        if ( is_string($array) ) {
            $array = explode(" ", $array);
        }

        // Add additional JavaScript Files
        if ( isset($array) ) {
            foreach ( $array as $i => $file ) {
                // If it's not the first one add a tab character.
                if ( $i > 0 ) echo "\t";
                echo "<script src=\"". $file ."\"></script>\n";
            }
        }
    }
}

这会做两件事之一。我将允许您在控制器文件中添加文件,这很好。我通过创建一个数据对象来做到这一点:

$data['css'] = array('/path/to/styles.css', '/path/to/otherstuff.css');
$data['js'] = array('/path/to/javascript.js');

然后在您的标题中包含执行以下操作:

<?
    $defaultCSS = array("/assets/css/global.css");
    $css = (isset($css)) ? array_merge($defaultCSS, $css) : $defaultCSS;        
    css($css);

    $defaultJS = array("/assets/js/global.js");
    $js = (isset($js)) ? array_merge($defaultJS, $js) : $defaultJS;
    js($js);
?>

我正在设置一些将在每个页面上加载的默认值,然后我可以根据我正在加载的控制器添加不同的文件。

希望这会有所帮助。

【讨论】:

  • 一个好主意,我希望每个视图(而不是控制器)都有它,但我在不同的控制器上使用相同的视图。但我敢打赌我可以将它与一个配置文件合并
  • 这样做的好处是它消除了您的视图中的更多逻辑。您最终拥有相同的控制权,但通过这种方法,您可以在其他项目中快速轻松地使用它。
  • 非常好的一点(也是我刚刚遇到的一个问题),感谢帮助类。它会派上用场(我只是想确保在标记您的答案之前可以修改它以使用配置文件)。
【解决方案2】:

拥有一个充当小型代理的脚本控制器怎么样:

/**
 * A basic controller which allows for the display of basic views.
 */
class Scripts extends CI_Controller
{

    public function _remap( $meth, array $params )
    {
        // ensure that parent directory contents can't be revealed.
        if( !$meth || strpos( $meth, '.' ) !== FALSE ) show_404();
        foreach( $params as $arg )
            // I put all css in a folder called css all js goes into 
            // a folder called js. It can be overly simple, but it works

            // (if you need these files to be php files, just add '.php'
            // to the end of the FILE's name. No need to worry about that
            // here, CodeIgniter's got your back...)
            $this->load->view( "$meth/${params}.$meth" ); 
    }
}

使用方法:

对于每个控制器(或者,您可以轻松地将其修改为适用于每个方法),在您的 views/js 文件夹中有一个适当的 js 文件。在各自的控制器之后命名每个。然后,从视图中指向它们中的每一个:

然后,在标题视图中:

<script type="text/javascript" src="
<?php 

// honestly, I normally use a helper function here, but this is the 
// short... short version.
echo base_url() . '/scripts/js/' . load_class('Router')->fetch_class();
// You can also replace the controller name with the view name by using the
// variable $_ci_view, you can get the full path with $_ci_path

?>" ></script>

最好的部分?您的控制器不知道视图的内容。视图或多或少与控制器无关(它是一个从外部源提前填充的变量,只是......在那里)。事实上,唯一“需要知道”的是 JS/CSS 文件,而且它们是根据具体情况进行配置的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 2016-10-20
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多