Uživatelské nástroje

Nástroje pro tento web


twig

Twig

Šablonovací systém pro PHP.

Twig home page: http://twig.sensiolabs.org/

Demo examples: http://twig.isn.cz/

Požadavky

  • pro Twig 1.13.2 miminálně PHP 5.2.4

Instalace pomocí Composeru

Protože mám v konfiguraci PHP (/etc/php.ini) volbu allow_url_fopen=Off, tak jsem do PHP CLI povolil hodnotu On pro stažení a instalaci.

Stažení

shell# mkdir -p /data/install/twig
shell# cd /data/install/twig
shell# curl -s http://getcomposer.org/installer | php -d allow_url_fopen=On

Vytvoření konfiguračního souboru pro Composer

composer.json
{
    "require": {
        "twig/twig": "1.*"
    }
}

Instalace přes Composer

shell# php -d allow_url_fopen=On composer.phar install

Příklady použití

Přejít do adresáře projektu.

shell# mkdir lib cache templates css
shell# cp -r /data/install/twig/vendor/twig/twig/lib/Twig ./lib

First Page

Demo example page: http://twig.isn.cz/first.php

"templates/base.twig"
<!DOCTYPE html>
<html>
    <head>
        {% block htmlHead %}
        <title>
            {% block titleHead %}
            {% endblock %}
        </title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="./css/style.css" />
        {% endblock %}
    </head>
    <body>
        <div id="content">
            {% block content %}
            {% endblock %}
        </div>
        <div id="footer">
            {% block footer %}
            Demo created by Jiří Blažek (jiri.blazek@isn.cz) | <a href="https://www.isn.cz">ISN.CZ</a>
            {% endblock %}
        </div>
    </body>
</html>
"templates/first.twig"
{% extends "base.html" %}
{# comment: include meta tags into block htmlHead #} 
{% block htmlHead %}
{# comment: include content of htmlHead block of parent #}
{{ parent() }}
<meta name="author" content="Jiří Blažek <jiri.blazek@isn.cz>" />
<meta name="Keywords" content="twig" />
<meta name="Description" content="Twig Demo Examples" />
<meta name="Robots" content="all|index|follow" />
{% endblock %}
 
{% block titleHead %}
{{ firstPageTitle }}
{% endblock %}
 
{% block content %}
<a href="index.php">&larr; Back to Demo Examples list of Twig</a>
 
<br /><br /><strong>Url doc: </strong><a href="{{ firstPageDocUrl }}">{{ firstPageDocUrl }}</a>
 
<br /><br />This is example text in content div.
{% endblock %}
"first.php"
<?php
 
/*
 * This file is demo of Twig.
 *
 * Author: Jiří Blažek <jiri.blazek@isn.cz>
 * Doc: https://www.isn.cz/kb/doku.php/twig#first-page
 * 
 */
 
require_once './lib/Twig/Autoloader.php';
Twig_Autoloader::register();
 
// loading templates, path to templates
$loader = new Twig_Loader_Filesystem('./templates');
 
// environment options
$twig = new Twig_Environment($loader, array(
    'cache' => './cache', // path to compilation cache, default to false (disable caching)
    'debug' => false, // default to false
    'charset' => 'utf-8', // default to utf-8
    'base_tamplate_class' => 'Twig_Template', // default to Twig_Template 
    'auto_reload' => false, // default to false
    'strict_variables' => false, // default to false, false = Twig will silently ignore invalid variables (variables and or attributes/methods that do not exist) and replace them with a null value, true = Twig throws an exception instead
    'autoescape' => 'html', // default to true, true = auto-escaping will be enabled by default for all templates, false = disable, or scaping strategy to use (html, jscss, url, html_attr) or callback
    'optimizations' => -1 // default to -1, -1 = all optimizations are enabled, 0 = disabled
        )
);
 
// load template first.html
$template = $twig->loadTemplate('first.twig');
 
// render page with this variables
echo $template->render(array('firstPageTitle' => 'First Page', 'firstPageDocUrl' => 'https://www.isn.cz/kb/doku.php/twig#first-page'));
 
?>
"css/style.css"
root { 
    display: block;
}
 
#content {
    width: 90%;
    min-height: 500px;
    border-top: 2px solid #000000;
}
 
#footer {
    width: 90%;
    min-height: 100px;
    border-top: 2px solid #000000;
}
twig.txt · Poslední úprava: 2023/12/26 19:13 autor: 127.0.0.1