A friendly template library |
[eluser]Ordos[/eluser]
Codeigniter template library This template library for Codeigniter lets you build complex templates using layouts, partial views and widgets. It's built with the same method chaining support that we are seeing so often in Codeigniter so it feels familiar. This library loads a layout file that uses partial views. These partial view sections are internally represented by Partial Objects managed by the template library. These objects let you modify their content through method chaining. Installation Download the files from: https://github.com/segersjens/CodeIgnite...te-Library and copy the files to the corresponding folder. Check the readme file for detailed configuration instructions. Layout files Layout files are loaded or parsed by Codeigniter and the partials are passed to them as data. You can easily load them like you would normally use data in your view files: Code: <head> Or when parsing is enabled you can use {content} etc. However, I prefer to directly call the library's methods from inside the layout file to work around php's Undefined variable errors when you are not setting all partials. Calling these methods well replace non-existing partials with empty one's so you don't get any errors: Code: <head> These variables are in fact Partial Objects, so you can still manipulate them from inside the layout view file like this: Code: <?php echo $title->prepend("My Website - "); ?> Partial manipulation methods will always return the partial object itself for further chaining or for displaying. So this is perfectly possible: Code: <?php echo $sidebar->cache(500)->widget("login")->prepend("Login: "); ?> Partial manipulation Partials have a few handy methods for manipulating their content such as: Quote:$partial->set() - overwrites the content You can also load dynamic content inside partials from view files or widgets. The object named partial used in the method chaining below is the name of the partial you want to load the content into. Code: $this->template->partial->view() Append or overwrite the partial with a view file with parameters. Code: $this->template->partial->view("view-file", array(), $overwrite=FALSE); Append or overwrite the partial with a parsed view file with parameters. Code: $this->template->partial->parse("view-file", array(), $overwrite=FALSE); Append or overwrite the partial with a widget's output. Code: $this->template->partial->widget("widget-name", array(), $overwrite=FALSE); Publishing The template class only has a few methods. I chose to do this because almost everything can be managed by using the flexible Partial Object. If you want to publish the entire template with the current partials to the output you can use the publish() method. You can pass a custom layout file and optional data if wanted: Code: $this->template->publish("layout", array("title"=>"Title is overwritten!")); Most of the time this will be empty using the layout file from the config: Code: $this->template->publish(); Triggers Some partials have built in triggers: Quote:stylesheet - you only need to pass the source This is an example of what these built in triggers do: Code: $this->template->stylesheet->add("stylesheet.css"); You can set your own triggers for functions or methods for any partial object like this: Code: //function This will trigger the function or method whenever you manipulate the partial's content. Widget Widgets are intelligent partial objects. When their content is asked, their display() method is activated which will fill the content using codeigniter or partial object methods. Widgets classes are found inside the application/widgets folder. They extend the main Widget class which has the same methods as the Partial class. This is an example widget: Code: /* File: widgets/hero.php */ And this is loaded from a controller like this: Code: $this->template->partial->widget("hero_widget", $args = array()); Read more about widges and caching from the readme file: (character limit) https://github.com/segersjens/CodeIgnite...te-Library
[eluser]lifran[/eluser]
Thank's for the info. I'm newbie in codeigniter template system. I have a question, where i have to place the 'demo' folder, so i can test the demo inside the template? Thank you...
[eluser]Ordos[/eluser]
[quote author="lifran" date="1328509821"]Thank's for the info. I'm newbie in codeigniter template system. I have a question, where i have to place the 'demo' folder, so i can test the demo inside the template? Thank you...[/quote] You need to place the content of the demo folder in your application folder.
[eluser]goFrendiAsgard[/eluser]
Good, you even think about CSS, javascript and meta in the template. Sounds interesting
[eluser]lifran[/eluser]
hi Ordos, i have do what you tell me, and now it works. Many thanks...
[eluser]seanloving[/eluser]
Please take a look at my use of this template library and how my first prototype is organized. Suggestions, comments and criticism welcome. I also have a specific question about each view partial having its own stylesheet. See below. File Structure application libraries Template.php modules users controllers users.php views templates admin.php admin head.php body.php header.php main.php footer.php assets themes admin css reset.css layout.css header.css main.css footer.css The name of my template / theme is "admin" Here is my login() controller which is a method of the users.php class application/modules/users/controllers/users.php (login method) Code: function login() views/templates/admin.php Code: <?php echo $this->template->doctype->set('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'); ?> views/templates/admin/head.php Code: <title><?php echo $this->template->title->set('Login Page'); ?></title> views/templates/admin/body.php Code: <div id="header"> views/templates/admin/header.php Code: <?php $this->template->stylesheet->add('assets/themes/admin/css/header.css'); ?> views/templates/admin/main.php Code: <?php $this->template->stylesheet->add('assets/themes/admin/css/main.css'); ?> views/templates/admin/footer.php Code: <?php $this->template->stylesheet->add('assets/themes/admin/css/footer.css'); ?> I don't like how my controller has to calculate the <body> before the <head>, and has to include some stylesheets in the controller itself, just to achieve the desired order of stylesheets in the <head> Page Source Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> The generated output looks good, but... Is there a better way to collect all the stylesheets into the page <head>?
[eluser]Mauricio de Abreu Antunes[/eluser]
Use an array with your style files. Foreach your array, adding your files to view, calling add method. Consider to build a method for this
[eluser]Ordos[/eluser]
[quote author="Mauricio de Abreu Antunes" date="1329225366"]Use an array with your style files. Foreach your array, adding your files to view, calling add method. Consider to build a method for this [/quote] You can now do stuff like: Code: $styles = array("reset.css", "layout.css"); or even: Code: $stuff = array("Hello ", "there");
[eluser]seanloving[/eluser]
Instead of a test in the view file: Code: if( $this->template->header ) echo $this->template->header; I wish the set_default() method would NOT set the contents of the partial if the partial already exists. This way, if the controller does not set the partial then the default view will be used, which makes the view file more friendly, and the library easier to use: Code: // echo default view unless header was already set in the controller Do you think this is useful and would you make this change? Or am I overlooking an obvious or built-in way to do this?
[eluser]Mauricio de Abreu Antunes[/eluser]
Anybody does has a solid example about the usage for this library? Thanks in advance! |
Welcome Guest, Not a member yet? Register Sign In |