Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

concatenate strings in {eval}

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
johannes
Smarty Regular


Joined: 10 May 2003
Posts: 85
Location: Malmö, Sweden

PostPosted: Fri Feb 06, 2004 9:00 am    Post subject: concatenate strings in {eval} Reply with quote

Hi

I use {eval} func.

I want to use It like this {eval var=$eval_$id}, where the $id is passed from the php. page, (assigned) and then dynamic create the 'var'.

But I cant pass the value $id to the tpl page.... why?

php
$smarty->assign(id, $id);
$smarty->assign($eval, $doc);
$evald = $smarty->fetch('eval.tpl');

eval TPL
I want to use It like this {eval var=$eval_$id}


How do I concatenate the strings and make this work, Is it possible??
{eval var=$eval_'$id'}
{eval var=$eval_"$id"}
and so on... wont work for me!

soon weekend Smile
_________________
--------------------------
nice culture on the Internet
www.poeter.se
Back to top
View user's profile Send private message Send e-mail Visit poster's website
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Fri Feb 06, 2004 9:53 am    Post subject: Reply with quote

Use {assign} in the template to put the strings in another variable.
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
till
Smarty Rookie


Joined: 25 Feb 2004
Posts: 19
Location: Berlin, Germany

PostPosted: Wed Feb 25, 2004 12:05 pm    Post subject: Reply with quote

Hello,

I have the same problem.

Setup:

I dump variables called "foo_1" and "foo_2" into my template.

How do I access those? The number appended is an ID. So basically, I have another section and loop through an array and need to output "foo_" with the current ID appended.

Any examples?

I am trying with assign and eval, and neither does exactly what I want.

Thanks,
Till
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Feb 25, 2004 12:13 pm    Post subject: Reply with quote

@till: don't use variable variables, use arrays instead.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
xscully
Smarty n00b


Joined: 25 May 2006
Posts: 2

PostPosted: Thu May 25, 2006 2:32 pm    Post subject: Reply with quote

sorry to bump this ooooold topic but I having the same 'dilema' as till and I can't change my var_1 var_2 to an array...

how do I access var_1 / var_2 ... using a section or for with fixed loops..

like

[php:1:2d92d02da4]<?
$smarty->assign('var_1', 'Bob');
$smarty->assign('var_2', 'Fred');
$smarty->assign('var_3', 'John');
?>[/php:1:2d92d02da4]

Code:

{section name=x start=1 loop=3}
   {$var_$smarty.section.x.index}
{/section}
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 25, 2006 3:00 pm    Post subject: Reply with quote

[php:1:de7686e078]<?
$smarty->assign('var', array( '1'=>'Bob', '2'=>'Fred', '3', 'John' ));
?>
[/php:1:de7686e078]
Code:
{section name=x start=1 loop=3}
   {$var[$smarty.section.x.index]}
{/section}
Back to top
View user's profile Send private message
xscully
Smarty n00b


Joined: 25 May 2006
Posts: 2

PostPosted: Thu May 25, 2006 3:19 pm    Post subject: Reply with quote

I can't change the php code... I can't make an array of those variables Sad

I have to find a way to make smarty read those $var_1.....

well... I think I have to write the variables not using {section} Crying or Very sad
{$var_1}..........
{$var_2}..........
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu May 25, 2006 3:59 pm    Post subject: Reply with quote

Well, you can use a clever (ie: ugly hack) arrangement of {capture}{literal}..{/literal}{/capture}{eval} to get a similar result. I won't give the full solution because it is a VERY bad idea to use it. Why not change the data structure? At any-rate, var-vars are not supported so trying to use and/or design for them is going to be a headache.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP