addSubBlocsDefinitions

This commit is contained in:
Gnieark 2018-06-02 00:17:48 +02:00
parent 1202a19e75
commit c2377040af
3 changed files with 53 additions and 1 deletions

View File

@ -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.
*

View File

@ -2,7 +2,7 @@
/**
* Gniearks TplBlock sample.
*
* PHP version 5
* PHP version 5 or 7
*
* @category Template
* @package TplBlock

View File

@ -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);
}
}