I've been wondering what's the use of Controller_Template when you can practically do the same things on a plain base Controller. Is there anything that I miss, wherein Controller_Template can do something that Controller can't out of the box?
I did understand the use of ViewModels compared to using Controllers and Views alone (though I can't think of any use for those nifty features right now), but no matter how I look at Controller_Template, I can't seem to grasp the purpose of it. Someone please enlighten me.
oh.
But it seems that some of functions are removed in latest release.like
$theme->get_template()->set('title', 'This is the page title');
and
$this->theme->set_template('default/main');
I used get_template and get this error :
ErrorException [ Error ]: Call to undefined method Fuel\Core\Theme::set_template()
!
Those were never in the latest release (which is 1.1).
These were introduced quite recently (that why they are in the development documentation but not in the release documentation) and will be part of the upcoming v1.2.
If you're looking at the development docs, be sure to install the 1.1/develop branch of all fuel repo's.
The Controller_Template is just a simple base controller.
It allows you do define a page template which echo's a variable there where the page content has to go. The action in the controller then only has to set that variable on the template. The action doesn't have to return anything, you don't have to create a response object that you have to return.
It's a matter of convenience, if you use the normal Controller you'll have to code this by hand in very controller you create.
A Viewmodel abstracts the support code for a view away from the controller. In many cases your controller needs to fetch or process data for the view that is not related to the purpose of the controller action. This can be loading data for form dropdowns, getting message information from session flash, getting data for the current user to display, maybe data for a breadcrumb on the form, etc.
Thanks for the info. So rather than doing this:
$views = View::forge('head', $data)->render();
$views = View::forge('body',$data)->render();
$views = View::forge('footer',$data)->render();
I would simply use:
$this->template->header = View::forge('head', $data);
$this->template->content = View::forge('body',$data);
...
so they basically do the same thing right? Thanks a lot for that info, that's a nice upgrade from CI which did not have templates built out of the box.
And thanks for the explanation on ViewModels, I suddenly saw the importance of ViewModels. I'm used to how CI handles things, which is put all the data on the Controller before sending it to the view. I guess this will come handy.
Correct.
And you don't have to call render() in any case, FuelPHP will do that for you if and when needed.
This will provide you basic templating functionality. If you want a step further, look at the Theme class, which adds multiple themes, multiple templates per theme, partial support, theme assets, and a theme fallback system (anything not found in the active theme will be loaded from the fallback theme). It also has full view support, so you can add views to your themes to overload default views in your app or modules so a theme can change the layout.
Thanks, just peeked at the core classes and spotted the theme class. So this class isn't yet documented right? Will read an learn the class right now, good thing there's plenty of comments.
The documentation on docs.fuelphp.com is the official release documentation, which represents the state of the 1.1/master branch.
The documentation of the current development branch can be found via the 'dev-docs' link in the header. It is much more up to date, and includes documentation of the Theme class.
As to inline documentation: I started my developers life writing assembler. Where inline documentation is a must. A habbit that has never left me...