Niezalogowany (Zaloguj się)

Kategorie

Vanilla 1.0.3 jest produktem Lussumo. Więcej informacji: Dokumentacja, Forum.

    • CommentAuthoradmirau
    • CommentTimeDec 27th 2006 zmieniony
     
    Przykład wzięty ze strony www.damiangoc.republika.pl/phptal/
    Autor: Gucio

    Źródło szablonu: (my_template_file.html)

    <code lang="xml">
    <?xml version="1.0"?>
    <html>
    <head>
    <title tal:content="title">
    place for the page title
    </title>
    </head>
    <body>
    <h1 tal:content="title">sample title0</h1>
    <span tal:repeat="item link">
    <a href="http://www.foo.com" title="some foo link"
    tal:attributes="href item/href; title item/title"
    tal:content="item/text">sample text</a><br />
    </span>
    </body>
    </html>
    </code>

    Źródło skryptu: (index.php)

    <code lang="php">
    <?php
    $old_path = ini_get('include_path');
    // dla windows trzeba użyć ';' zamaist ':' w lini poniżej
    ini_set('include_path', $old_path.':/path/to/lib/with/PHPTAL-1.1.7');

    // katalog dla plików skompilowanych
    define('PHPTAL_PHP_CODE_DESTINATION', '/path/to/folder/_compile/');
    // wymuś kompilację za każdym razem
    define('PHPTAL_FORCE_REPARSE', 1);
    // katalog z szablonami
    define('PHPTAL_TEMPLATE_REPOSITORY', '/path/to/folder/_tpl/');

    require_once 'PHPTAL.php';

    // stworzenie nowego obiektu "szablon"
    $template = new PHPTAL('my_template_file.html');

    // Link class

    class Link {
    public $href;
    public $title;
    public $text;

    function Link($href, $title, $text) {
    $this->href = $href;
    $this->title = $title;
    $this->text = $text;
    }
    }

    // tworzymy tablicę elementów składających się na nasz link

    $link = array();
    $link[] = new Link("http://www.gogle.pl", "ano gogle.pl","ano gogle.pl" );
    $link[] = new Link("http://www.smog.pl", "ano smog.pl", "ano smog.pl");

    // put some data into the template context
    $template->title= 'generowanie linków';
    $template->link = $link;
    // execute the template
    try {
    echo $template->execute();
    }
    catch (Exception $e){
    echo $e;
    }
    ?>
    </code>

    Wynik:

    <code lang="xml">
    <?xml version="1.0"?>
    <html>
    <head>
    <title>generowanie linków</title>
    </head>
    <body>
    <h1>generowanie linków</h1>
    <span>
    <a href="http://www.gogle.pl" title="ano gogle.pl">ano gogle.pl</a><br/>
    </span><span>
    <a href="http://www.smog.pl" title="ano smog.pl">ano smog.pl</a><br/>
    </span>
    </body>
    </html>
    </code>