Fix change in signature of getTemplateVars (#995)

This commit is contained in:
Simon Wisselink
2024-04-13 16:53:05 +02:00
committed by GitHub
parent 77c0b74e3b
commit 5ee4363000
3 changed files with 117 additions and 99 deletions

1
changelog/994.md Normal file
View File

@@ -0,0 +1 @@
- Fix that getTemplateVars would return an array of objects instead of the assigned variables values [#994](https://github.com/smarty-php/smarty/issues/994)

View File

@@ -224,7 +224,10 @@ class Data
return $this->getValue($varName, $searchParents);
}
return array_merge($this->parent && $searchParents ? $this->parent->getTemplateVars() : [], $this->tpl_vars);
return array_merge(
$this->parent && $searchParents ? $this->parent->getTemplateVars() : [],
array_map(function(Variable $var) { return $var->getValue(); }, $this->tpl_vars)
);
}
/**

View File

@@ -1,17 +1,6 @@
<?php
/**
* Smarty PHPunit tests getTemplateVars method
*
* @author Uwe Tews
*/
/**
* class for getTemplateVars method test
*
*
*
*
*/
class GetTemplateVarsTest extends PHPUnit_Smarty
{
@@ -20,7 +9,6 @@ class GetTemplateVarsTest extends PHPUnit_Smarty
$this->setUpSmarty(__DIR__);
}
public function testInit()
{
$this->cleanDirs();
@@ -109,4 +97,30 @@ class GetTemplateVarsTest extends PHPUnit_Smarty
$this->assertEquals("bar2", $data2->getTemplateVars('foo2', false));
$this->assertEquals("", $data2->getTemplateVars('blar', false));
}
/**
* test that variable assigned by global assign in template is included in getTemplateVars
*/
public function testAssignedInTemplate()
{
$this->smarty->fetch('string:{assign var="b" value="x" scope="global"}');
$this->assertEquals('x', $this->smarty->getTemplateVars('b'));
}
/**
* test that getTemplateVars returns simple array of values
*/
public function testSimpleCallReturnsArrayWithAllValues()
{
$this->smarty->assign('foo', 'bar');
$this->smarty->assign('i', 3);
$vars = $this->smarty->getTemplateVars();
$this->assertArrayHasKey('foo', $vars);
$this->assertArrayHasKey('i', $vars);
$this->assertEquals('bar', $vars['foo']);
$this->assertEquals(3,$vars['i']);
}
}