svn: /pear2/sandbox/Templates_IT/trunk/ examples/example1/footer.html examples/example1/test.html examples/example1/test.php
src/Templates/IT/Block.php src/Templates/IT/Template.php

From: Date: Tue, 16 Mar 2010 15:19:02 +0000
Subject: svn: /pear2/sandbox/Templates_IT/trunk/ examples/example1/footer.html examples/example1/test.html examples/example1/test.php
src/Templates/IT/Block.php src/Templates/IT/Template.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-core+get-9307@lists.php.net to get a copy of this message
gregorycu Tue, 16 Mar 2010 15:19:02 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=296281 Log: Added example. Small improvements to cache system. Changed paths: A pear2/sandbox/Templates_IT/trunk/examples/example1/ A pear2/sandbox/Templates_IT/trunk/examples/example1/footer.html A pear2/sandbox/Templates_IT/trunk/examples/example1/test.html A pear2/sandbox/Templates_IT/trunk/examples/example1/test.php U pear2/sandbox/Templates_IT/trunk/src/Templates/IT/Block.php U pear2/sandbox/Templates_IT/trunk/src/Templates/IT/Template.php Added: pear2/sandbox/Templates_IT/trunk/examples/example1/footer.html =================================================================== --- pear2/sandbox/Templates_IT/trunk/examples/example1/footer.html (rev 0) +++ pear2/sandbox/Templates_IT/trunk/examples/example1/footer.html 2010-03-16 15:19:02 UTC (rev 296281) @@ -0,0 +1,3 @@ +This is the footer. (It has been included from an additional template file.) + + Added: pear2/sandbox/Templates_IT/trunk/examples/example1/test.html =================================================================== --- pear2/sandbox/Templates_IT/trunk/examples/example1/test.html (rev 0) +++ pear2/sandbox/Templates_IT/trunk/examples/example1/test.html 2010-03-16 15:19:02 UTC (rev 296281) @@ -0,0 +1,40 @@ +<html> + <head> + <title>This is a test</title> + <style> + div { + margin: 8px 0px 0px 0px; + padding: 8px 8px 8px 8px; + } + div.person { + background-color: #fcc; + } + div.pet { + background-color: #cfc; + } + div.footer { + background-color: #ccf; + } + div.people_group { + background-color: #ffa; + } + .footer + </style> + </head> + <body> + <div class="people_group">This is the people in the house. (This is an instance of a people_group block). + <!-- BEGIN person --> + <div class="person">The persons name is "{name}" and they are {age} years old. (This is a instance of a person block) + <!-- BEGIN pet --> + <div class="pet">They have a pet, and the pets name is {name}. (This is an instance of a pet block)</div> + <!-- END pet --> + </div> + <!-- END person --> + </div> + <div class="footer"> + <!-- INCLUDE footer.html --> + </div> + </body> +</html> + + Added: pear2/sandbox/Templates_IT/trunk/examples/example1/test.php =================================================================== --- pear2/sandbox/Templates_IT/trunk/examples/example1/test.php (rev 0) +++ pear2/sandbox/Templates_IT/trunk/examples/example1/test.php 2010-03-16 15:19:02 UTC (rev 296281) @@ -0,0 +1,23 @@ +<?php + +require 'pear2/autoload.php'; +use pear2\Templates\IT\Template; + +$t = new Template(); +$t->loadTemplatefile('test.html',true,true); + +$person = $t->getBlock('person'); +$pet = $t->getBlock('pet'); + +$person->setVariable('name','greg'); +$person->setVariable('age','12'); +$pet->setVariable('name','fred'); + +$person->parse(); + +$person->setVariable('name','bob'); +$person->setVariable('age','30'); + +$t->show(); + +?> Modified: pear2/sandbox/Templates_IT/trunk/src/Templates/IT/Block.php =================================================================== --- pear2/sandbox/Templates_IT/trunk/src/Templates/IT/Block.php 2010-03-16 15:15:54 UTC (rev 296280) +++ pear2/sandbox/Templates_IT/trunk/src/Templates/IT/Block.php 2010-03-16 15:19:02 UTC (rev 296281) @@ -1,6 +1,6 @@ <?php -namespace PEAR2\Templates\IT; +namespace pear2\templates\IT; class Block { Modified: pear2/sandbox/Templates_IT/trunk/src/Templates/IT/Template.php =================================================================== --- pear2/sandbox/Templates_IT/trunk/src/Templates/IT/Template.php 2010-03-16 15:15:54 UTC (rev 296280) +++ pear2/sandbox/Templates_IT/trunk/src/Templates/IT/Template.php 2010-03-16 15:19:02 UTC (rev 296281) @@ -1,6 +1,6 @@ <?php -namespace PEAR2\Templates\IT; +namespace pear2\templates\it; class Template { @@ -10,17 +10,17 @@ const VARIABLENAME_REGEXP = '[\.0-9A-Za-z_-]+'; const BLOCKNAME_REGEXP = '[\.0-9A-Za-z_-]+'; const GLOBAL_BLOCK = ''; + const CACHE_FOLDER = '.cache'; + const CACHE_FILE_SUFFIX = '.spo'; + const USE_CACHE = true; - function setOption($options) { - - + function setUseCache($value = self::USE_CACHE) { + $this->useCache = $value; } - function __construct($root = '.', $options = null) { + function __construct($root = '.') { - if ($options !== null) { - $this->setOption($options); - } + $this->rootDirectory = $root; $this->globalnstances = array(); $this->globalBlock = new Block($this,'',$this); @@ -30,33 +30,48 @@ . ')\s+-->(.*)<!--\s+END\s+\1\s+-->|' . '(\{(' . self::VARIABLENAME_REGEXP . ')\})@sm'; - $this->rootDirectory = $root; + + $this->cacheFolder = self::CACHE_FOLDER; + $this->cacheFileSuffix = self::CACHE_FILE_SUFFIX; + $this->useCache = self::USE_CACHE; } public function loadTemplateFile($filename, $removeUnknownVariables = true, $removeEmptyBlocks = true) { + $root_directory = $this->rootDirectory; + $cache_folder = $this->cacheFolder; + + $getCacheFilename = function ($filename) use ($root_directory, $cache_folder) { + return $root_directory.DIRECTORY_SEPARATOR.$cache_folder.DIRECTORY_SEPARATOR.$filename; + }; + $filename = $this->rootDirectory . DIRECTORY_SEPARATOR . $filename; - - $cache_directory = $this->rootDirectory . DIRECTORY_SEPARATOR . ".cache"; - mkdir($cache_directory); + $cache_directory = $getCacheFilename(""); + if (!file_exists($cache_directory)) { + mkdir($cache_directory); + } - $cache_filename = $cache_directory . DIRECTORY_SEPARATOR . $filename; + $cache_filename = $getCacheFilename($filename); - $load_from_file = true; - if (file_exists($filename.".cache") && is_readable($filename.".cache")) { - if (filemtime($filename) < filemtime($filename.".cache")) { - $load_from_file = false; - $global = unserialize(file_get_contents($filename.".cache")); + $reload_from_file = true; + $use_cache = true; + + if (file_exists($cache_filename) && is_readable($cache_filename)) { + if (filemtime($filename) < filemtime($cache_filename)) { + $reload_from_file = true; + $global = unserialize(file_get_contents($filename)); foreach ($global->included_files as $file) { - if (filemtime($file) > filemtime($filename.".cache")) { - $load_from_file = true; + if (filemtime($file) > filemtime($filename)) { + $reload_from_file = false; } } } + } else { + $reload_from_file = true; } - if ($load_from_file) { + if ($reload_from_file) { $contents = file_get_contents($filename); $sub_round; @@ -68,7 +83,11 @@ if (!in_array($m[1],$included_files)) { $included_files[] = $m[1]; } - return file_get_contents($m[1]); + if (file_exists($m[1])) { + return file_get_contents($m[1]); + } else { + return ''; + } }; $sub_round = 1; @@ -85,7 +104,7 @@ $this->globalBlock->parent = null; $this->globalBlock->included_files = $included_files; - file_put_contents($cache_filename.".cache",serialize($this->globalBlock)); + file_put_contents($cache_filename.".spo",serialize($this->globalBlock)); } else { $this->globalBlock = $global; } @@ -116,7 +135,7 @@ } public function getBlock($name) { - return $this->globalBlock; + return $this->globalBlock->findBlock($name); }

« previous php.pear.core (#9307) next »