aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/template
diff options
context:
space:
mode:
authorjaviexin <javiexin@gmail.com>2017-02-05 21:47:31 +0100
committerjaviexin <javiexin@gmail.com>2017-02-05 21:47:31 +0100
commitf23d9bf2e0886498a3d9d5bb0a800d663b795e61 (patch)
treea3042937f9f2cbaced596f61d4c8fca496dbf83c /phpBB/phpbb/template
parent3322117c3863c443ca1b79d25541bde4c662c0ed (diff)
downloadforums-f23d9bf2e0886498a3d9d5bb0a800d663b795e61.tar
forums-f23d9bf2e0886498a3d9d5bb0a800d663b795e61.tar.gz
forums-f23d9bf2e0886498a3d9d5bb0a800d663b795e61.tar.bz2
forums-f23d9bf2e0886498a3d9d5bb0a800d663b795e61.tar.xz
forums-f23d9bf2e0886498a3d9d5bb0a800d663b795e61.zip
[ticket/15068] Add template vars retrieval from the template object
PHPBB3-15068
Diffstat (limited to 'phpBB/phpbb/template')
-rw-r--r--phpBB/phpbb/template/base.php29
-rw-r--r--phpBB/phpbb/template/context.php57
-rw-r--r--phpBB/phpbb/template/template.php24
3 files changed, 110 insertions, 0 deletions
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
@@ -107,6 +107,27 @@ abstract class base implements template
/**
* {@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}
+ */
public function assign_block_vars($blockname, array $vararray)
{
$this->context->assign_block_vars($blockname, $vararray);
@@ -127,6 +148,14 @@ abstract class base implements template
/**
* {@inheritdoc}
*/
+ public function retrieve_block_vars($blockname, array $vararray)
+ {
+ return $this->context->retrieve_block_vars($blockname, $vararray);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert')
{
return $this->context->alter_block_array($blockname, $vararray, $key, $mode);
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
@@ -87,6 +87,17 @@ class context
}
/**
+ * 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.
*
* This function is public so that template renderer may invoke it.
@@ -264,6 +275,52 @@ class context
}
/**
+ * 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)
*
* An example of how to use this function:
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
@@ -128,6 +128,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
* @param array $vararray A hash of variable name => value pairs
@@ -144,6 +160,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)
*
* An example of how to use this function: