Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame^] | 1 | ##################### |
| 2 | Template Parser Class |
| 3 | ##################### |
| 4 | |
| 5 | The Template Parser Class enables you to parse pseudo-variables |
| 6 | contained within your view files. It can parse simple variables or |
| 7 | variable tag pairs. If you've never used a template engine, |
| 8 | pseudo-variables look like this:: |
| 9 | |
| 10 | <html> <head> <title>{blog_title}</title> </head> <body> <h3>{blog_heading}</h3> {blog_entries} <h5>{title}</h5> <p>{body}</p> {/blog_entries} </body> </html> |
| 11 | |
| 12 | These variables are not actual PHP variables, but rather plain text |
| 13 | representations that allow you to eliminate PHP from your templates |
| 14 | (view files). |
| 15 | |
| 16 | .. note:: CodeIgniter does **not** require you to use this class since |
| 17 | using pure PHP in your view pages lets them run a little faster. |
| 18 | However, some developers prefer to use a template engine if they work |
| 19 | with designers who they feel would find some confusion working with PHP. |
| 20 | |
| 21 | .. important:: The Template Parser Class is **not** a full-blown |
| 22 | template parsing solution. We've kept it very lean on purpose in order |
| 23 | to maintain maximum performance. |
| 24 | |
| 25 | Initializing the Class |
| 26 | ====================== |
| 27 | |
| 28 | Like most other classes in CodeIgniter, the Parser class is initialized |
| 29 | in your controller using the $this->load->library function:: |
| 30 | |
| 31 | $this->load->library('parser'); |
| 32 | |
| 33 | Once loaded, the Parser library object will be available using: |
| 34 | $this->parser |
| 35 | |
| 36 | The following functions are available in this library: |
| 37 | |
| 38 | $this->parser->parse() |
| 39 | ====================== |
| 40 | |
| 41 | This method accepts a template name and data array as input, and it |
| 42 | generates a parsed version. Example:: |
| 43 | |
| 44 | $this->load->library('parser'); $data = array( 'blog_title' => 'My Blog Title', 'blog_heading' => 'My Blog Heading' ); $this->parser->parse('blog_template', $data); |
| 45 | |
| 46 | The first parameter contains the name of the :doc:`view |
| 47 | file <../general/views>` (in this example the file would be called |
| 48 | blog_template.php), and the second parameter contains an associative |
| 49 | array of data to be replaced in the template. In the above example, the |
| 50 | template would contain two variables: {blog_title} and {blog_heading} |
| 51 | |
| 52 | There is no need to "echo" or do something with the data returned by |
| 53 | $this->parser->parse(). It is automatically passed to the output class |
| 54 | to be sent to the browser. However, if you do want the data returned |
| 55 | instead of sent to the output class you can pass TRUE (boolean) to the |
| 56 | third parameter:: |
| 57 | |
| 58 | $string = $this->parser->parse('blog_template', $data, TRUE); |
| 59 | |
| 60 | $this->parser->parse_string() |
| 61 | ============================== |
| 62 | |
| 63 | This method works exactly like parse(), only accepts a string as the |
| 64 | first parameter in place of a view file. |
| 65 | |
| 66 | Variable Pairs |
| 67 | ============== |
| 68 | |
| 69 | The above example code allows simple variables to be replaced. What if |
| 70 | you would like an entire block of variables to be repeated, with each |
| 71 | iteration containing new values? Consider the template example we showed |
| 72 | at the top of the page:: |
| 73 | |
| 74 | <html> <head> <title>{blog_title}</title> </head> <body> <h3>{blog_heading}</h3> {blog_entries} <h5>{title}</h5> <p>{body}</p> {/blog_entries} </body> </html> |
| 75 | |
| 76 | In the above code you'll notice a pair of variables: {blog_entries} |
| 77 | data... {/blog_entries}. In a case like this, the entire chunk of data |
| 78 | between these pairs would be repeated multiple times, corresponding to |
| 79 | the number of rows in a result. |
| 80 | |
| 81 | Parsing variable pairs is done using the identical code shown above to |
| 82 | parse single variables, except, you will add a multi-dimensional array |
| 83 | corresponding to your variable pair data. Consider this example:: |
| 84 | |
| 85 | $this->load->library('parser'); $data = array( 'blog_title' => 'My Blog Title', 'blog_heading' => 'My Blog Heading', 'blog_entries' => array( array('title' => 'Title 1', 'body' => 'Body 1'), array('title' => 'Title 2', 'body' => 'Body 2'), array('title' => 'Title 3', 'body' => 'Body 3'), array('title' => 'Title 4', 'body' => 'Body 4'), array('title' => 'Title 5', 'body' => 'Body 5') ) ); $this->parser->parse('blog_template', $data); |
| 86 | |
| 87 | If your "pair" data is coming from a database result, which is already a |
| 88 | multi-dimensional array, you can simply use the database result_array() |
| 89 | function:: |
| 90 | |
| 91 | $query = $this->db->query("SELECT * FROM blog"); $this->load->library('parser'); $data = array( 'blog_title' => 'My Blog Title', 'blog_heading' => 'My Blog Heading', 'blog_entries' => $query->result_array() ); $this->parser->parse('blog_template', $data); |
| 92 | |