From f23d9bf2e0886498a3d9d5bb0a800d663b795e61 Mon Sep 17 00:00:00 2001 From: javiexin Date: Sun, 5 Feb 2017 21:47:31 +0100 Subject: [PATCH] [ticket/15068] Add template vars retrieval from the template object PHPBB3-15068 --- phpBB/phpbb/template/base.php | 29 ++++++++++++++++ phpBB/phpbb/template/context.php | 57 +++++++++++++++++++++++++++++++ phpBB/phpbb/template/template.php | 24 +++++++++++++ 3 files changed, 110 insertions(+) diff --git a/phpBB/phpbb/template/base.php b/phpBB/phpbb/template/base.php index 9a40702ba8..e84aef6abe 100644 --- a/phpBB/phpbb/template/base.php +++ b/phpBB/phpbb/template/base.php @@ -104,6 +104,27 @@ abstract class base implements template return $this; } + /** + * {@inheritdoc} + */ + public function retrieve_vars(array $vararray) + { + $result = array(); + foreach ($vararray as $varname) + { + $result[$varname] = $this->retrieve_var($varname); + } + return $result; + } + + /** + * {@inheritdoc} + */ + public function retrieve_var($varname) + { + return $this->context->retrieve_var($varname); + } + /** * {@inheritdoc} */ @@ -124,6 +145,14 @@ abstract class base implements template return $this; } + /** + * {@inheritdoc} + */ + public function retrieve_block_vars($blockname, array $vararray) + { + return $this->context->retrieve_block_vars($blockname, $vararray); + } + /** * {@inheritdoc} */ diff --git a/phpBB/phpbb/template/context.php b/phpBB/phpbb/template/context.php index 4ee48205c8..0e5f3287aa 100644 --- a/phpBB/phpbb/template/context.php +++ b/phpBB/phpbb/template/context.php @@ -86,6 +86,17 @@ class context return true; } + /** + * Retreive a single scalar value from a single key. + * + * @param string $varname Variable name + * @return mixed Variable value, or null if not set + */ + public function retrieve_var($varname) + { + return isset($this->rootref[$varname]) ? $this->rootref[$varname] : null; + } + /** * Returns a reference to template data array. * @@ -263,6 +274,52 @@ class context return true; } + /** + * Retrieve key variable pairs from the specified block + * + * @param string $blockname Name of block to retrieve $vararray from + * @param array $vararray An array of variable names + * @return array of hashes with variable name as key and retrieved value or null as value + */ + public function retrieve_block_vars($blockname, array $vararray) + { + // For nested block, $blockcount > 0, for top-level block, $blockcount == 0 + $blocks = explode('.', $blockname); + $blockcount = sizeof($blocks) - 1; + + $block = $this->tpldata; + for ($i = 0; $i <= $blockcount; $i++) + { + if (($pos = strpos($blocks[$i], '[')) !== false) + { + $name = substr($blocks[$i], 0, $pos); + + if (strpos($blocks[$i], '[]') === $pos) + { + $index = sizeof($block[$name]) - 1; + } + else + { + $index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1); + } + } + else + { + $name = $blocks[$i]; + $index = sizeof($block[$name]) - 1; + } + $block = $block[$name]; + $block = $block[$index]; + } + + $result = array(); + foreach ($vararray as $varname) + { + $result[$varname] = isset($block[$varname]) ? $block[$varname] : null; + } + return $result; + } + /** * Change already assigned key variable pair (one-dimensional - single loop entry) * diff --git a/phpBB/phpbb/template/template.php b/phpBB/phpbb/template/template.php index 041ecb12e4..183999d5f4 100644 --- a/phpBB/phpbb/template/template.php +++ b/phpBB/phpbb/template/template.php @@ -127,6 +127,22 @@ interface template */ public function append_var($varname, $varval); + /** + * Retrieve multiple template values + * + * @param array $vararray An array with variable names + * @return array A hash of variable name => value pairs (value is null if not set) + */ + public function retrieve_vars(array $vararray); + + /** + * Retreive a single scalar value from a single key. + * + * @param string $varname Variable name + * @return mixed Variable value, or null if not set + */ + public function retrieve_var($varname); + /** * Assign key variable pairs from an array to a specified block * @param string $blockname Name of block to assign $vararray to @@ -143,6 +159,14 @@ interface template */ public function assign_block_vars_array($blockname, array $block_vars_array); + /** + * Retrieve variable values from an specified block + * @param string $blockname Name of block to retrieve $vararray from + * @param array $vararray An array with variable names + * @return array A hash of variable name => value pairs (value is null if not set) + */ + public function retrieve_block_vars($blockname, array $vararray); + /** * Change already assigned key variable pair (one-dimensional - single loop entry) *