Page 1 of 1
Building an array with smarty templates
Posted: Sat Aug 12, 2006 11:47 am
by GeXus
For those of you who are familiar with Smarty, I'm trying to basically build an array for a Year field. So im using a loop and just looping through until the current year is reached, any idea how I would assign this to an array that smarty would read? such as array('1','2','3')
Thanks!
Posted: Sun Aug 13, 2006 1:01 am
by RobertGonzalez
You are building this array code side and want to parse it in the template? That is simple... you make the array, assign it to a var and reference using an array construct in the template (either for, foreach, section, etc). Is that what you are asking about?
Re: Building an array with smarty templates
Posted: Sun Aug 13, 2006 1:40 am
by christian_phpbeginner
GeXus wrote:For those of you who are familiar with Smarty, I'm trying to basically build an array for a Year field. So im using a loop and just looping through until the current year is reached, any idea how I would assign this to an array that smarty would read? such as array('1','2','3')
Thanks!
Hi, I'm not familiar with smarty, because I am just trying to use it only in a couple of days. But hopefully, the code below could help.
Code: Select all
<?php
$numbers = array ('1', '2', '3', '4');
$numbersLength = count($numbers);
for ($i = 0; $i < $numbersLength; $i++) {
$smarty->assign('number1',$numbers[0]);
$smarty->assign('number2',$numbers[1]);
$smarty->assign('number3',$numbers[2]);
$smarty->assign('number4',$numbers[3]);
}
?>
The code hasn't tested, so sorry if it doesn't work.
Cao
Posted: Sun Aug 13, 2006 3:19 am
by RobertGonzalez
The joy of the templating engine is that you don't have to do that. You can just do...
Code: Select all
<?php
$numbers = array ('1', '2', '3', '4');
$smarty->assign('numarray',$numbers);
?>
Then in the template, loop through the members of the array...
Code: Select all
<ul>
{foreach key=i value=val from=$numarray}
<li>ID {$i} has a value of {$val}.</li>
{/foreach}
</ul>
Posted: Sun Aug 13, 2006 4:08 am
by christian_phpbeginner
Everah wrote:The joy of the templating engine is that you don't have to do that. You can just do...
Code: Select all
<?php
$numbers = array ('1', '2', '3', '4');
$smarty->assign('numarray',$numbers);
?>
Then in the template, loop through the members of the array...
Code: Select all
<ul>
{foreach key=i value=val from=$numarray}
<li>ID {$i} has a value of {$val}.</li>
{/foreach}
</ul>
Thanks for reviewing the code, Everah ! My moddy is Everah...

Posted: Tue Aug 15, 2006 7:47 pm
by GeXus
christian_phpbeginner wrote:Everah wrote:The joy of the templating engine is that you don't have to do that. You can just do...
Code: Select all
<?php
$numbers = array ('1', '2', '3', '4');
$smarty->assign('numarray',$numbers);
?>
Then in the template, loop through the members of the array...
Code: Select all
<ul>
{foreach key=i value=val from=$numarray}
<li>ID {$i} has a value of {$val}.</li>
{/foreach}
</ul>
Thanks for reviewing the code, Everah ! My moddy is Everah...

Hmmm.... I still don't think that works exactly as I had imagined...
It does not even really have to be an array, but basically I want to start a loop from 1900 and continue it until 2006 and echo it as <option value="1900>1900</option>.. etc... This would typically be simple, but with the templates (which im determined to use)... I just don't grasp it.
So just to clarify, I don't want to have to actually put 1900, 1901, 1902 all the way up to 2006.. i want to loop through and echo them out.
Posted: Tue Aug 15, 2006 8:10 pm
by RobertGonzalez
See, I use TemplateLite, a fork of Smarty. Smarty does not have a built in {for} function like TemplateLite does. In TL, you can run a for loop like that easily.
Code: Select all
{ for start=1990 stop=2006 value=current }
We are on number { $current }
{ /for }
But alas, Smarty does not offer this. At least I cannot find one in the docs. So you are going to have to develop an array code-side and walk it in the template using either a {foreach} or {section}.
Posted: Tue Aug 15, 2006 8:38 pm
by Jenk
Code: Select all
//assign the array and name it 'var'
$smarty->assign('var', array('1', '2', '3'));
Code: Select all
{*Smarty Template*}
{section name=foo loop=$var}
<p>{$var[foo]}</p>
{/section}
outputs:
If you are trying to limit the number of loops on anything other than the number of available items within the array, then it shouldn't be in the template anyway as that should be part of your model.
Posted: Tue Aug 15, 2006 9:39 pm
by GeXus
Jenk wrote:Code: Select all
//assign the array and name it 'var'
$smarty->assign('var', array('1', '2', '3'));
Code: Select all
{*Smarty Template*}
{section name=foo loop=$var}
<p>{$var[foo]}</p>
{/section}
outputs:
If you are trying to limit the number of loops on anything other than the number of available items within the array, then it shouldn't be in the template anyway as that should be part of your model.
So, I guess my question is how would I use a loop that would create that array?
Posted: Wed Aug 16, 2006 12:22 pm
by RobertGonzalez
Code: Select all
<?php
$year_array = array();
$this_year = date('Y');
for ($a = 1990; $a <= $this_year; $a++)
{
$year_array[] = $a;
}
$smarty->assign('years', $year_array);
?>
Code: Select all
{*Smarty Template*}
{section name=y loop=$years}
<p>{$years[y]}</p>
{/section}