From c2377040af5fc8838d7ffb7affbc28b1c8d9edd7 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 2 Jun 2018 00:17:48 +0200 Subject: [PATCH] addSubBlocsDefinitions --- TplBlock.php | 21 +++++++++++++++++++++ sample/sample.php | 2 +- test/TplBlockTest.php | 31 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/TplBlock.php b/TplBlock.php index b23962f..e1baa32 100644 --- a/TplBlock.php +++ b/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. * diff --git a/sample/sample.php b/sample/sample.php index e82f3a2..9baea7a 100644 --- a/sample/sample.php +++ b/sample/sample.php @@ -2,7 +2,7 @@ /** * Gnieark’s TplBlock sample. * - * PHP version 5 + * PHP version 5 or 7 * * @category Template * @package TplBlock diff --git a/test/TplBlockTest.php b/test/TplBlockTest.php index cfefb98..6f636e7 100644 --- a/test/TplBlockTest.php +++ b/test/TplBlockTest.php @@ -214,4 +214,35 @@ class TplBlockTest extends TestCase $this->assertContains("wpooie456",$tpl-> applyTplStr($str)); } + + public function testaddSubBlocsDefinitions(){ + $model = " +{{simpleVar}} + +{{bloc.simpleVar}} + +{{bloc.subBloc.simpleVar}} + + +"; + $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); + + + } }