Manual

4. Interfejs PHPTAL_Trigger

Atrybut phptal:id został dodany do systemu PHPTAL dla potrzeb PHP5 w celu zastąpienia starego interfejsu PHPTAL_Cache i wprowadzenia w nim drobnych poprawek.

Gdy phptal:id zostanie osiągnięty, PHPTAL będzie szukał go na liście triggerów na podstawie id i wykona metody start() i end() przed i po wejściu do elementu.

Jeśli metoda PHPTAL_Trigger::start() zwróci PHPTAL_Trigger::SKIPTAG, PHPTAL zignoruje element i jego zawartość (start() może wyświetlić coś aby ją podmienić).

Jeśli Twój trigger będzie chciał wykonać zawartość w elemencie, zwróci PHPTAL_Trigger::PROCEED.

Metoda PHPTAL_Trigger::end() zostanie wykonana za elementem (niezależnie czy był wykonany czy nie). To umożliwia zbudowanie systemu cache'u korzystając z funkcji ob_start() w metodzie start() i ob_get_contents(), ob_end_clean() w end().

<html>
  ...
  <div>
    ...
    foo bar baz <span tal:replace="id"/> foo bar baz
    ...
  </div>
  ...
</html>

Z pewnych powodów chcemy aby blok div był cache'owany. Wygenerujemy phptal:id w szablonie :

<html>
  ...
  <div phptal:id="somePossiblyUniqueKeyword">
    ...
    foo bar baz <span tal:replace="id"/> foo bar baz
    ...
  </div>
  ...
</html>

Następnie napiszemy trigger, który będzie cache'ował zawartość bloku div:

<?php
require_once 'PHPTAL.php';
require_once 'PHPTAL/Trigger.php';

class CacheTrigger implements PHPTAL_Trigger
{
    public function start($phptalid, $tpl)
    {
        // this cache depends on 'id' which must appears in
        // the template execution context
        $this->_cachePath = 'cache.' . $tpl->getContext()->id;

        // if already cached, read the cache and tell PHPTAL to
        // ignore the tag content
        if (file_exists($this->_cachePath)){
            $this->_usedCache = true;
            readfile($this->_cachePath);
            return self::SKIPTAG;
        }
        
        // no cache found, we start an output buffer and tell
        // PHPTAL to proceed (ie: execute the tag content)
        $this->_usedCache = false;
        ob_start();
        return self::PROCEED;
    }

    // Invoked after tag execution
    public function end($phptalid, $tpl)
    {
        // end of tag, if cached file used, do nothing
        if ($this->_usedCache){
            return;
        }

        // otherwise, get the content of the output buffer
        // and write it into the cache file for later usage
        $content = ob_get_contents();
        ob_end_clean();
        echo $content;

        $f = fopen($this->_cachePath, 'w');
        fwrite($f, $content);
        fclose($f);
    }

    private $_cachePath;
    private $_usedCache;
}
?>

Najważniejsze jest zwrócenie przez metodę start() stałych SKIPTAG lub PROCEED.

Gdy zwrócony będzie SKIPTAG, PHPTAL po prostu zignoruje znacznik i wykona metodę end(). Zazwyczaj oznacza to, że trigger This usually means that the trigger takes the hand in deciding what to show there.

Gdy zwrócony będzie PROCEED, PHPTAL wykona zawartość znacznika i wykona metodę end(). To umożliwia naszej klasie cache This allows our cache class to play with output buffers to execute the tag once and to store the result in a file which will be used in later calls.

To install our trigger we use :

<?php
require_once 'PHPTAL.php';
require_once 'CacheTrigger.php'; // our custom trigger

$trigger = new CacheTrigger();

$tpl = new PHPTAL('test.html');
    
// this trigger will only be called for phptal:id="triggerId"
$tpl->addTrigger('somePossiblyUniqueKeyword', $trigger);

$tpl->id = 1;

echo $tpl->execute();

?>

Możesz dodawać dowolną ilość trigerów w swoich szablonach. Wbudowany trigger cache może obslugiwać więcej niż jedno phptal:id.