Damien Pyseek
Damien Pyseek

Reputation: 37

Convert value of object into PHP variable (smarty)

I am trying to the the value of a HTML tag to a PHP variable. The code I have below will not work, the value of {mytag} is 21.

I have {mytag} as an HTML hook inside the smarty template to extract a field from a db, so I have to have {mytag}.

The plan is to use the int from the db inside some maths but as {mytag} is treated as an object I have to get the value into another variable.

$t = new stdClass;
$t->bat = "{mytag}";
$bar = $t->foo;
ob_start();
$b = var_export($bar, true);
echo $b;
$stdClass = ob_get_contents();
ob_end_clean();
$the_var = $stdClass;

When I check the output I get the name but not the value.

echo $the_var[0] 
//prints ' and not 2
echo $the_var[1] 
//prints { and not 1

How can I get $the_var to contain value 21?

Upvotes: 1

Views: 1114

Answers (1)

Craig Taub
Craig Taub

Reputation: 4179

Quite a small solution but should work.

{php}
$the_var = "$this->get_template_vars('mytag')";
{/php}

From smarty docs:

get_template_vars() — returns assigned variable value(s)
array get_template_vars(string varname);

Upvotes: 1

Related Questions