Twig "for loop" in CodeIgniter in PHP

<?php defined('BASEPATH') or exit('No direct script access allowed');

class Welcome extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->library("twig");

    }

    public function forLoop()
    {
        $data = array(
            "title" => "Welcome to Twig",
            "title2" => "Hello Welcome to Twig, we are from forLoop function",
            "title3" => "For Loop",
            "title4" => "Date",
            "title5" => "Current Date is : ",
            "items" => array(
                array("name"=>"Item 1 is Present", "description"=>"This is my Item 1"),
                array("name"=>"Item 2 is Present", "description"=>"This is my Item 2"),
            )
        );
        $this->twig->render("for_loop.twig", $data);
        // $this->twig->render("date.twig", $data);
    }

} 




TWIG FILE IN VIEW

<?php defined("BASEPATH") OR exit("No direct script access allowed"); ?>

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <title>{{ title3 }}</title>
    <body>
        <h1>{{ title }}</h1>
        <ul>
            {% for i in 0..10 %}
                * {{ i }}
            {% endfor %}

            <br><br>
            {% for letter in "a".."z" %}
                * {{ letter }}
            {% endfor %}
            <br><br>

            {% for item in items %}
                <li>
                    <strong>{{ item.name }}</strong> : {{ item.description }}
                </li>
            {% else %}
                <li>No items Found</li>
            {% endfor %}

            {% for letter in "a"|upper.."z"|upper %}
                * {{ letter }}
            {% endfor %}

            <br><br>
            {% for item in items %}
                {{ loop.index }} - {{ user.username }}
            {% endfor %}
        </ul>
    </body>
</html>


Comments