Use twig view engine

Update at 2017-12-22 07:58:28


In the three MVC components, the template acts as a V (view). Normally we often associate php with html to create a template like the following example

        <?php foreach ($users as $user) : ?>
            <tr><?php echo $user->id ?></tr>
            <tr><?php echo $user->fullname ?></tr>
        <?php endforeach ?>

The combination is not really bad, it is still very popular in many CMS and framework.

But besides using PHP with HTML to create the template, we can use some other template engine such as TWIG. The template engine in general will make the code in our template becomes clearer, easier to visualize. For example, with twig you can use the following

        {% for (user in users) %}
            <tr>{{ }}</tr>
            <tr>{{ user.fullname }}</tr>
        {% endfor %}

CI Base now has a built-in TWG engine, you just get to use it.

What is Twig?

Twig is a template engine, which makes the template more secure, limiting errors such as xss. At the same time, it also makes the template syntax clearer.

Details can be found here

Use Twig in CI Base

Twig has been integrated in the admin area. To use twig, when working with views you need to note:

  1. Let the extension of the view be .twig instead of .php as usual
  2. Use $this->view->render() instead of $this->load->view()

CI Base has a built-in view library, which automatically recognizes which browser you are using and therefore has the appropriate rendering engine.

You can find the view library at /admin/libraries/View.php

Note: View is a library that has been autoload, you do not need to reload

Note: View will prioritize the twig template before php