[eluser]Unknown[/eluser]
The idea behind this is to turn the display of files and its contents into as little code as possible. Many things are guessed from the connection between the name and the context in which it is executed (call it convention over configuration if you want, since many of the ideas here are taken from Rails). The way views are handled was never satisfying for me. There were no libraries available, that needed little configuration and only 1 line of code to do lots of "magic", most of them need to be configured with various parameters or even static HTML. I like to place views for a specific controller inside a folder with the controller’s name. This keeps a clean folder structure, where everything can be found easily and since my view file matches the function name (e.g. edit, new, login,..), it gets very obvious which file is responsible for what action. Since the definition of the folder and templates should be done as automated as possible, LazyTemplate requires the developer/designer to stick to a certain folder structure that is very natural.
Sounds like there are/might be lots of rules? Let's have an example.
!All examples assume that the controllers name is "user" and we are working with the "edit" function!
Case 1:
The easiest way to get display something is calling
Code:
$this->renderer->render()
from within your method. This does lots of things: it loads the master template and populates it with a doctype, your configured and/or manually added styles and scripts, handles caching if you defined it. Furthermore, it sets the pages title to "User | Edit". The view that will be used lies within the views/user folder and is called edit.php. So the "edit" view will be embedded into the master template. No data is displayed from the controller since we didn't pass any
.
Case 2:
Let's pass some data to the view:
Code:
$this->renderer->render(array('some_value' => 'FOO!', 'other_value' => 'bar')
This is the same way as CI passes data to its views, except you do not need to define the view file if you stick to the default. Nothing special here, again, views/user/edit.php will be used since we haven't defined it otherwise. The title will again be "
User | Edit". This is the most common use of the renderer() function (at least for me).
Case 3:
If you want to just display a view with no data, you know already that you can simply call
Code:
$this->renderer->render()
But I don't want to load the "edit" view. So let's just load another view:
Code:
$this->renderer->render("update")
(per default, this would be "edit"). This way, a file that's inside views/user named update.php will be used. Can't get any simpler. The same goes for the title as in Cases 1 & 2.
Case 4:
You can combine everything to your liking: passing data to the view is shown in "Case 2". But if you don't wish to load the "edit" view, just pass the view you want to load with the renderer() call:
Code:
$this->renderer->render(array('some_value' => 'FOO!', 'other_value' => 'bar'), "update")
So although we are inside the "edit" function, another file is loaded. Since our URL is still user/edit, the title will also be "User | Edit". So let's change that:
Code:
$this->renderer->set_title("User | Change")
or alternatively pass
Code:
$data['title'] = "User | Update"
with the data array. In any case, if you defined a site name in the configs, it will be appended to the title.
Please check the next post to get a list of all features