Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Assign an array in a template file

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
bcse
Smarty Rookie


Joined: 20 Oct 2004
Posts: 5

PostPosted: Wed Oct 20, 2004 6:48 pm    Post subject: Assign an array in a template file Reply with quote

Is there a way to assign an array in a template file (*.tmpl) ?

I'd like to assign an array, and use {foreach}, {html_options} to generate some HTML code.

I've tried a long time, but still can't do this.
_________________
Lvx ex Caelis --HIC AMICITVR ARCHA CEDERIS.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Wed Oct 20, 2004 8:20 pm    Post subject: Reply with quote

No.

But yes, if you are willing to go against the recommended practices and are willing to roll some of your own code and potentially obfuscate your templates. There are various techniques, many of which have been discussed here on the forum. (Try a search to find other suggestions than those listed here)

For example, you can use PHP's array_merge() function as a modifier to create an array on the fly. This often won't work with PHP5 since PHP5's array_merge() will not cast non-arrays to arrays. Also, remember the @ syntax for modifiers when you pass them arrays.

You can create an append plugin (based on assign) that uses $smarty->append internally instead of $smarty->assign. You can write your own plugins to manage creation of arrays (but remember the @ syntax for modifiers when you pass them arrays).

For associative arrays, review the following:
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=220
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=3387
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=64

And remember, it is not a recommended practice!

PS. You can also use config files to create indexed arrays for static items (eg: option lists) http://smarty.php.net/manual/en/config.files.php
Back to top
View user's profile Send private message
bcse
Smarty Rookie


Joined: 20 Oct 2004
Posts: 5

PostPosted: Thu Oct 21, 2004 5:32 am    Post subject: Reply with quote

Thanks a lot!! But I don't want to add plugins.

I will try to use config files. Smile
_________________
Lvx ex Caelis --HIC AMICITVR ARCHA CEDERIS.
Back to top
View user's profile Send private message Visit poster's website
bcse
Smarty Rookie


Joined: 20 Oct 2004
Posts: 5

PostPosted: Thu Oct 21, 2004 9:50 am    Post subject: Reply with quote

I've search in the forum, and now I found this. But it doesn't work.
(No error or warning, just a blank page.)

Could you please tell me what's wrong?

btw, my Smarty version is 2.6.2, it's include in pLog -- a blogging system -- so I can't update it. (If I update, other people may can't use my template.)

faq.conf wrote:
question = How do I...?
answer = Easy silly, just...

question = What if...?
answer = You can solve this by...

question = Could I please...?
answer = No. Piss off.


test.template wrote:
{config_load file="faq.conf"}
{php}
$smarty->config_overwrite = false;
{/php}
{assign var=question value=$smarty.config.question}
{assign var=answer value=$smarty.config.answer}
{section name=faq loop=$question}
Q: {$question[faq]} <br>
A: {$answer[faq]} <br>
{/section}

_________________
Lvx ex Caelis --HIC AMICITVR ARCHA CEDERIS.
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


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

PostPosted: Thu Oct 21, 2004 1:30 pm    Post subject: Reply with quote

In your {php} .. {/php} block you access your Smarty instance with $this instead of $smarty. Still, that should probably be in your php script when you actually implement it instead of the template.

HTH
Back to top
View user's profile Send private message
bcse
Smarty Rookie


Joined: 20 Oct 2004
Posts: 5

PostPosted: Thu Oct 21, 2004 3:48 pm    Post subject: Reply with quote

It's OK now!!

Thank you very much!! Very Happy
_________________
Lvx ex Caelis --HIC AMICITVR ARCHA CEDERIS.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    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