Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
About loop in an associative array with {section}

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
jjgod
Smarty n00b


Joined: 19 Apr 2003
Posts: 4

PostPosted: Sat Apr 19, 2003 12:33 pm    Post subject: About loop in an associative array with {section} Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3327
Location: Oldenburg, Germany

PostPosted: Sat Apr 19, 2003 1:16 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
LoP
Smarty n00b


Joined: 18 Apr 2003
Posts: 4
Location: Biale Blota, Poland

PostPosted: Sat Apr 19, 2003 4:59 pm    Post subject: Reply with quote

yep, u surely prefer {foreach} Very Happy
Back to top
View user's profile Send private message Send e-mail Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3327
Location: Oldenburg, Germany

PostPosted: Sat Apr 19, 2003 8:29 pm    Post subject: Reply with quote

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. Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 5537
Location: Lincoln Nebraska, USA

PostPosted: Sat Apr 19, 2003 11:35 pm    Post subject: Reply with quote

An advantage of section is you can access other assigned arrays with the same loop var, much like for() vs. foreach()
Back to top
View user's profile Send private message Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3327
Location: Oldenburg, Germany

PostPosted: Sun Apr 20, 2003 8:47 am    Post subject: Reply with quote

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 ?

Question

greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
jjgod
Smarty n00b


Joined: 19 Apr 2003
Posts: 4

PostPosted: Tue Apr 22, 2003 9:51 am    Post subject: Reply with quote

It really works. Thanks for help. Smile

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 5537
Location: Lincoln Nebraska, USA

PostPosted: Tue Apr 22, 2003 7:27 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
jjgod
Smarty n00b


Joined: 19 Apr 2003
Posts: 4

PostPosted: Wed Apr 23, 2003 10:32 am    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 5537
Location: Lincoln Nebraska, USA

PostPosted: Wed Apr 23, 2003 1:43 pm    Post subject: Reply with quote

Yeah that should be fine.
Back to top
View user's profile Send private message Visit poster's website
jjgod
Smarty n00b


Joined: 19 Apr 2003
Posts: 4

PostPosted: Thu Apr 24, 2003 5:07 am    Post subject: Reply with quote

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
View user's profile Send private message
mutant
Smarty Rookie


Joined: 11 May 2004
Posts: 7

PostPosted: Mon Aug 02, 2004 5:16 am    Post subject: Reply with quote

jjgod wrote:
It really works. Thanks for help. Smile

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. Sad

Anywho.. I would appreciate anything you could tell me.
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5608
Location: Toronto, Canada

PostPosted: Mon Aug 02, 2004 6:01 am    Post subject: Reply with quote

@mutant: you can't do that with section. Associative arrays are NOT the same thing as indexed arrays. Try this thread for a possible solution for you: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=3093
Back to top
View user's profile Send private message
mutant
Smarty Rookie


Joined: 11 May 2004
Posts: 7

PostPosted: Mon Aug 02, 2004 1:50 pm    Post subject: Reply with quote

boots wrote:
@mutant: you can't do that with section. Associative arrays are NOT the same thing as indexed arrays. Try this thread for a possible solution for you: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=3093


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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP