summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/libraries/parser.rst
diff options
context:
space:
mode:
Diffstat (limited to 'user_guide_src/source/libraries/parser.rst')
-rw-r--r--user_guide_src/source/libraries/parser.rst202
1 files changed, 104 insertions, 98 deletions
diff --git a/user_guide_src/source/libraries/parser.rst b/user_guide_src/source/libraries/parser.rst
index e7c7e3abd..d66684d9b 100644
--- a/user_guide_src/source/libraries/parser.rst
+++ b/user_guide_src/source/libraries/parser.rst
@@ -10,18 +10,18 @@ If you've never used a template engine,
pseudo-variable names are enclosed in braces, like this::
<html>
- <head>
- <title>{blog_title}</title>
- </head>
- <body>
-
- <h3>{blog_heading}</h3>
-
- {blog_entries}
- <h5>{title}</h5>
- <p>{body}</p>
- {/blog_entries}
- </body>
+ <head>
+ <title>{blog_title}</title>
+ </head>
+ <body>
+ <h3>{blog_heading}</h3>
+
+ {blog_entries}
+ <h5>{title}</h5>
+ <p>{body}</p>
+ {/blog_entries}
+
+ </body>
</html>
These variables are not actual PHP variables, but rather plain text
@@ -30,8 +30,8 @@ representations that allow you to eliminate PHP from your templates
.. 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
+ However, some developers prefer to use a template engine if
+ they work with designers who they feel would find some
confusion working with PHP.
.. important:: The Template Parser Class is **not** a full-blown
@@ -53,7 +53,7 @@ Initializing the Class
======================
Like most other classes in CodeIgniter, the Parser class is initialized
-in your controller using the $this->load->library function::
+in your controller using the ``$this->load->library()`` method::
$this->load->library('parser');
@@ -63,12 +63,12 @@ $this->parser
Parsing templates
=================
-You can use the ``parse()`` method to parse (or render) simple templates,
+You can use the ``parse()`` method to parse (or render) simple templates,
like this::
$data = array(
- 'blog_title' => 'My Blog Title',
- 'blog_heading' => 'My Blog Heading'
+ 'blog_title' => 'My Blog Title',
+ 'blog_heading' => 'My Blog Heading'
);
$this->parser->parse('blog_template', $data);
@@ -96,18 +96,18 @@ iteration containing new values? Consider the template example we showed
at the top of the page::
<html>
- <head>
- <title>{blog_title}</title>
- </head>
- <body>
-
- <h3>{blog_heading}</h3>
-
- {blog_entries}
- <h5>{title}</h5>
- <p>{body}</p>
- {/blog_entries}
- </body>
+ <head>
+ <title>{blog_title}</title>
+ </head>
+ <body>
+ <h3>{blog_heading}</h3>
+
+ {blog_entries}
+ <h5>{title}</h5>
+ <p>{body}</p>
+ {/blog_entries}
+
+ </body>
</html>
In the above code you'll notice a pair of variables: {blog_entries}
@@ -122,31 +122,31 @@ corresponding to your variable pair data. Consider this example::
$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')
- )
+ '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);
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::
+multi-dimensional array, you can simply use the database ``result_array()``
+method::
$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()
+ 'blog_title' => 'My Blog Title',
+ 'blog_heading' => 'My Blog Heading',
+ 'blog_entries' => $query->result_array()
);
$this->parser->parse('blog_template', $data);
@@ -154,94 +154,95 @@ function::
Usage Notes
===========
-If you include substitution parameters that are not referenced in your
+If you include substitution parameters that are not referenced in your
template, they are ignored::
$template = 'Hello, {firstname} {lastname}';
$data = array(
- 'title' => 'Mr',
- 'firstname' => 'John',
- 'lastname' => 'Doe'
+ 'title' => 'Mr',
+ 'firstname' => 'John',
+ 'lastname' => 'Doe'
);
$this->parser->parse_string($template, $data);
- Result: Hello, John Doe
+ // Result: Hello, John Doe
-If you do not include a substitution parameter that is referenced in your
+If you do not include a substitution parameter that is referenced in your
template, the original pseudo-variable is shown in the result::
$template = 'Hello, {firstname} {initials} {lastname}';
$data = array(
- 'title' => 'Mr',
- 'firstname' => 'John',
- 'lastname' => 'Doe'
+ 'title' => 'Mr',
+ 'firstname' => 'John',
+ 'lastname' => 'Doe'
);
$this->parser->parse_string($template, $data);
- Result: Hello, John {initials} Doe
+ // Result: Hello, John {initials} Doe
-If you provide a string substitution parameter when an array is expected,
+If you provide a string substitution parameter when an array is expected,
i.e. for a variable pair, the substitution is done for the opening variable
pair tag, but the closing variable pair tag is not rendered properly::
$template = 'Hello, {firstname} {lastname} ({degrees}{degree} {/degrees})';
$data = array(
- 'degrees' => 'Mr',
- 'firstname' => 'John',
- 'lastname' => 'Doe',
- 'titles' => array(
- array('degree' => 'BSc'),
- array('degree' => 'PhD')
-
- )
+ 'degrees' => 'Mr',
+ 'firstname' => 'John',
+ 'lastname' => 'Doe',
+ 'titles' => array(
+ array('degree' => 'BSc'),
+ array('degree' => 'PhD')
+ )
);
$this->parser->parse_string($template, $data);
- Result: Hello, John Doe (Mr{degree} {/degrees})
+ // Result: Hello, John Doe (Mr{degree} {/degrees})
-If you name one of your individual substitution parameters the same as one
-used inside a variable pair, the results
-may not be as expected::
+If you name one of your individual substitution parameters the same as one
+used inside a variable pair, the results may not be as expected::
$template = 'Hello, {firstname} {lastname} ({degrees}{degree} {/degrees})';
$data = array(
- 'degree' => 'Mr',
- 'firstname' => 'John',
- 'lastname' => 'Doe',
- 'degrees' => array(
- array('degree' => 'BSc'),
- array('degree' => 'PhD')
-
- )
+ 'degree' => 'Mr',
+ 'firstname' => 'John',
+ 'lastname' => 'Doe',
+ 'degrees' => array(
+ array('degree' => 'BSc'),
+ array('degree' => 'PhD')
+ )
);
$this->parser->parse_string($template, $data);
- Result: Hello, John Doe (Mr Mr )
+ // Result: Hello, John Doe (Mr Mr )
View Fragments
==============
-You do not have to use variable pairs to get the effect of iteration in
-your views. It is possible to use a view fragment for what would be inside
-a variable pair, and to control the iteration in your controller instead
+You do not have to use variable pairs to get the effect of iteration in
+your views. It is possible to use a view fragment for what would be inside
+a variable pair, and to control the iteration in your controller instead
of in the view.
An example with the iteration controlled in the view::
$template = '<ul>{menuitems}
- <li><a href="{link}">{title}</a></li>
- {/menuitems}</ul>';
+ <li><a href="{link}">{title}</a></li>
+ {/menuitems}</ul>';
+
$data = array(
- 'menuitems' => array(
- array('title' => 'First Link', 'link' => '/first'),
- array('title' => 'Second Link', 'link' => '/second'),
- )
+ 'menuitems' => array(
+ array('title' => 'First Link', 'link' => '/first'),
+ array('title' => 'Second Link', 'link' => '/second'),
+ )
);
$this->parser->parse_string($template, $data);
- Result:
- - First Link
- - Second Link
+Result::
+
+ <ul>
+ <li><a href="/first">First Link</a></li>
+ <li><a href="/second">Second Link</a></li>
+ </ul>
An example with the iteration controlled in the controller,
using a view fragment::
@@ -252,19 +253,24 @@ using a view fragment::
array('title' => 'First Link', 'link' => '/first'),
array('title' => 'Second Link', 'link' => '/second'),
);
- foreach ($data1 as $menuitem) {
- $temp .= $this->parser->parse_string($template1, $menuitem, TRUE);
+
+ foreach ($data1 as $menuitem)
+ {
+ $temp .= $this->parser->parse_string($template1, $menuitem, TRUE);
}
$template = '<ul>{menuitems}</ul>';
$data = array(
- 'menuitems' => $temp
+ 'menuitems' => $temp
);
$this->parser->parse_string($template, $data);
- Result:
- - First Link
- - Second Link
+Result::
+
+ <ul>
+ <li><a href="/first">First Link</a></li>
+ <li><a href="/second">Second Link</a></li>
+ </ul>
***************
Class Reference
@@ -290,8 +296,8 @@ Class Reference
:returns: Parsed template string
:rtype: string
- This method works exactly like ``parse()``, only it accepts
- the template as a string instead of loading a view file.
+ This method works exactly like ``parse()``, only it accepts
+ the template as a string instead of loading a view file.
.. method:: set_delimiters([$l = '{'[, $r = '}']])
@@ -299,5 +305,5 @@ Class Reference
:param string $r: Right delimiter
:rtype: void
- Sets the delimiters (opening and closing) for a
- pseudo-variable "tag" in a template. \ No newline at end of file
+ Sets the delimiters (opening and closing) for a
+ pseudo-variable "tag" in a template. \ No newline at end of file