Thursday, 24 July 2014

Twig Complete reference




- Developed By :  Fabien Potencier.

- Rquirements : PHP5.2.4

- Functionality : Twig parses templates and then outputs the result.

- Advantages:
    1) However, you can speed up the whole process by caching the compiled version in the form of plan PHP files to reduce the overhead.
    2) The framework is extensible, meaning that in case something you need is missing, you can create your own language constructs.

Installing the Twig:
----------------------------------

- Install the twig via Composer. The most common and recommended way is via Composer.
- Create the folder in your working directory

    Ex: $ mkdir work/twigtests

- If you are not installed the composer

    Run this in your terminal to get the latest Composer version:

    $ curl -sS https://getcomposer.org/installer | php
   
    Reference: https://getcomposer.org/download/

    Out put :
          #!/usr/bin/env php
      All settings correct for using Composer
      Downloading...
      Composer successfully installed to: /home/yanju/work/twigtests/composer.phar
      Use it: php composer.phar

- Next, step is to install the twig by the composer using following command.

    $ php composer.phar require "twig/twig:1.*"
    Out put:
        ./composer.json has been updated
        Loading composer repositories with package information
        Updating dependencies (including require-dev)
        - Installing twig/twig (v1.16.0)
            Loading from cache

        Writing lock file
        Generating autoload files

 In this step your successfully genereated the autoload file.

Twig Tutorials: -
-------------------------

    You want to very familiar about the twig concepts to build the templates.
    For Reference please find the following link.

        http://twig.sensiolabs.org/doc/intro.html ( or )
        http://twig.sensiolabs.org/pdf/Twig.pdf

Create a Sample Template :
-------------------------------------------------

    Create the 2 directories as templates, cache in your application directoy. In this example twigtests is the application directory

        $ mkdir work/twigtests/templates
        $ mkdir work/twigtests/cache

    give the write permissions on cache directory.

        $ sudo chmod -R 0777 /cache/twig

    and switch to the folder templates and create the 1 twig template and add some code. I done with the following code.

        {# I'm a comment and these are some examples #}

        {# Evaluates the variable bookTitle and print the result inside an h1  #}
        <h1>{{ bookTitle }}</h1>

        {# Evaluates the expression composed of the authorName variable and the string " De Rosa", concatenated by the "~" character #}
        <h2>{{ authorName ~ " De Rosa" }}</h2>

        {# Evaluates the variable count and change the resulting template accordingly #}
        {% if count < 1000 %}
           <p>This is a good book</p>
        {% else %}
           <p>This is a best seller</p>
        {% endif %}



How to load and show a template :
---------------------------------------------------------------
    Change the directory to your working directory and create the file as test1.php

        $ vi test1.php

    Remove everything inside and add this line to the top:

        <?php require_once '../vendor/autoload.php'; ?>

    With this line, we load the Composer autoloader which in turn loads Twig.
   
    If you installed Twig some other way, you’ll have to require the Twig autoloader directly:

        require_once '/path/to/lib/Twig/Autoloader.php';
        Twig_Autoloader::register();


    Now you want to initialize the twig :

        $twig = new Twig_Environment(
            new Twig_Loader_Filesystem('templates/'), array(
                      'cache' => 'cache/twig/',
                      'auto_reload' => true,
                      'strict_variables' => true
              )
        );


    The last step is to load the template and then render it, passing in the required variables.
    To load a template you have to call the loadTemplate() method and pass the path of the template as a parameter.

        $template = $twig->loadTemplate('test1.twig');
        echo $template->render(array('bookTitle' => 'Instant jQuery Selectors', 'bookAuthor' => 'Aurelio De Rosa'));


    or
        $twig->display('test1.twig', array('bookTitle' => 'Instant jQuery Selectors', 'bookAuthor' => 'Aurelio De Rosa'));

No comments:

Post a Comment