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

Arrays in config files would be great!

 
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 -> Feature Requests
View previous topic :: View next topic  
Author Message
BRDude
Smarty Rookie


Joined: 23 Nov 2003
Posts: 17

PostPosted: Sun Nov 23, 2003 2:45 pm    Post subject: Arrays in config files would be great! Reply with quote

I love Smarty, but with all the awesome features it has, I wonder why this isn't in. Arrays being defined in a config file seems like such a natural thing, something like:
Quote:

faq.config:

$faq[1].question = "How do I...?"
$faq[1].answer = """Easy silly, just..."""

$faq['foo'].question = "What if...?"
$faq['foo'].answer = """You can solve this by..."""

$faq[].question = "Could I please...?"
$faq[].answer = "No. Piss off."

template.tpl:

{config_load file="faq.config"}
{section name=question loop=$smarty.config.faq}
Q: {$smarty.config.faq[question].question} <br>
A: {$smarty.config.faq[question].answer} <br>
{/section}


A [] append operator would make things a lot easier too. Smile I'd like to see developer's thoughts on this.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Mon Nov 24, 2003 2:49 am    Post subject: Reply with quote

BRDude--Not a bad idea.

In the meantime, config files DO have limited support for arrays if you set $config_overwrite = false. In that case, overloading a var causes the var to become an indexed array, so:

var = "a"
var = "b"

results in var[0] = "a", var[1]="b". You can then use sections to have a quasi form of named arrays.

Does that affect your suggestion at all?
Back to top
View user's profile Send private message
BloodRath
Smarty Rookie


Joined: 06 Jul 2003
Posts: 23
Location: France

PostPosted: Mon Nov 24, 2003 2:10 pm    Post subject: in same kind of reflexion , how about a concat config var Reply with quote

sorry for rapid and dirty piece of code

/*
* Smarty plugin
* -------------------------------------------------------------
* Type: function
* Name: concat_configvar
* Purpose: concat an element with a config var and return the value
* of the config var so pointed
* param: var : the config var to concat
* with: the element to concat var with
* format : (optional) to transform with before concatenation
* author: Blood
* $id: 1.00 27.03.2003 : 23:27:00
* example:
* in test.conf :
* icono1 = test.gif
* in test.tpl :
* {foreach from=$menu key=key item=menuCourant}
* {concat_configvar var="icono" with=$key format="%02d"}
* {/foreach}
* result(depends of menu size):
* icono00
* icono01...
* -------------------------------------------------------------
*/


function smarty_function_concat_configvar($params, &$this)
{
extract($params);

if (!isset($var)) {
$this->trigger_error("concat_configvar: missing 'var' parameter");
return;
}

if (!isset($with)) {
$this->trigger_error("concat_configvar: missing 'with' parameter");
return;
}

$tmpAr = $this->get_config_vars();
$newvar = (isset($format)) ? $var.sprintf($format,$with) : $var.$with;
while (list ($key, $val) = each ($tmpAr)){
if(!strcasecmp($newvar,$key))
return $val;
}

return "$var$with couldn't be found in configvar";
}
_________________
froggies forever.... Wink
Back to top
View user's profile Send private message
BRDude
Smarty Rookie


Joined: 23 Nov 2003
Posts: 17

PostPosted: Wed Nov 26, 2003 1:28 am    Post subject: Reply with quote

boots, thanks for telling me about $config_overwrite, seems kind of hackish (not that I'm complaining), but who cares, it'll help me tons! Smile

I hope I can switch it off just while I'm loading a particular config, overloading for all configs would mess up some other stuff, no big deal though.

BloodRath, interesting piece of code, not 100% sure what it does, but will look into it!
Back to top
View user's profile Send private message
BRDude
Smarty Rookie


Joined: 23 Nov 2003
Posts: 17

PostPosted: Wed Nov 26, 2003 4:51 am    Post subject: Reply with quote

Oops, there seems to be a problem with {section} reading arrays generated by config files. This doesn't work:
Quote:

faq.config:

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

(the above config file, when loaded with $config_overwrite = false,
creates the $smarty.config.question and $smarty.config.answer
arrays perfectly)


template.tpl:

{config_load file="faq.config"}
{section name=faq loop=$smarty.config.question}
Q: {$smarty.config.question[faq]} <br>
A: {$smarty.config.answer[faq]} <br>
{/section}


I get an invalid reference error. I fixed it by "hacking" the Smarty_Compiler.class.php, line 1960, setting it from:

$_max_index = 2;

to:

$_max_index = 3;

Sections are working as they should now. It seems they weren't expecting $smarty.config to be a two-dimensional array. This should be fixed.

I'll post it in the bug report forum if someone else can confirm this, I'd appreciate that! Smile
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Nov 26, 2003 5:10 am    Post subject: Reply with quote

Hi BRDude. I had trouble getting your example to work at all. For some reason, $smarty.config.question[faq] isn't parsing here.

The following workaround does the trick without resorting to a class hack:

Code:
{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}


Its perhaps a little clumsy, but it works Smile
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Wed Nov 26, 2003 8:28 am    Post subject: Reply with quote

@BRDude: i can confirm that this is a problem.

but shouldn't it be without $_max_index instead of $_max_index=3 ?
no $_max_index means no limit, so the next guy who has $smarty.config.foo[a][b] doesn't have to hack it up to four ? Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website
BRDude
Smarty Rookie


Joined: 23 Nov 2003
Posts: 17

PostPosted: Wed Nov 26, 2003 1:48 pm    Post subject: Reply with quote

thanks again boots, smart workaround Smile I was feeling kind of dirty inside for messing with Smarty's code.

messju, thanks, maybe it should be limitless (I don't understand what's with that limit checking anyway), but these config generated arrays are a very limited feature, apparently you can't build fancy multi-dimensional arrays, so $_max_index = 3 is just enough for it to barely work.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Wed Nov 26, 2003 2:03 pm    Post subject: Reply with quote

that sounds reasonable. one cannot create any deeper structures of config-vars right now anyway, right?

i commited your fix. it will be in the next release.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
BloodRath
Smarty Rookie


Joined: 06 Jul 2003
Posts: 23
Location: France

PostPosted: Wed Nov 26, 2003 8:32 pm    Post subject: Reply with quote

this piece of code just allow u to make ref to a config var but by constructing the index of the ref
thus it allow u to give a icone for a menu depending of the menu postion, and when construct the menu obtain the good ref, by mixing boucle count and cofig var

// in test.conf :
icono00 = bof.gif
icono01 = test.gif
icono02 = testb.gif

// in test.tpl
{foreach from=$menu key=key item=menuCourant}
{concat_configvar var="icono" with=$key format="%02d"}
{/foreach}

depending on menu
icono00 and icono01 and icono02 could be shown

blood
_________________
froggies forever.... Wink
Back to top
View user's profile Send private message
BRDude
Smarty Rookie


Joined: 23 Nov 2003
Posts: 17

PostPosted: Thu Nov 27, 2003 3:51 am    Post subject: Reply with quote

messju that's great, I believe this functionality will be very helpful! It's already made my life easier, now I have staff updating FAQs by editing simple config files instead of messing with my precious templates Smile gotta' love Smarty!

BloodRath, cool, that can be pretty useful, maybe I'll come up with a plugin myself for arrays and such
Back to top
View user's profile Send private message
checat
Smarty Rookie


Joined: 05 Feb 2004
Posts: 13
Location: Russia, Saint-Petersburg

PostPosted: Fri Feb 06, 2004 8:39 am    Post subject: Reply with quote

I asked for a way to define associative arrays in the config file some time ago.
Since they are not implemented, I am constrained to define them in PHP. It makes a lot of ungracefulness.
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 -> Feature Requests 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