From e5dc4a5cd4132a652e63eed1eefb7805e731863b Mon Sep 17 00:00:00 2001 From: Rael Gugelmin Cunha Date: Wed, 13 Jan 2021 18:57:05 -0300 Subject: [PATCH 1/3] Adding the mapBlock method (#48) --- raelgc/view/Template.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/raelgc/view/Template.php b/raelgc/view/Template.php index 229cb25..16def6d 100644 --- a/raelgc/view/Template.php +++ b/raelgc/view/Template.php @@ -466,7 +466,8 @@ protected function subst($value) { * content. * * @param string $block the block name to be parsed - * @param boolean $append true if the content must be appended + * @param boolean $append true if the content must be appended (default) + * @return void */ public function block($block, $append = true) { if(!in_array($block, $this->blocks)) throw new \InvalidArgumentException("block $block does not exist"); @@ -487,6 +488,22 @@ public function block($block, $append = true) { if(isset($this->parents[$block])) foreach($this->parents[$block] as $child) $this->clear($child.'_value'); } + /** + * Shortcut to iterate over a block, assign values, then parse the related block. + * + * @param string $block the block name to be parsed + * @param string $varname constains a varname + * @param iterable $values any collection of values + * @param boolean $append true if the content must be appended (default) + * @return void + */ + public function mapBlock($block, $varname, $values, $append = true) { + foreach($values as $v) { + $this->$varname = $v; + $this->block($block, $append); + } + } + /** * Returns the final content * From 867924e2ec9e6febbac79acc7660542c41c7bc48 Mon Sep 17 00:00:00 2001 From: Rael Gugelmin Cunha Date: Wed, 13 Jan 2021 18:57:24 -0300 Subject: [PATCH 2/3] A simple test (#48) --- tests/TemplateTest.php | 13 ++++++++++++- tests/map_block.html | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/map_block.html diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index bc5e124..0f3cc03 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -161,7 +161,6 @@ public function __toString() { $this->assertEquals('foobar', trim($tpl->parse())); } - public function testObjectParse() { $tpl = new Template(__DIR__ . '/simple_var.html'); @@ -175,6 +174,18 @@ public function setBar($bar) { $this->assertEquals('Object', trim($tpl->parse())); } + public function testMapBlock() + { + $tpl = new Template(__DIR__ . '/map_block.html'); + $user = new class { + public $name; + }; + $user->name = 'Joe'; + $users = [$user]; + $tpl->mapBlock('BLOCK_USERS', 'USER', $users); + $this->assertEquals('Name: Joe', trim($tpl->parse())); + } + public function testFailureSimpleObjectCaseMismatch() { $this->expectException(RuntimeException::class); diff --git a/tests/map_block.html b/tests/map_block.html new file mode 100644 index 0000000..7be269f --- /dev/null +++ b/tests/map_block.html @@ -0,0 +1,3 @@ + +Name: {USER->NAME} + \ No newline at end of file From 6be34fa51d69bba589d30a05c702a46583a6e3ea Mon Sep 17 00:00:00 2001 From: Rael Gugelmin Cunha Date: Wed, 13 Jan 2021 19:04:13 -0300 Subject: [PATCH 3/3] Bump on version (#48) --- raelgc/view/Template.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/raelgc/view/Template.php b/raelgc/view/Template.php index 16def6d..fa93a27 100644 --- a/raelgc/view/Template.php +++ b/raelgc/view/Template.php @@ -2,7 +2,7 @@ namespace raelgc\view { /** - * Template Management for PHP5 + * Template Management for PHP >=5 * * The Template engine allows to keep the HTML code in some external files * which are completely free of PHP code. This way, it's possible keep logical @@ -15,7 +15,7 @@ * minor features. * * @author Rael G.C. (rael.gc@gmail.com) - * @version 2.2.7 + * @version 2.3.1 */ class Template {