fix #2 and directly trim on the global regex
This commit is contained in:
parent
d02867eb5d
commit
018785867f
|
@ -65,12 +65,15 @@ class TplBlock {
|
||||||
$this->subBlocs[$bloc->name][] = $bloc;
|
$this->subBlocs[$bloc->name][] = $bloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function subBlockRegex($prefix, $blocName) {
|
private function subBlockRegex($prefix, $blocName,$trim = true) {
|
||||||
|
echo "t".$trim;
|
||||||
return '/'
|
return '/'
|
||||||
. self::blockStartStart
|
. self::blockStartStart
|
||||||
. preg_quote($prefix . $blocName)
|
. preg_quote($prefix . $blocName)
|
||||||
. self::blockStartEnd
|
. self::blockStartEnd
|
||||||
|
. (($trim === false)? '' : '(?:\R|)?' )
|
||||||
. '(.*?)'
|
. '(.*?)'
|
||||||
|
. (($trim === false)? '' : '(?:\R|)?' )
|
||||||
. self::blockEndStart
|
. self::blockEndStart
|
||||||
. preg_quote($prefix . $blocName)
|
. preg_quote($prefix . $blocName)
|
||||||
. self::blockEndEnd
|
. self::blockEndEnd
|
||||||
|
@ -78,7 +81,8 @@ class TplBlock {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Shake template and input vars and returns the text
|
* Shake the template string and input vars
|
||||||
|
* Then returns the parsed text
|
||||||
* Input:
|
* Input:
|
||||||
* $str String containing the template to parse
|
* $str String containing the template to parse
|
||||||
* $subBlocsPath String optional, for this class internal use. The path like "bloc.subbloc"
|
* $subBlocsPath String optional, for this class internal use. The path like "bloc.subbloc"
|
||||||
|
@ -100,7 +104,7 @@ class TplBlock {
|
||||||
//parse blocs
|
//parse blocs
|
||||||
foreach($this->subBlocs as $blocName => $blocsArr){
|
foreach($this->subBlocs as $blocName => $blocsArr){
|
||||||
$str = preg_replace_callback(
|
$str = preg_replace_callback(
|
||||||
$this->subBlockRegex($prefix, $blocName),
|
$this->subBlockRegex($prefix, $blocName, $trim),
|
||||||
function($m) use($blocName,$blocsArr,$prefix, $trim) {
|
function($m) use($blocName,$blocsArr,$prefix, $trim) {
|
||||||
$out = "";
|
$out = "";
|
||||||
foreach($blocsArr as $bloc){
|
foreach($blocsArr as $bloc){
|
||||||
|
@ -115,12 +119,8 @@ class TplBlock {
|
||||||
|
|
||||||
// Delete unused blocs
|
// Delete unused blocs
|
||||||
$str = preg_replace($this->unusedRegex, "", $str);
|
$str = preg_replace($this->unusedRegex, "", $str);
|
||||||
|
return $str;
|
||||||
if($trim){
|
|
||||||
return trim($str,"\n");
|
|
||||||
}else{
|
|
||||||
return $str;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -44,4 +44,4 @@ for ($i = 2; $i < 121; $i++){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo $tpl->apply_tpl_file("tpl.txt",true);
|
echo $tpl->apply_tpl_file("tpl.txt");
|
15
test-TplBlock/sample.txt
Normal file
15
test-TplBlock/sample.txt
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
hello {{title}} {{name}} {{firstname}}
|
||||||
|
|
||||||
|
The first primes numbers are:
|
||||||
|
<!-- BEGIN primes -->
|
||||||
|
- {{primes.number}}
|
||||||
|
<!-- END primes -->
|
||||||
|
|
||||||
|
Lets list all divisors of:
|
||||||
|
|
||||||
|
<!-- BEGIN number -->
|
||||||
|
{{number.value}} Have those divisors:
|
||||||
|
<!-- BEGIN number.divisor -->
|
||||||
|
- {{number.divisor.value}}
|
||||||
|
<!-- END number.divisor -->
|
||||||
|
<!-- END number -->
|
47
test-TplBlock/test.php
Normal file
47
test-TplBlock/test.php
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
include("../class.TplBlock.php");
|
||||||
|
|
||||||
|
$tpl = new TplBlock();
|
||||||
|
|
||||||
|
//simples vars
|
||||||
|
|
||||||
|
$tpl->add_vars(array(
|
||||||
|
"name" => "Gnieark",
|
||||||
|
"title" => "Monsieur",
|
||||||
|
"firstname" => "Grouik"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
$primes = array(1,2,3,5,7,11);
|
||||||
|
|
||||||
|
// a sub bloc
|
||||||
|
foreach($primes as $prime){
|
||||||
|
$tplPrime = new TplBlock('primes');
|
||||||
|
$tplPrime->add_vars(array('number' => $prime));
|
||||||
|
$tpl->add_sub_block($tplPrime);
|
||||||
|
}
|
||||||
|
|
||||||
|
// test sub - sub blocs
|
||||||
|
for ($i = 2; $i < 121; $i++){
|
||||||
|
|
||||||
|
$tplNumber = new TplBlock('number');
|
||||||
|
$tplNumber->add_vars( array("value" => $i));
|
||||||
|
$index = 1;
|
||||||
|
$number = $i;
|
||||||
|
while ( $number > 1 && $index < count($primes)){
|
||||||
|
if($number % $primes[$index] == 0){
|
||||||
|
$number = $number / $primes[$index];
|
||||||
|
$tplDivisor = new TplBlock("divisor");
|
||||||
|
$tplDivisor->add_vars( array("value" => $primes[$index]));
|
||||||
|
$tplNumber->add_sub_block($tplDivisor);
|
||||||
|
}else{
|
||||||
|
$index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$tpl->add_sub_block($tplNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
echo $tpl->apply_tpl_file("sample.txt",false);
|
Loading…
Reference in New Issue
Block a user