30 December, 2009

How to use CodeIgniters template parser

This little tutorials shows how you can use CodeIgniters template parser. I like to use this utility to make my HTML code more readable and keep PHP code out of design files. It's also easier to write documents based on this.

  • Start the parser in your control file.
  • Add some data that goes to view.
  • And put the stuff together, you don't need to make a call to $this->load->view after parse.
  • Add the tags in view file where you need the data.

This is a part of my controller file:

1 function index()
2 {
3 $this->load->library('parser');
4 $data = array(
5 'name_for_some_data' => 'some_data',
6 'name_for_more_data' => 'more_data'
7 );
8 $this->parser->parse('view_name', $data);
9 }

This one is in view file:

1 <html>
2 <head>
3 <title>{name_for_some_data}</title>
4 </head>
5 <body>
6 {name_for_more_data}
7 </body>
8 </html>

No comments:

Post a Comment