I used PHP years ago but have since been developing in C#/VB.Net.
In ASP.Net MVC 2, you can provide a master page which defines content areas eg:
<html>
<head>
<title>Overshare | <?=$Title?></t开发者_如何学Goitle>
<ContentArea name="Head"/>
</head>
<body>
<ContentArea name="Body"/>
</body>
</html>
Your view would then have something like:
<Content name="Head">
<!-- Some Head Content -->
</Content>
<Content name="Body">
<h1>Some Body Here</h1>
</Content>
I don't seem to be able to emulate the same functionality with Code Igniter. The options seem to be:
- manually pre-set some associative array of variables (eg in the controller) and then simply substitute the values into a template file - This is a lot of code to repeat in each view and doesn't belong in the controller. It also means it's a real pain to put large bodies of html into one of the
ContentAreas
- It's either string concatenation or something equally nasty with almost no chance of HTML intellisense in any IDE. - Use a templating library - I haven't found one which doesn't fundamentally work as described above
Now, I haven't used CodeIgniter before and am about to start a large PHP project so want to make sure it's the correct tool before actually starting work. Am I missing something obvious or is this templating functionality difficult to replicate?
Edit: Libraries tested:
- Phil Sturgeon's Template Library
- CI Smarty
- PHXView
If you have a good idea of how your pages are to be built then you can write a set of functions to deal with it either in a MY_Controller.php file or in a library.
So you could have a routine which calls
$this->mypagetemplates();
Which calls data out of a class's properties eg $this->page->title;
I split my data as I create it into
$this->page->head, $this->page->header, $this->page->content, $this->page->aside $this->page->footer
Which corresponds with the HTML5 sections we use in 90% of our projects
My $this->mypagetemplates() function (or method if you prefer) can take a number of arguments and calls various views as a result eg:
$contentview = 'shop/products'; $asideview = 'shop/basket';
Which, if populated, are then called thus
If ($asideview) {
$this->load->view($asideview, $this->page->aside);
}
Overall Though, I'd say don't design your biggest ever project on a framework that us new to you. Play around first.
I ended up creating 3 files which represented the following
OpenHeader:
<html>
<head>
<Head Stuff/>
OpenBody:
</head>
<body>
<html>
<Templating Stuff>
Close:
</Templating Stuff>
</html>
</body>
</html>
And then modified my views to include these three at the appropriate time.
It's inelegant but flexible and powerful so it'll do for now - especially since I can pass varuables eg Page title into the files during the include if I use the CodeIgniter view engine to retrieve them
精彩评论