Building an array with smarty templates
Moderator: General Moderators
Building an array with smarty templates
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!
Thanks!
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- christian_phpbeginner
- Forum Contributor
- Posts: 136
- Joined: Sat Jun 03, 2006 2:43 pm
- Location: Java
Re: Building an array with smarty templates
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.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!
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]);
}
?>Cao
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
The joy of the templating engine is that you don't have to do that. You can just do...
Then in the template, loop through the members of the array...
Code: Select all
<?php
$numbers = array ('1', '2', '3', '4');
$smarty->assign('numarray',$numbers);
?>Code: Select all
<ul>
{foreach key=i value=val from=$numarray}
<li>ID {$i} has a value of {$val}.</li>
{/foreach}
</ul>- christian_phpbeginner
- Forum Contributor
- Posts: 136
- Joined: Sat Jun 03, 2006 2:43 pm
- Location: Java
Thanks for reviewing the code, Everah ! My moddy is Everah...Everah wrote:The joy of the templating engine is that you don't have to do that. You can just do...
Then in the template, loop through the members of the array...Code: Select all
<?php $numbers = array ('1', '2', '3', '4'); $smarty->assign('numarray',$numbers); ?>Code: Select all
<ul> {foreach key=i value=val from=$numarray} <li>ID {$i} has a value of {$val}.</li> {/foreach} </ul>
Hmmm.... I still don't think that works exactly as I had imagined...christian_phpbeginner wrote:Thanks for reviewing the code, Everah ! My moddy is Everah...Everah wrote:The joy of the templating engine is that you don't have to do that. You can just do...
Then in the template, loop through the members of the array...Code: Select all
<?php $numbers = array ('1', '2', '3', '4'); $smarty->assign('numarray',$numbers); ?>Code: Select all
<ul> {foreach key=i value=val from=$numarray} <li>ID {$i} has a value of {$val}.</li> {/foreach} </ul>
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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}.
Code: Select all
{ for start=1990 stop=2006 value=current }
We are on number { $current }
{ /for }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}Code: Select all
<p>1</p>
<p>2</p>
<p>3</p>So, I guess my question is how would I use a loop that would create that array?Jenk wrote:Code: Select all
//assign the array and name it 'var' $smarty->assign('var', array('1', '2', '3'));outputs:Code: Select all
{*Smarty Template*} {section name=foo loop=$var} <p>{$var[foo]}</p> {/section}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.Code: Select all
<p>1</p> <p>2</p> <p>3</p>
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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}