Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions HTML/Template/IT.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,12 @@ class HTML_Template_IT
var $_options = array(
'preserve_data' => false,
'use_preg' => true,
'preserve_input'=> true
'preserve_input'=> true,
'check_placeholder_exists' => true,
);

public static $globalOptions = [];

/**
* Builds some complex regular expressions and optinally sets the
* file root directory.
Expand All @@ -407,8 +410,11 @@ class HTML_Template_IT
* @see setRoot()
* @access public
*/
function HTML_Template_IT($root = '', $options = null)
function __construct($root = '', $options = null)
{
if (!empty(self::$globalOptions)) {
$this->setOptions(self::$globalOptions);
}
if (!is_null($options)) {
$this->setOptions($options);
}
Expand All @@ -427,6 +433,13 @@ function HTML_Template_IT($root = '', $options = null)
$this->setRoot($root);
} // end constructor

/**
* Dont break the php4 constructor
*/
function HTML_Template_IT($root = '', $options = null)
{
self::__construct($root, $options);
}

/**
* Sets the option for the template class
Expand Down Expand Up @@ -482,6 +495,16 @@ function setOptions($options)
return IT_OK;
}

/**
* Define global options used for all new instances.
* options defined using constructor parameter will overwrite
* globalOptions
*/
public static function setGlobalOptions($options)
{
self::$globalOptions = $options;
}

/**
* Print a certain block with all replacements done.
*
Expand Down Expand Up @@ -767,7 +790,7 @@ function setVariable($variable, $value = '')
$this->setVariable($key, $value);
}
} else {
if ($this->checkPlaceholderExists($this->currentBlock, $variable)) {
if (!$this->_options['check_placeholder_exists'] || $this->checkPlaceholderExists($this->currentBlock, $variable)) {
$this->variableCache[$variable] = $value;
}
}
Expand Down Expand Up @@ -1065,7 +1088,7 @@ function findBlocks($string)
*/
function getFile($filename)
{
if ($filename{0} == '/' && substr($this->fileRoot, -1) == '/') {
if ($filename[0] == '/' && substr($this->fileRoot, -1) == '/') {
$filename = substr($filename, 1);
}

Expand All @@ -1088,9 +1111,13 @@ function getFile($filename)
$content = fread($fh, $fsize);
fclose($fh);

return preg_replace(
"#<!-- INCLUDE (.*) -->#ime",
"\$this->getFile('\\1')",
$callback = function($matches) {
return $this->getFile($matches[1]);
};

return preg_replace_callback(
"#<!-- INCLUDE (.*) -->#im",
$callback,
$content
);
} // end func getFile
Expand Down Expand Up @@ -1183,4 +1210,3 @@ function errorMessage($value, $blockname = '')
$errorMessages[$value] : $errorMessages[IT_ERROR];
}
} // end class IntegratedTemplate
?>
26 changes: 17 additions & 9 deletions HTML/Template/ITX.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class HTML_Template_ITX extends HTML_Template_IT
* @access public
* @see HTML_Template_IT()
*/
function HTML_Template_ITX($root = '')
function __construct($root = '')
{

$this->checkblocknameRegExp = '@' . $this->blocknameRegExp . '@';
Expand All @@ -147,6 +147,14 @@ function HTML_Template_ITX($root = '')
$this->HTML_Template_IT($root);
} // end func constructor

/**
* Dont break the php4 constructor
*/
function HTML_Template_ITX($root = '')
{
self::__construct($root);
}

/**
* Clears all datafields of the object and rebuild the internal blocklist
*
Expand Down Expand Up @@ -311,7 +319,7 @@ function addBlock($placeholder, $blockname, $template)
} elseif (count($parents) > 1) {

reset($parents);
while (list($k, $parent) = each($parents)) {
foreach ($parents as $k => $parent) {
$msg .= "$parent, ";
}
$msg = substr($parent, -2);
Expand Down Expand Up @@ -397,7 +405,7 @@ function placeholderExists($placeholder, $block = '')
if (is_array($variables = $this->blockvariables[$block])) {
// search the value in the list of blockvariables
reset($variables);
while (list($k, $variable) = each($variables)) {
foreach ($variables as $k => $variable) {
if ($k == $placeholder) {
$found = $block;
break;
Expand All @@ -409,7 +417,7 @@ function placeholderExists($placeholder, $block = '')
// search all blocks and return the name of the first block that
// contains the placeholder
reset($this->blockvariables);
while (list($blockname, $variables) = each($this->blockvariables)) {
foreach ($this->blockvariables as $blockname => $variables) {
if (is_array($variables) && isset($variables[$placeholder])) {
$found = $blockname;
break;
Expand All @@ -430,7 +438,7 @@ function placeholderExists($placeholder, $block = '')
function performCallback()
{
reset($this->functions);
while (list($func_id, $function) = each($this->functions)) {
foreach ($this->functions as $func_id => $function) {
if (isset($this->callback[$function['name']])) {
if ($this->callback[$function['name']]['expandParameters']) {
$callFunction = 'call_user_func_array';
Expand Down Expand Up @@ -696,7 +704,7 @@ function buildFunctionlist()
while ($head != '' && $args2 = $this->getValue($head, ',')) {
$arg2 = trim($args2);

$args[] = ('"' == $arg2{0} || "'" == $arg2{0}) ?
$args[] = ('"' == $arg2[0] || "'" == $arg2[0]) ?
substr($arg2, 1, -1) : $arg2;

if ($arg2 == $head) {
Expand Down Expand Up @@ -782,7 +790,7 @@ function deleteFromBlockvariablelist($block, $variables)
}

reset($this->blockvariables[$block]);
while (list($varname, $val) = each($this->blockvariables[$block])) {
foreach ($this->blockvariables[$block] as $varname => $val) {
if (isset($variables[$varname])) {
unset($this->blockvariables[$block][$varname]);
}
Expand Down Expand Up @@ -840,10 +848,10 @@ function findPlaceholderBlocks($variable)
{
$parents = array();
reset($this->blocklist);
while (list($blockname, $content) = each($this->blocklist)) {
foreach ($this->blocklist as $blockname => $content) {
reset($this->blockvariables[$blockname]);

while (list($varname, $val) = each($this->blockvariables[$blockname])) {
foreach ($this->blockvariables[$blockname] as $varname => $val) {
if ($variable == $varname) {
$parents[] = $blockname;
}
Expand Down
12 changes: 10 additions & 2 deletions HTML/Template/IT_Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ class IT_Error extends PEAR_Error
* @param string $file file where the error occured
* @param string $line linenumber where the error occured
*/
function IT_Error($msg, $file = __FILE__, $line = __LINE__)
function __construct($msg, $file = __FILE__, $line = __LINE__)
{
$this->PEAR_Error(sprintf("%s [%s on line %d].", $msg, $file, $line));
} // end func IT_Error


/**
* Dont break the php4 constructor
*/
function IT_Error($msg, $file = __FILE__, $line = __LINE__)
{
self::__construct($msg, $file, $line);
}

} // end class IT_Error
?>
9 changes: 5 additions & 4 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ Please report all new issues via the PEAR bug tracker.

If this package is marked as unmaintained and you have fixes, please submit your pull requests and start discussion on the pear-qa mailing list.

To test, run either
$ phpunit tests/
or
$ pear run-tests -r
To test, run

$ composer install
$ php vendor/bin/phpunit


To build, simply
$ pear package
Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@
"./"
],
"license": "Modified BSD license",
"name": "pear/html_template_it",
"name": "elma/html_template_it",
"support": {
"issues": "http://pear.php.net/bugs/search.php?cmd=display&package_name[]=HTML_Template_IT",
"source": "https://github.com/pear/HTML_Template_IT"
"source": "https://github.com/Elma/HTML_Template_IT"
},
"type": "library",
"require": {
"pear/pear_exception": "*"
},
"require-dev": {
"phpunit/phpunit": "*"
"phpunit/phpunit": "^9.4"
},
"extra": {
"branch-alias": {
"dev-v1.4.0-rc": "1.4.x-dev"
}
}
}
}
10 changes: 10 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="./vendor/autoload.php">

<testsuites>
<testsuite name="html_template_it">
<directory>./tests</directory>
</testsuite>
</testsuites>

</phpunit>
8 changes: 4 additions & 4 deletions tests/AllTests.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
if (!defined('PHPUnit_MAIN_METHOD')) {
define('PHPUnit_MAIN_METHOD', 'HTML_Template_IT_AllTests::main');
define('PHPUnit_MAIN_METHOD', 'AllTests::main');
}

require_once 'PHPUnit/TextUI/TestRunner.php';
Expand All @@ -11,7 +11,7 @@
require_once 'ITXTest.php';


class HTML_Template_IT_AllTests
class AllTests
{
public static function main()
{
Expand All @@ -30,7 +30,7 @@ public static function suite()
}
}

if (PHPUnit_MAIN_METHOD == 'HTML_Template_IT_AllTests::main') {
HTML_Template_IT_AllTests::main();
if (PHPUnit_MAIN_METHOD == 'AllTests::main') {
AllTests::main();
}
?>
13 changes: 7 additions & 6 deletions tests/ITTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<?php
require_once 'HTML/Template/IT.php';
require_once 'PHPUnit/Framework/TestCase.php';

class ITTest extends PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase;

class ITTest extends TestCase
{
/**
* An HTML_Template_IT object
* @var object
*/
var $tpl;

function setUp()
function setUp(): void
{
$this->tpl = new HTML_Template_IT(dirname(__FILE__) . '/templates');
}

function tearDown()
function tearDown(): void
{
unset($this->tpl);
}
Expand Down Expand Up @@ -391,9 +392,9 @@ public function testShouldSetOptionsCorrectly() {
}


public function testPlaceholderReplacementScope() {
public function testPlaceholderReplacementScope() {
$result = $this->tpl->loadTemplateFile('placeholderreplacementscope.html', true, true);

if (PEAR::isError($result)) {
$this->fail('Error loading template file: ' . $result->getMessage());
}
Expand Down
7 changes: 3 additions & 4 deletions tests/ITXTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
require_once 'HTML/Template/ITX.php';
require_once 'PHPUnit/Framework/TestCase.php';

require_once 'ITTest.php';

Expand All @@ -11,20 +10,20 @@ function _uppercaseCallback($ary)

class Callbacks
{
function _lowercaseCallback($ary)
public static function _lowercaseCallback($ary)
{
return strtolower($ary[0]);
}

function _numberFormatCallback($float, $decimals)
public static function _numberFormatCallback($float, $decimals)
{
return number_format($float, $decimals);
}
}

class ITXTest extends ITTest
{
function setUp()
function setUp(): void
{
$this->tpl = new HTML_Template_ITX(dirname(__FILE__) . '/templates');
}
Expand Down