Try Templating On Code Igniter

Today, I’ve refreshing with CRUD on CI and now I get learn parser library on Code Igniter. I think it’s bery useful to make more complex website. Just read parser on Code Igniter user guide and read about parser library.

There are simple example on it. The Template Parser Class enables you to parse pseudo-variables contained within your view files. It can parse simple variables or variable tag pairs. If you’ve never used a template engine, pseudo-variables look like this:

{blog_title}
 
 
<h3>{blog_heading}</h3>
 
 
{blog_entries}
 
<h5>{title}</h5>
 
 
{body}
 
{/blog_entries}

These variables are not actual PHP variables, but rather plain text representations that allow you to eliminate PHP from your templates (view files).
In the above code you’ll notice a pair of variables: {blog_entries} data… {/blog_entries}. In a case like this, the entire chunk of data between these pairs would be repeated multiple times, corresponding to the number of rows in a result.

Parsing variable pairs is done using the identical code shown above to parse single variables, except, you will add a multi-dimensional array corresponding to your variable pair data. Consider this example:

$this-&gt;load-&gt;library('parser');
 
$data = array(
              'blog_title'   =&gt; 'My Blog Title',
              'blog_heading' =&gt; 'My Blog Heading',
              'blog_entries' =&gt; array(
                                      array('title' =&gt; 'Title 1', 'body' =&gt; 'Body 1'),
                                      array('title' =&gt; 'Title 2', 'body' =&gt; 'Body 2'),
                                      array('title' =&gt; 'Title 3', 'body' =&gt; 'Body 3'),
                                      array('title' =&gt; 'Title 4', 'body' =&gt; 'Body 4'),
                                      array('title' =&gt; 'Title 5', 'body' =&gt; 'Body 5')
                                      )
            );
 
$this-&gt;parser-&gt;parse('blog_template', $data);

If your “pair” data is coming from a database result, which is already a multi-dimensional array, you can simply use the database result_array() function:

$query = $this-&gt;db-&gt;query("SELECT * FROM blog");
 
$this-&gt;load-&gt;library('parser');
 
$data = array(
              'blog_title'   =&gt; 'My Blog Title',
              'blog_heading' =&gt; 'My Blog Heading',
              'blog_entries' =&gt; $query-&gt;result_array()
            );
 
$this-&gt;parser-&gt;parse('blog_template', $data);

Nice ya.. but I don’t want to use this.. because of rendering time reason.

Note: CodeIgniter does not require you to use this class since using pure PHP in your view pages lets them run a little faster. However, some developers prefer to use a template engine if they work with designers who they feel would find some confusion working with PHP.



Related Post:

Post a Comment

Your email is never published nor shared. You're allow to say what you want...

Blogroll Link Update