所以我基本上是尝试将<link>
和<script>
标签从模块视图文件(显示在页面正文中)传递到我的原始头文件中。如何传递包含这些引用的变量?
目前,我刚刚在我的模块视图中放置了额外的<head></head>
标签,但这样做感觉很混乱和狡猾,因为这意味着头部标签在页面顶部和中途用完。
:没有意识到堆栈溢出编辑了我对这个问题至关重要的标签!对不起,伙计们!
听起来你真的需要一个CodeIgniter的模板设置。以下是我最喜欢的一些链接:
http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html
http://philsturgeon.co.uk/demos/codeigniter-template/user_guide/
我个人最喜欢的简单:
http://maestric.com/doc/php/codeigniter_template
编辑:根据@Sneaksta的问题,以下是如何将 css 脚本添加到我的模板中:
在我的主模板中,我有以下代码:
<?php if (!empty($cssFiles)): ?>
<?php foreach ($cssFiles as $styleSheet): ?>
<link rel="stylesheet" media="screen" href="<?= base_url(); ?>styles/<?= $styleSheet; ?>" />
<?php endforeach; ?>
<?php endif; ?>
然后在可能需要为每个函数加载不同 CSS 文件的控制器中,我这样做:
$cssFiles = array('style1.css', 'style2.css', 'style3.css');
$this->template->set('cssFiles', $cssFiles);
Sneaksta,
我想我明白你在问什么,但我不是 100% 确定,因为你没有发布任何代码示例。
因此,我将为您提供一个示例,说明如何拥有一个"伸缩"视图,该视图允许您在头部标签内模块化加载不同的样式标签。
正如 Damien Pirsy 所提到的,视图是缓冲的,这意味着 CI 会创建一个特殊的输出缓冲区,并将一系列 View 对象连接在一起,然后将最终的缓冲区内容作为完成的网页输出。
我下面的例子就是建立在这种思维链之上的:
最终用户 (调用) -->页面控制器,然后: (调用和传递参数) --> 基本视图 (调用多个片段视图) --> 片段视图 + --> 片段视图 + --> 片段视图 = 最终累积页面 -->(作为输出发回) --> 最终用户
首先制作"基础视图",为了参考,我们称之为"基础.php":
<!doctype html>
<html>
<head>
<!-- Base View -->
<?php
//This "if" + "isset()" statement is important,
// because if you forget to pass the variable in the controller
// CI will throw an error.
// Also important: the name of variable ($style) MUST match the
// the name of the associative element in the controller! (See
// remarks below on handling this in the controller)
if(isset($style))
{
//Loading views is like a telescoping effect:
//A view may load other views into itself recursively
$this->load->view($style);
}
else
{
//This echo statement will show a comment in
// source code if an error occurred during loading a view
echo "<!-- Style not found -->");
}
?>
</head>
<body>
<!-- Page Content Here -->
</body>
</html>
接下来,您创建样式视图(注意:以下代码片段将单独位于一个单独的文件中),我们将其称为"style1.php",并且必须与其他视图一起放置以便CI找到它,例如在"应用程序/视图"文件夹中。这使您只需更改加载的样式视图即可交换标头中声明的内联样式块:
<style type="text/css">
/*Style 1:*/
/*Just do something simple and obvious, i.e. turn text red*/
body { color: red; }
</style>
接下来,您创建备用样式视图(注意:以下代码片段将单独位于一个单独的文件中),我们将其称为"style2.php",并且必须与其他视图一起放置以便CI找到它,例如在"应用程序/视图"文件夹中。这使您只需更改加载的样式视图即可交换标头中声明的内联样式块:
<style type="text/css">
/*Style 2:*/
/*Just do something simple and obvious, i.e. turn text blue*/
body { color: blue; }
</style>
现在在我们的控制器"example.php"中,我们告诉 base.php 将 style1.php 文件加载到其标头中。 我们通过在加载 base.php 视图时将文件名作为参数传递来做到这一点,通过将文件名作为关联数组的元素传递,代码点火器将解析该参数数组并创建一个与关联元素同名的变量,并使该变量在 base.php 视图中可用:
<?php
class Example extends CI_Controller
{
//Constructor
function __construct(){ parent::__construct(); }
//Base View request handler
function baseview()
{
//Note: Make an array, be certain to name the element
// the same as what will be expected inside base.php
$params = array("style" => "style1.php");
//Switching to load a different style is now easy
// just comment out the line above, and uncomment the line below:
//$params = array("style" => "style2.php");
//Pass the parameters array into the CI load view method:
$this->load->view("base.php", $params);
}
}
?>
累积结果应该是通过指定要加载的"样式视图"来切换页面标题内的样式标记的模块化能力(您甚至可以构建一个模型来检索要从数据库表中加载的"样式视图")。 显然,这种方法在 Web 浏览器中具有一定的处理开销限制,因为您正在构建实际的内联 HTML 源代码,而不是通过链接标签链接到 CSS 文件。 这意味着浏览器不会为每个页面加载缓存 css 内容,而是必须在每个后续请求中下载它。