CodeIgniter,包括从一个视图到另一个视图的代码?

时间:2011-07-17 03:01:31

标签: php codeigniter templates scope

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

$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存储在内容视图文件中的变量中,并(以某种方式)将其加载到标题视图中。说实话,我甚至认为这是不可能的。

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

谢谢,
最大

2 个答案:

答案 0 :(得分: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);
?>

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

希望这有帮助。

答案 1 :(得分:0)

如何使用充当小代理的脚本控制器:

/**
 * 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文件,无论如何它们都是个案配置。

相关问题