0

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)

Jeff
  • 12,085
  • 12
  • 82
  • 152
  • Why don't you use `$this->load->view('child_view_name');` inside of your parent view? – Tim Aug 13 '12 at 18:33
  • Could you please show me an example of how I could achieve my goal with that? :) – Jeff Aug 13 '12 at 18:54
  • It seems to me that you just need to add one portion of HTML to another portion, right? If yes, then loading a view from another view looks like a good idea to me. – Tim Aug 13 '12 at 19:13
  • Yes, but the portion I wish to add is specified in a view that gets loaded into the main content, and the portion I wish to add is supposed to go in the header - I will edit my example in MVC3 – Jeff Aug 13 '12 at 19:20

1 Answers1

2

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).

Community
  • 1
  • 1
  • I edited the OP - will any of these help me achieve my desired result? I cant seem to find anything relevant. :) – Jeff Aug 13 '12 at 19:57