View previous topic :: View next topic |
Author |
Message |
bmittleider Smarty n00b
Joined: 25 Feb 2015 Posts: 4
|
Posted: Wed Feb 25, 2015 9:22 pm Post subject: Use Smarty assigned variable in Smarty assigned variable |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Feb 25, 2015 10:00 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Feb 25, 2015 11:06 pm Post subject: |
|
|
I think, better answer would be "You're doing it wrong, please rethink your goals and methods." |
|
Back to top |
|
bmittleider Smarty n00b
Joined: 25 Feb 2015 Posts: 4
|
Posted: Wed Feb 25, 2015 11:32 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Feb 25, 2015 11:39 pm Post subject: |
|
|
Remove second assignment. Use included templates. |
|
Back to top |
|
bmittleider Smarty n00b
Joined: 25 Feb 2015 Posts: 4
|
Posted: Thu Feb 26, 2015 6:07 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Feb 27, 2015 12:22 am Post subject: |
|
|
Do note you're contradicting yourself. |
|
Back to top |
|
bmittleider Smarty n00b
Joined: 25 Feb 2015 Posts: 4
|
Posted: Fri Feb 27, 2015 5:43 pm Post subject: |
|
|
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 |
|
|