Jeff
Jeff

Reputation: 12183

Using sections in CodeIgniter

I'm using ASP.NET MVC3 at work, while using PHP at home. I wish to use the Sections feature of MVC3 in CodeIgniter, does it have such feature?

Example in ASP.NET MVC3 (not tested):

<!-- A view, that serves the content -->
@section Head{
    <script src="myscript.js"></script>
}
<p>This is my main content - 
I require myscript.js to be included on my page, 
so I specify a head section, which the header of my page will render</p>



<!-- ANOTHER view, that serves the content -->
@section Head{
    <script src="otherscript.js"></script>
    <link rel="stylesheet" type="text/css" href="somestyle.css" />
}
<p>Some OTHER view, with OTHER dependencies!</p>





<!-- Header File, where I include CSS and JS! -->
<head>
<script src="jquery.js"></script>

@Html.Section("Head")

</head>

The point is, for example, in a specific view, I wish to include a script file, so I add the <script> to the section, and that section is rendered in the header (which is in a different view)

Upvotes: 0

Views: 2401

Answers (1)

It seems like @Section is just some syntactic sugar for the notion of a partial template, or a sub view you can contain within a view.

In CodeIgnier, that's simply another view that you would load in your controller while loading the main view, and customize the HTML of your main view to include. For loading multiple views in CodeIgniter, see here.

Of course, that's just if you wanna wire things up yourself, as seen here. A better way is to use a template library. There are many template libraries available for CodeIgniter. Some people have asked for comparisons, and Phil Sturgeon's seems popular (and to do what you want).

Upvotes: 2

Related Questions