Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Uvod u TWIG - PHP template engine

DaFED
August 16, 2013

Uvod u TWIG - PHP template engine

DaFED #13
Speaker: Dejan Dexter Duduković, Eton Digital

DaFED

August 16, 2013
Tweet

More Decks by DaFED

Other Decks in Programming

Transcript

  1.  Tvorac twiga: Fabien Potencier ( Symfony )  Twig

    je moderan templejt sistem za PHP  Twig je – Fast , Flexible , Secure  Sve sto vam treba je dokumentacija.
  2.  Symfony  Drupal 8 ( in core ) 

    CodeIgniter ( Spark , extension )  Zend ( extension )  Laravel Twig ( bundles )  Magento 2  WordPress ( plugin )  ……
  3.  Lako !!!! - Composer (recommended ) - Git –

    ( development version ) - PEAR package
  4. Ucitavanje biblioteke: require_once 'vendor/twig/twig/lib/Twig/Autoloader.php'; Registrovanje loadera: Twig_Autoloader::register(); Kreiranje loader objekta

    – ( folder za template fajlove ) $loader = new Twig_Loader_Filesystem('templates/'); Krairanje twig objekta i setovanje konfiguracije $twig = new Twig_Environment($loader , array( 'cache' => 'cache', // opcija za kesiranje stranica 'debug' => true // debug mode u development fazi ));
  5. - Nadogradnja Twig funkcionalnosti kroz eksenzije $twig->addExtension( new Twig_Extension_Debug() );

    - Dodavanje ( PHP funkcija u twig ) $twig->addFunction( new Twig_SimpleFunction('krumo', ‘tKrumo') );
  6. Postoje 2 nacina za ucitavanje template fajlova. 1. Ucitavanje template

    fajla sa prosledjenim varijablama ( promenjive ) I smestanje HTML,Twig koda u varijablu. $data['content'] = $twig->render('users.twig' , $user ); 2. Ucitavanje temlate fajla i prikazivanje na stranici ( zavrsni korak ) $twig->display( 'template.twig' , $data );
  7. Variable: PHP: <?php echo $var ?> <?php echo htmlspecialchars($var, ENT_QUOTES,

    'UTF-8') ?> Twig: {{ variable }} {{ variable|escape }} Funkcije i logike: {% funkcija() %} {% for user in users %} * {{ user.name }} {% else %} No user have been found. {% endfor %}
  8.  Tags ( macro , autoescape , extends , set

    , include , filter )  Filters  Functions ( date , dump , random )  Operators  Test ( empty , even , null )