addSubBlocsDefinitions
This commit is contained in:
parent
1202a19e75
commit
c2377040af
21
TplBlock.php
21
TplBlock.php
|
@ -171,6 +171,27 @@ class TplBlock
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatically add subs blocs and sub sub blocs ..., and vars
|
||||
* directly from an associative array
|
||||
* @param $subBlocsDefinitions the associative array
|
||||
* @return TplBlock For chaining.
|
||||
*/
|
||||
public function addSubBlocsDefinitions(ARRAY $subBlocsDefinitions, $deleteExistingsBlocs=false)
|
||||
{
|
||||
|
||||
foreach($subBlocsDefinitions as $itemKey => $itemValue){
|
||||
if(is_array($itemValue)){
|
||||
$subBloc = new TplBlock($itemKey);
|
||||
$subBloc->addSubBlocsDefinitions($itemValue,$deleteExistingsBlocs);
|
||||
$this->addSubBlock($subBloc);
|
||||
}else{
|
||||
$this->addVars(array($itemKey => $itemValue));
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
|
||||
}
|
||||
/**
|
||||
* Generate the sub block regex.
|
||||
*
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
/**
|
||||
* Gnieark’s TplBlock sample.
|
||||
*
|
||||
* PHP version 5
|
||||
* PHP version 5 or 7
|
||||
*
|
||||
* @category Template
|
||||
* @package TplBlock
|
||||
|
|
|
@ -214,4 +214,35 @@ class TplBlockTest extends TestCase
|
|||
$this->assertContains("wpooie456",$tpl-> applyTplStr($str));
|
||||
|
||||
}
|
||||
|
||||
public function testaddSubBlocsDefinitions(){
|
||||
$model = "
|
||||
{{simpleVar}}
|
||||
<!-- BEGIN bloc -->
|
||||
{{bloc.simpleVar}}
|
||||
<!-- BEGIN bloc.subBloc -->
|
||||
{{bloc.subBloc.simpleVar}}
|
||||
<!-- END bloc.subBloc -->
|
||||
<!-- END bloc -->
|
||||
";
|
||||
$blocsDefinitions = array(
|
||||
"simpleVar" => "hey",
|
||||
"bloc" => array(
|
||||
"simpleVar" => "Ho",
|
||||
"subBloc" => array(
|
||||
"simpleVar" => "HAAAAAA!"
|
||||
)
|
||||
)
|
||||
);
|
||||
$resultShouldBe = "
|
||||
hey
|
||||
Ho
|
||||
HAAAAAA!
|
||||
";
|
||||
$tpl = new TplBlock();
|
||||
$result = $tpl -> addSubBlocsDefinitions($blocsDefinitions)->applyTplStr($model);
|
||||
$this->assertEquals($result, $resultShouldBe);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user