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