Welcome Guest, Not a member yet? Register   Sign In
LazyTemplate - Templateing with intelligence
#1

[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 Wink.

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
#2

[eluser]Unknown[/eluser]
Features:

* Global Template: A "master template" is used in which all sub-templates will be embedded. There are currently the following variables exposed to the template:

$doctype, $title, $styles, $scripts, $content. This can be extended to any number you like, see next point... The template file needs to be in the root "views" directory!

* Sub-templating: if you need to include a e.g. menu on some of your pages, you can simply define it inside the config, a variable based on the name of the sub-template will be exposed to the templates. Example: array("menu" => "includes/menu") will include the defined file whenever the $menu var is present. Of course sub-templates can have sub-templates, just add an additional include to the config array and write the var in the template.

* Emphasizes a clean view directory by placing template files in a folder that matches the controllers name. So an action called "edit" that is executed when the "user" controller is requested is found as views/user/edit.php per default. (this behavior can be overridden, but then the whole purpose of LazyTempalte is gone and you can stick to the default CI rendering).

* Automatic creation of a title, if none is set. The title will be constructed from the URL, so if it you request user/edit, the title would be User | Edit (the "|" of course is configurable). Whenever you wish to override this behavior, you can change in on a per-method basis.

* A site name can be defined to be added to the title independently to the actual title e.g. "User | Edit - My Site" (again, easy customizable)

* Global caching: you can define a global caching time and a list of controllers/methods/controller+method when caching should not be executed. This way, all sites expect the listed ones will be cached. If you need to alter the cache lifetime, you can do it in each method. So if your news/index site changes with every request, you could go ahead and add news/index to the excluded sites so nothing is cached. If index actions should be excluded, add "/index" to the list, for a complete exclusion of a user controller, add "user/"

* Configurable folders for css and javascripts. They are placed inside a (per default namedSmile "public" folder. See config for more infos!

* Doctype can also be defined.

* Define your styles/javascripts in a global config and they will be included on all sites. To add a specific file on a per-method basis, use add_css(array/string) or add_js(array/string) accordingly.

* Chaining of methods so you can do
Code:
$this->renderer->set_title("FOO")->set_cachetime(60)->render();

* All functions can be accessed via a short alias to have even more "laziness":
Code:
$this->renderer->t("My page title")->c(60)->r(array("foo" => "bar"))

* Very basic (!) log info is added if you need to do some debugging (check CIs docs for logging)

Check the library to see what I might have forgotten to mention and check the config to learn what can be customized.
#3

[eluser]Dan Horrigan[/eluser]
Nice idea, but bad implementation.

If you are going to force the "lazy" developer to stick to a defined folder structure, why not just go whole hog and create a MY_Controller. Then you can have that automatically do everything (i.e. in your controllers that extend MY_Controller, no need to call the render).

Search for Jamie Rumbelow's Base Controller (MY_Controller). It does most everything you are trying to do...but more lazily :-).

Also, if I were to use this library, the first thing I would do is change the name of it. "Renderer" (while a real word), is just confusing to me (could just be me). Also seams redundant to type $this->renderer->render().

EDIT: If you are calling it "LazyTemplate" then name the class "Lazy" or just "Template" IMO.

Just my opinions. Nice work though.

Dan




Theme © iAndrew 2016 - Forum software by © MyBB