Zaq is a PHP based template parser engine developed to work with Codeigniter. This library has been developed for developers to integrate php codes in views easily. Using this library will also allow the view file to be more readable. View files in Codeigniter (or in any other framework following MVC) always contains both html and php codes which make them a bit harder to read. This problem can be eradicated by using a parser engine which makes the view files a lot more easier to work with.
Codeigniter, by default, comes with an optional template parser. But, unfortunately, that one does not provide sufficient pseudo markup to work with. Zaq, without doubt, is able to provide much more flexibility while building view files with pseudo markup to replace php codes.
Let’s take a look at Zaq’s insight and usage.
- Download Zaq
- Create the folder if not exists:
Like other libraries in CodeIgniter, the Zaq library class is initialized in your controller using the
Or you can autoload the library in
Once loaded, the Zaq library object will be available using:
You can use the
parse() method to parse (or render) your views or templates. The syntax is:
The first parameter contains the name of the view file, the second parameter contains an associative array of data to be made available in the template, and the third parameter specify whether to return the parsed string.
There is no need to “echo” or do something with the data returned by
$this->zaq->parse(). It is automatically passed to the output class to be sent to the browser. However, if you do want the data returned instead of sent to the output class you can pass TRUE (boolean) as the third parameter:
The pseudo markup of Zaq is much more cleaner than its equivalent php code. It enhance the readability and maintainability of the code. Designer will also be cheerful to work with a cleaner file.
From the above example, we are creating a template file at
The html output will be:
To achieve the same output by php code:
Use foreach for associative array
All variables and methods will be automatically preceded by
To use constants, precede the constant with #:
How Zaq Works
- Parse template and convert pseudo-markup to valid php code.
- Store the code in a temporary file in its cache folder (configurable).
- Load view from the temporary folder.
- Delete the temporary file (configurable).
The configuration file,
zaq.php, should be located in
There are two configuration settings.
cache_dirsetting tells Zaq where to store the temporary files.
enable_cache setting is set to
TRUE, Zaq will keep the temporary files and will skip the parsing process next time if the parsed file is newer than the view file.
There are several helper methods to ease the application development a bit more.
Sets the delimiters (opening and closing) for a pseudo-markup “tag” in a template.
- $left_delimiter (String) – Left delimiter
- $right_delimiter (String) – Right delimiter
Add exception to list of pseudo-markups.
- $val (String) – The string to skip parsing (without delimiters)
memory_usage have been added to the list of exceptions as these are Codeigniter’s default pseudo-markup tag.