スニークスタ、
あなたが何を求めているかは理解できたと思いますが、コード例が投稿されていないため、100%確信はありません。
そこで、ヘッドタグ内にさまざまなスタイルのタグをモジュール式にロードできる「伸縮」ビューを作成する方法の例を示します。
Damien Pirsyが述べたように、ビューはバッファリングされます。つまり、CIは特別な出力バッファを作成し、一連のViewオブジェクトを連結して、最終的なバッファコンテンツを完成したWebページとして出力します。
以下の私の例は、この種の思考の連鎖に基づいています。
エンドユーザー(呼び出し)->ページコントローラー、次に:(パラメーターを呼び出して渡す)->ベースビュー(複数のフラグメントビューを呼び出す)->フラグメントビュー+->フラグメントビュー+->フラグメントビュー=最終累積ページ->(出力として返送)->エンドユーザー
まず、参照用に「base.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がそれを見つけるには、他のビューと一緒に配置する必要があります。 「application/views」フォルダ。これにより、ロードされるスタイルビューを変更するだけで、ヘッダーで宣言されたインラインスタイルブロックを交換できます。
<style type="text/css">
/*Style 1:*/
/*Just do something simple and obvious, i.e. turn text red*/
body { color: red; }
</style>
次に、「style2.php」と呼ばれる代替スタイルビューを作成します(注:次のコードフラグメントは、それ自体で別のファイルに含まれます)。CIがそれを見つけるには、他のビューと一緒に配置する必要があります。 「application/views」フォルダ内。これにより、ロードされるスタイルビューを変更するだけで、ヘッダーで宣言されたインラインスタイルブロックを交換できます。
<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);
}
}
?>
累積的な結果は、ロードする「スタイルビュー」を指定するだけで、ページヘッダー内のスタイルタグを切り替えるモジュラー機能になるはずです(データベーステーブルからロードする「スタイルビュー」を取得するモデルを構築することもできます)。リンクタグを介してCSSファイルにリンクするのではなく、実際のインラインHTMLソースコードを作成しているため、このアプローチには明らかにWebブラウザ内で特定の処理オーバーヘッドの制約があります。これは、ブラウザがページの読み込みごとにcssコンテンツをキャッシュしないことを意味しますが、後続のリクエストごとにダウンロードする必要があります。