Building an array with smarty templates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Building an array with smarty templates

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Re: Building an array with smarty templates

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

Post 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... :D
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post 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... :D
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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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}.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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:

Code: Select all

<p>1</p>
<p>2</p>
<p>3</p>
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.
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Post 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:

Code: Select all

<p>1</p>
<p>2</p>
<p>3</p>
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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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}
Post Reply