{body}
- {/blog_entries} + {blog_entries} +{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). .. note:: CodeIgniter does **not** require you to use this class since - using pure PHP in your view pages (for instance using the - :doc:`View renderer ` ) - lets them run a little faster. - However, some developers prefer to use some form of template engine if - they work with designers who they feel would find some - confusion working with PHP. + using pure PHP in your view pages (for instance using the + :doc:`View renderer ` ) + lets them run a little faster. + However, some developers prefer to use some form of template engine if + they work with designers who they feel would find some + confusion working with PHP. *************************** Using the View Parser Class @@ -45,12 +45,12 @@ Using the View Parser Class The simplest method to load the parser class is through its service:: - $parser = \Config\Services::parser(); + $parser = \Config\Services::parser(); Alternately, if you are not using the ``Parser`` class as your default renderer, you can instantiate it directly:: - $parser = new \CodeIgniter\View\Parser(); + $parser = new \CodeIgniter\View\Parser(); Then you can use any of the three standard rendering methods that it provides: **render(viewpath, options, save)**, **setVar(name, value, context)** and @@ -87,13 +87,13 @@ Parser templates You can use the ``render()`` method to parse (or render) simple templates, like this:: - $data = [ - 'blog_title' => 'My Blog Title', - 'blog_heading' => 'My Blog Heading', - ]; + $data = [ + 'blog_title' => 'My Blog Title', + 'blog_heading' => 'My Blog Heading', + ]; - echo $parser->setData($data) - ->render('blog_template'); + echo $parser->setData($data) + ->render('blog_template'); View parameters are passed to ``setData()`` as an associative array of data to be replaced in the template. In the above example, the @@ -110,18 +110,18 @@ Several options can be passed to the ``render()`` or ``renderString()`` methods. - ``cache`` - the time in seconds, to save a view's results; ignored for renderString() - ``cache_name`` - the ID used to save/retrieve a cached view result; defaults to the viewpath; - ignored for renderString() + ignored for renderString() - ``saveData`` - true if the view data parameters should be retained for subsequent calls; - default is **false** -- ``cascadeData`` - true if pseudo-variable settings should be passed on to nested - substitutions; default is **true** + default is **false** +- ``cascadeData`` - true if pseudo-variable settings should be passed on to nested + substitutions; default is **true** :: - echo $parser->render('blog_template', [ - 'cache' => HOUR, - 'cache_name' => 'something_unique', - ]); + echo $parser->render('blog_template', [ + 'cache' => HOUR, + 'cache_name' => 'something_unique', + ]); *********************** Substitution Variations @@ -134,12 +134,12 @@ The **simple substitution** performed by the parser is a one-to-one replacement of pseudo-variables where the corresponding data parameter has either a scalar or string value, as in this example:: - $template = '{body}
- {/blog_entries} + {blog_entries} +{body}
+ {/blog_entries} - - + + 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 @@ -186,20 +186,20 @@ 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:: - $data = [ - 'blog_title' => 'My Blog Title', - 'blog_heading' => 'My Blog Heading', - 'blog_entries' => [ - ['title' => 'Title 1', 'body' => 'Body 1'], - ['title' => 'Title 2', 'body' => 'Body 2'], - ['title' => 'Title 3', 'body' => 'Body 3'], - ['title' => 'Title 4', 'body' => 'Body 4'], - ['title' => 'Title 5', 'body' => 'Body 5'], - ], - ]; - - echo $parser->setData($data) - ->render('blog_template'); + $data = [ + 'blog_title' => 'My Blog Title', + 'blog_heading' => 'My Blog Heading', + 'blog_entries' => [ + ['title' => 'Title 1', 'body' => 'Body 1'], + ['title' => 'Title 2', 'body' => 'Body 2'], + ['title' => 'Title 3', 'body' => 'Body 3'], + ['title' => 'Title 4', 'body' => 'Body 4'], + ['title' => 'Title 5', 'body' => 'Body 5'], + ], + ]; + + echo $parser->setData($data) + ->render('blog_template'); The value for the pseudo-variable ``blog_entries`` is a sequential array of associative arrays. The outer level does not have keys associated @@ -209,16 +209,16 @@ If your "pair" data is coming from a database result, which is already a multi-dimensional array, you can simply use the database ``getResultArray()`` method:: - $query = $db->query("SELECT * FROM blog"); + $query = $db->query("SELECT * FROM blog"); - $data = [ - 'blog_title' => 'My Blog Title', - 'blog_heading' => 'My Blog Heading', - 'blog_entries' => $query->getResultArray(), - ]; + $data = [ + 'blog_title' => 'My Blog Title', + 'blog_heading' => 'My Blog Heading', + 'blog_entries' => $query->getResultArray(), + ]; - echo $parser->setData($data) - ->render('blog_template'); + echo $parser->setData($data) + ->render('blog_template'); If the array you are trying to loop over contains objects instead of arrays, the parser will first look for an ``asArray`` method on the object. If it exists, @@ -236,17 +236,17 @@ Nested Substitutions A nested substitution happens when the value for a pseudo-variable is an associative array of values, like a record from a database:: - $data = [ - 'blog_title' => 'My Blog Title', - 'blog_heading' => 'My Blog Heading', - 'blog_entry' => [ - 'title' => 'Title 1', - 'body' => 'Body 1', - ], - ]; + $data = [ + 'blog_title' => 'My Blog Title', + 'blog_heading' => 'My Blog Heading', + 'blog_entry' => [ + 'title' => 'Title 1', + 'body' => 'Body 1', + ], + ]; - echo $parser->setData($data) - ->render('blog_template'); + echo $parser->setData($data) + ->render('blog_template'); The value for the pseudo-variable ``blog_entry`` is an associative array. The key/value pairs defined inside it will be exposed inside @@ -254,13 +254,13 @@ the variable pair loop for that variable. A ``blog_template`` that might work for the above:: -{body}
-{body}
+{body}
-{body}
+