Do these CakePHP features exist in CodeIgniter? |
[eluser]TheActionCombo[/eluser]
I'm making the switch from CakePHP to CodeIgniter and I would like to know how CodeIgniter handles the following (if these are in the User Guide, I must've overlooked them): 1) Templates CakePHP allows you to create a single html template file where you just put <?=$content_for_layout?> where you want all views to be rendered. How is this handled in CI? Do you just put php includes in every view for the header and footer? 2) Elements This stems from the previous question. Elements in CakePHP are blocks of html that you want displayed on multiple pages (DRY). In CI, would I just use normal php includes again? And if so, do you just stick them in a folder in your root directory like your css/js/img files? 3) Components Components are like elements, but apply to controllers instead (segments of code you write once, but include in multiple controllers). Thanks
[eluser]gtech[/eluser]
1) Templates A controller function is called through the URL. A controller can call a model to modify or retrieve info from a database. Data can be passed to a view (the bit that displays the HTML). You can template pages by either loading the header then content then footer views in the controller OR by loading a header and footer inside a view (a view within a view). Views can contain PHP so they are very flexible. 2) Elements again there is more than one way to do this. technically your views are blocks of html, there is a views folder (and a models folder and a controllers folder), where you can apply your own structure. 3) Components I use libraries and helpers. these are classes that can be used in multiple controllers, models can also be included in multiple controllers but tend to be database centric. I have been using CI for a few months now and one thing I do like about CI is its community, the framework is very lightweight and easy to get into. searching the forums you can find solutions to most problems you encounter, [url="http://www.ciforge.com/"]CIForge[/url] and the codeigniter wiki have lots of usefull utlities you can download. Hope this helps
[eluser]TheActionCombo[/eluser]
Thanks for the reply. What if I wanted every page to have a "recent posts" list? Is there a way I can do this without coding it in every controller method/view?
[eluser]xwero[/eluser]
you could use my template library : Simple view. It allows you to use a template (1) where you can insert views (2) or controllers (3). I'm currently working on a more object way to switch the lay-out in the controllers and automatically giving the undefined variables an empty string so they don't throw an error and the configuration file gets smaller. i'm also going to try to find a way to have one place to process forms. I'm thinking about the login form that is visible everywhere on the site and most of the time changes into member shortcuts once they are logged in.
[eluser]TheActionCombo[/eluser]
Is there any way in CI to have a recents posts list on every page without using 3rd party solutions or manually generating that list in every controller method?
[eluser]nate_02631[/eluser]
[quote author="TheActionCombo" date="1195359535"]Is there any way in CI to have a recents posts list on every page without using 3rd party solutions or manually generating that list in every controller method?[/quote] The Wiki FAQ has some notes about dealing with partials, etc... as CI is very flexible in the way it lets you do them. Simplest way, I think, is you have a model that fetches a result set object (or array, if you prefer) of the most recent posts, then in the header/footer or other included view file of the layout you can call the model method and output the posts... Code: <? $latestPosts = $this->post->getLatest(); ?> Code: $data['latestPosts'] = $this->post->getLatest();
[eluser]mdavis1982[/eluser]
Actually, I don't think that is a violation of the MVC structure as Views are allowed Read-Only access to models.
[eluser]sophistry[/eluser]
The CI FAQ has a rundown of the many different ways to handle this in CI Please add to / edit if you notice something good that is not covered. |
Welcome Guest, Not a member yet? Register Sign In |