| View previous topic :: View next topic |
| Author |
Message |
jjgod Smarty n00b

Joined: 19 Apr 2003 Posts: 4
|
Posted: Sat Apr 19, 2003 12:33 pm Post subject: About loop in an associative array with {section} |
|
|
Hi, I'm an user of Smarty in China. Here are some of my questions:
1. I found that I can't use {section} to loop an array in my templates as follow:
| Code: |
$document_info = array("Article A" => "URL of Article A",
"Article B" => "URL of Article B",
"Article C" => "URL of Article C",
...);
|
I hope the output will be as follow:
| Quote: |
<a herf="URL of Article A">Article A</a>
<a herf="URL of Article B">Article B</a>
<a herf="URL of Article C">Article C</a>
...
|
But I found that I could not use {smarty.section.section_name.index} to display the key of this associative array (such as "Article A", "Article B" ...).
2. I even found that I could not loop some loop some associative arrays in nested sections. The way showed in Smarty's manual doesn't work.
--
I wonder anyone could help. Thanx. |
|
| Back to top |
|
messju Administrator

Joined: 16 Apr 2003 Posts: 3327 Location: Oldenburg, Germany
|
Posted: Sat Apr 19, 2003 1:16 pm Post subject: |
|
|
yes, {section} needs the keys to be 0,1,2...
use {foreach} for associative arrays:
| Code: |
{foreach from=$document_info key=href item=title}
<a href="{$href}">{$title}</a>
{/foreach}
|
HTH
greetings to china  |
|
| Back to top |
|
LoP Smarty n00b

Joined: 18 Apr 2003 Posts: 4 Location: Biale Blota, Poland
|
Posted: Sat Apr 19, 2003 4:59 pm Post subject: |
|
|
yep, u surely prefer {foreach}  |
|
| Back to top |
|
messju Administrator

Joined: 16 Apr 2003 Posts: 3327 Location: Oldenburg, Germany
|
Posted: Sat Apr 19, 2003 8:29 pm Post subject: |
|
|
yes.
i prefer emacs over ultraedit.
i prefer plain enlightenment over gnome or kde.
i prefer tcsh over any graphical filemanager on the planet.
but when it comes to dumping arrays into pages with smarty
i prefer the new-school/modern {foreach} over {section} whenever i have the choice.  |
|
| Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 5537 Location: Lincoln Nebraska, USA
|
Posted: Sat Apr 19, 2003 11:35 pm Post subject: |
|
|
| An advantage of section is you can access other assigned arrays with the same loop var, much like for() vs. foreach() |
|
| Back to top |
|
messju Administrator

Joined: 16 Apr 2003 Posts: 3327 Location: Oldenburg, Germany
|
Posted: Sun Apr 20, 2003 8:47 am Post subject: |
|
|
but i can use the key-attribute to get a loop-var as a hook to access the corresponding values of other arrays than the loop-array.
or did you mean something different?
or am i missing something obvious ?
greetings
messju |
|
| Back to top |
|
jjgod Smarty n00b

Joined: 19 Apr 2003 Posts: 4
|
Posted: Tue Apr 22, 2003 9:51 am Post subject: |
|
|
It really works. Thanks for help.
I wonder whether I can translate some parts of the manual of Smarty into Chinese, for some Chinese PHP Developers. But since I may not have too much time to do it so I would try to finish this work bit by bit.
Waiting for permission. |
|
| Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 5537 Location: Lincoln Nebraska, USA
|
Posted: Tue Apr 22, 2003 7:27 pm Post subject: |
|
|
| No permission needed. Grab the sgml files from the CVS repository and translate them, send me back the sgml files and I'll get them posted. Then it is a matter of keeping up with the docs diffs from that point onward. |
|
| Back to top |
|
jjgod Smarty n00b

Joined: 19 Apr 2003 Posts: 4
|
Posted: Wed Apr 23, 2003 10:32 am Post subject: |
|
|
Should I add file headers like this?
| Quote: |
<!-- Smarty German Documentation Port -->
<!-- $Id: preface.sgml,v 1.2 2003/04/17 14:50:49 mohrt Exp $ -->
<!-- $Author: mohrt $ -->
<!-- $Revision: 1.2 $ -->
|
|
|
| Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 5537 Location: Lincoln Nebraska, USA
|
Posted: Wed Apr 23, 2003 1:43 pm Post subject: |
|
|
| Yeah that should be fine. |
|
| Back to top |
|
jjgod Smarty n00b

Joined: 19 Apr 2003 Posts: 4
|
Posted: Thu Apr 24, 2003 5:07 am Post subject: |
|
|
Obviously there won't be any trouble in generating HTML files, just set the encoding to "gb2312" is ok.
But another problem is: How to generate PDF document from these .sgml files? If no fonts included, can it support any languages (such as Chinese, Japanese and so on)?
I used to use TeX to generate my documents from .tex into .pdf, and I need to use it with some packages for Chinese supporting. |
|
| Back to top |
|
mutant Smarty Rookie
Joined: 11 May 2004 Posts: 7
|
Posted: Mon Aug 02, 2004 5:16 am Post subject: |
|
|
| jjgod wrote: | It really works. Thanks for help.
I wonder whether I can translate some parts of the manual of Smarty into Chinese, for some Chinese PHP Developers. But since I may not have too much time to do it so I would try to finish this work bit by bit.
Waiting for permission. |
Hi jjgod,
Did you figure out how to do this? {section} has some features like last, first, and prev that I would like to use, but I can't figure out how to get the key value from a section. Everything I've read appears that you can't.. If you have any pointers on how you did this, I would really appreciate it.
| Code: |
$directions['from i-76'] = ' ... some directions ';
$directions['from i-78'] = ' ... some directions ';
$directions['from i-79'] = ' ... some directions ';
$smarty=>assign('directions', $directions);
....
{section name=dir loop=$directions}
{ if $smarty.section.dir.first }
-- Print nice image --
{/if}
From <b>{$directions[dir] !!![key??]!!}</b> take {$directions[dir][ value? ]}.
|
As near as I can tell, I can't reference the associative array's key/value pair from a section. Interestingly, it will iterate the array, just not let you have the key.
Anywho.. I would appreciate anything you could tell me. |
|
| Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5608 Location: Toronto, Canada
|
|
| Back to top |
|
mutant Smarty Rookie
Joined: 11 May 2004 Posts: 7
|
Posted: Mon Aug 02, 2004 1:50 pm Post subject: |
|
|
Thats a good post/thread -- THANK YOU for providing a definative answer to this issue. At least I'm not bashing my head into my monitor.
I've set a temporary variable to count the assoc array, then use an {if} in the code.. This seems to be working fine. I can tell last and first now by using {counter}. Its not really elegant, but it works.
Thank you. |
|
| Back to top |
|
|