←    Back

How to use Zaq: Codeigniter Template Parser Engine

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.

Installation

  1. Download Zaq
  2. Copy libraries/Zaq.php to your application/libraries/ folder
  3. Copy config/zaq.php to your application/config/ folder.
  4. Create the folder if not exists: application/cache
  5. Set application/cache writable.

Initialization

Like other libraries in CodeIgniter, the Zaq library class is initialized in your controller using the

method:


Or you can autoload the library in autoload.php

Once loaded, the Zaq library object will be available using: $this->zaq

Parsing Views/Templates

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.

Example:


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:

Pseudo Markup

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.

foreach:

From the above example, we are creating a template file at products/list.php:


The html output will be:

To achieve the same output by php code:

Use foreach for associative array

if/elseif/else:

echo:

All variables and methods will be automatically preceded by echo:

constants:

To use constants, precede the constant with #:

How Zaq Works

  1. Parse template and convert pseudo-markup to valid php code.
  2. Store the code in a temporary file in its cache folder (configurable).
  3. Load view from the temporary folder.
  4. Delete the temporary file (configurable).

Configuration

The configuration file, zaq.php, should be located in application/config/.

There are two configuration settings.


The cache_dir setting tells Zaq where to store the temporary files.

If 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.

Helper Methods

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.

Parameters:

  • $left_delimiter (String) – Left delimiter
  • $right_delimiter (String) – Right delimiter

Return: void


Add exception to list of pseudo-markups.

Parameters:

  • $val (String) – The string to skip parsing (without delimiters)

Return: void

By default, elapsed_time and memory_usage have been added to the list of exceptions as these are Codeigniter’s default pseudo-markup tag.


The Author

Other Articles

  • Category Pagination in Jekyll

    This site is built on Jekyll – a blog aware static site generator. When I was building this, I wanted to separate my blog from my portfolio. Of course I would be using custom post type for my portfolio if I was developing on Wordpress. Here I thought to stay straight and simple and I just created two site categories: portfolio and blog and I am using custom permalink which makes it difficult to use the default pagination setup. And I also want to paginate only the blog category.

  • 15 Best Bootstrap Alternatives for Web Developers

    Choosing the right framework that is the perfect fit for your projects could be a little bit overwhelming – there are a lot to choose from. Perhaps, you want to go with popular choices like Bootstrap or Foundation, but if your website is going to be a fairly simple one, you won’t be needing most of the building blocks and materials included in the default package. The good news is that there are a handful of alternatives that are much leaner than Bootstrap or Foundation. Most of these frameworks ship with just the right amount of styles and components to help you get started, while allowing you to be able to extend them in the direction you want for your project.

  • 8 Icon Fonts to speed up Website Design Process

    Icon Fonts are nothing but fonts. But, instead of containing letters or numbers, they contain symbols and shapes. You can style them with CSS in the same way you style regular text. They’re ideal for small, frequently used shapes such as email, envelopes, telephones, widget controls and social media logos. Here is a list of most popular and easy-to-use icon font sets.

  • Download ZIP File Dynamically with PHP

    Here we will see how we can make a webpage act as an initializer to download a zip file. We will just provide the location of the file and PHP will download it to the user. In the back-end, the HTTP headers are responsible for the download. We will set the headers with PHP.

  • How to Add Scalable Vector Graphics (SVG) to Web Page

    Scalable Vector Graphics (SVG) is an XML-based vector image format for two-dimensional graphics with support for interactivity and animation. If you want to embed your hand-crafted SVG in your web page, you can use any of the methods discussed here.

  • Create File Upload Class in PHP

    The basic process of providing a HTML form for uploading user submitted files to the server with PHP is fairly easy and simple. But there are some security implications that many of us are unaware of. We will be building a custom PHP class for secure file upload. This class will check the type and size of the file and rename the file in case of duplication.