Welcome Guest, Not a member yet? Register   Sign In
A friendly template library
#1

[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>
    <title><?php echo $title; ?></title>
    <?php echo $stylesheet; ?>
</head>
<body>
    <?php echo $content; ?>
</body>

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>
    <title><?php echo $this->template->title; ?></title>
    <?php echo $this->template->stylesheet; ?>
</head>
<body>
    <?php echo $this->template->content; ?>
</body>

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
$partial->append() - append something
$partial->add() - same as append (alias)
$partial->prepend() - prepend something
$partial->content() - gets the content
$partial->default() - only set content if empty

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
javascript - you only need to pass the source
meta - will convert the arguments to the right meta tag
title - converts special characters
description - will convert special characters just like the title

This is an example of what these built in triggers do:

Code:
$this->template->stylesheet->add("stylesheet.css");
//<link rel="stylesheet" type="text/css" href="http://myweb.com/stylesheet.css" />

$this->template->javascript->add("script.js");
//[removed][removed]

$this->template->meta->add("robots", "index,follow");
//<meta name="robots" content="index,follow" />

$this->template->title->set("Dad & Son");
//Dad & Son

You can set your own triggers for functions or methods for any partial object like this:

Code:
//function
$this->template->partial->set_trigger("strtoupper");

//method
$this->template->partial->set_trigger($this->typography, "auto_typography");

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 */
class hero_widget extends Widget {
     public function display($args = array()) {
         //$this->prepend("<h1>Slideshow:</h1>");

         $this->load->model("my_model");
         $data = $this->my_model->all();

         $this->view("widgets/hero", $data);
     }
}

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


Messages In This Thread
A friendly template library - by El Forum - 01-13-2012, 05:22 AM
A friendly template library - by El Forum - 02-05-2012, 11:30 PM
A friendly template library - by El Forum - 02-06-2012, 02:47 AM
A friendly template library - by El Forum - 02-06-2012, 06:15 AM
A friendly template library - by El Forum - 02-06-2012, 08:05 PM
A friendly template library - by El Forum - 02-13-2012, 05:21 PM
A friendly template library - by El Forum - 02-14-2012, 06:16 AM
A friendly template library - by El Forum - 02-15-2012, 10:12 AM
A friendly template library - by El Forum - 03-28-2012, 11:37 AM
A friendly template library - by El Forum - 04-02-2012, 08:51 AM
A friendly template library - by El Forum - 04-02-2012, 02:53 PM
A friendly template library - by El Forum - 04-03-2012, 09:25 AM



Theme © iAndrew 2016 - Forum software by © MyBB