English 中文(简体)
Smarty: how to evaluate string as a variable
原标题:
  • 时间:2009-12-08 10:59:20
  •  标签:
  • php
  • smarty

Variable assigned in my template is {$testVariable_123}, where 123 is auto generated and is also assigned to template as {$autoValue}

So in my template I want to get value of above variable.

{assign var="anotherValue" value="testVariable_"|cat:$autoValue}

So if I print {$anotherValue} instead of value I get string testVariable_123

Any help appreciated...

最佳回答

You need to create a variable whose value is {$testVariable_123}
Then you can call {eval} on it.

The problem is, I couldn t find a way to do this in a good manner.
Everything that looks reasonably good doesn t work and the options that work are ugly.

Maybe you d consider some changes in your application design?

Here s what I managed to get working:

# file.php:

  $smarty->assign("autoValue", 123);
  $smarty->assign("testVariable_123", "foo");

  //Option 1
  $smarty->assign("anotherValue", "{$testVariable_123}");
  //Option 2
  $smarty->assign("rb",  } ); // Hack to get the right bracket } withou Smarty parsing it.
  //Option 3
  $smarty->assign("mask",  {$testVariable_%s} ); // pass the full string_format "mask" directly from PHP

# file.tpl

1) Uses the $anotherValue from PHP:
Plain: {$anotherValue}
Evaled: {eval var=$anotherValue}

2) Build the string on Smarty itself:
{assign var="yetAnotherValue" value=$autoValue|string_format:"{$testVariable_%s$rb"}
Plain: {$yetAnotherValue}
Evaled: {eval var=$yetAnotherValue}

3) Build the string using the mask from php:
{assign var="enoughOfValue" value=$autoValue|string_format:$mask}
Plain: {$enoughOfValue}
Evaled: {eval var=$enoughOfValue}

Mostly, the problem is that Smarty won t ignore a closing bracket } or a $variable even if it s in the middle of a string. Escaping with doesn t work neither.

If you try:

{assign var="yetAnotherValue" value="{$testVariable_$autoValue}"}

it will ignore the "} at the end and consider the Smarty statement as:

{assign var="yetAnotherValue" value="{$testVariable_$autoValue}

and it will evaluate the $testVariable even tho it was supposed to be escaped.
So we will end up with {123 as the value :(

Everything I tried ended up stumbling on that issue. If you find a better way, please, make sure you share it here :)

问题回答

I think the solution you are looking for is this:

{assign var="anotherValue" value= {$testVariable_ |cat:$autoValue|cat: } }
{eval var=$anotherValue}

You can also get the value without having to assign a new variable:

{eval var= {$testVariable_ |cat:$autoValue|cat: } }

The trick is to use single quotes, so that Smarty does not automatically try to evaluate things that look like variables immediately.

{assign var="myVar" value="{$testVariable"|cat:$autoValue|cat:$smarty.rdelim}
{eval var=$myVar}

Worked for me.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签