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

Use Smarty assigned variable in Smarty assigned variable

 
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
bmittleider
Smarty n00b


Joined: 25 Feb 2015
Posts: 4

PostPosted: Wed Feb 25, 2015 9:22 pm    Post subject: Use Smarty assigned variable in Smarty assigned variable Reply with quote

Is it possible to assign a variable (e.g. $smarty->assign('myVar','Hello World')) and then use that in another assign $smarty->assign('someVar','This should print {$myVar}!') and have that actually output 'This should print Hello World!'?
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Feb 25, 2015 10:00 pm    Post subject: Reply with quote

The content of assigned variables is not parsed for Smarty tags.
But in this example you could use the replace modifier in your template.

Code:
{$someVar|replace:'{myVar}':$myVar}
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 25, 2015 11:06 pm    Post subject: Reply with quote

I think, better answer would be "You're doing it wrong, please rethink your goals and methods."
Back to top
View user's profile Send private message
bmittleider
Smarty n00b


Joined: 25 Feb 2015
Posts: 4

PostPosted: Wed Feb 25, 2015 11:32 pm    Post subject: Reply with quote

The issue is the replacement isn't in the template. I have a template that loops over an array and in this assigned array I have the text that will populate in the template. The text I want to be used as the replacement has already been assigned and is used in other templates or sections and I was trying to reduce repetitive code and make it easier for my non PHP/HTML editor to be able to output the desired content and {$myVar} is much easier to put in for less skilled person who doesn't understand full PHP/HTML.

Maybe an explanation through sample.

Code:

$smarty->assign('state',array('full'=>'California', 'abrv'=>'CA'));
$smarty->assign('examples' ,array(
    'example', array(
      'a'=>'I am from {$state.abrv}',
      'b'=>', also known as {$state.full}
    ),
    'example', array(
      'a'=>'I have lived in {$state.full} for 20 years. ',
      'b'=>'I like the weather in {$state.abrv}'
    ),
  )
);


Template file:
Code:

<div class="block">
{foreach $examples as $example}
  <li>{$example.a}{$example.b}</li>
{/foreach}
</div>
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Feb 25, 2015 11:39 pm    Post subject: Reply with quote

Remove second assignment. Use included templates.
Back to top
View user's profile Send private message
bmittleider
Smarty n00b


Joined: 25 Feb 2015
Posts: 4

PostPosted: Thu Feb 26, 2015 6:07 pm    Post subject: Reply with quote

AnrDaemon wrote:
Remove second assignment. Use included templates.


In my particular project I can't. The loop is generated from an array of questions that are static with the exception of the replacement value. In these items I need to replace a keyword with a value being passed through $_GET.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Feb 27, 2015 12:22 am    Post subject: Reply with quote

Do note you're contradicting yourself.
Back to top
View user's profile Send private message
bmittleider
Smarty n00b


Joined: 25 Feb 2015
Posts: 4

PostPosted: Fri Feb 27, 2015 5:43 pm    Post subject: Reply with quote

U.Tews wrote:
The content of assigned variables is not parsed for Smarty tags.
But in this example you could use the replace modifier in your template.

Code:
{$someVar|replace:'{myVar}':$myVar}


Had I looked at this closer the first time I would have had my answer. I didn't put it into context with where I needed to use the code snippet.

Thanks U.Tews.
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