View previous topic :: View next topic |
Author |
Message |
BRDude Smarty Rookie
Joined: 23 Nov 2003 Posts: 17
|
Posted: Sun Nov 23, 2003 2:45 pm Post subject: Arrays in config files would be great! |
|
|
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. I'd like to see developer's thoughts on this. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Nov 24, 2003 2:49 am Post subject: |
|
|
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 |
|
BloodRath Smarty Rookie
Joined: 06 Jul 2003 Posts: 23 Location: France
|
Posted: Mon Nov 24, 2003 2:10 pm Post subject: in same kind of reflexion , how about a concat config var |
|
|
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.... |
|
Back to top |
|
BRDude Smarty Rookie
Joined: 23 Nov 2003 Posts: 17
|
Posted: Wed Nov 26, 2003 1:28 am Post subject: |
|
|
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!
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 |
|
BRDude Smarty Rookie
Joined: 23 Nov 2003 Posts: 17
|
Posted: Wed Nov 26, 2003 4:51 am Post subject: |
|
|
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! |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Nov 26, 2003 5:10 am Post subject: |
|
|
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 |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Nov 26, 2003 8:28 am Post subject: |
|
|
@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 ? |
|
Back to top |
|
BRDude Smarty Rookie
Joined: 23 Nov 2003 Posts: 17
|
Posted: Wed Nov 26, 2003 1:48 pm Post subject: |
|
|
thanks again boots, smart workaround 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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Nov 26, 2003 2:03 pm Post subject: |
|
|
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 |
|
BloodRath Smarty Rookie
Joined: 06 Jul 2003 Posts: 23 Location: France
|
Posted: Wed Nov 26, 2003 8:32 pm Post subject: |
|
|
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.... |
|
Back to top |
|
BRDude Smarty Rookie
Joined: 23 Nov 2003 Posts: 17
|
Posted: Thu Nov 27, 2003 3:51 am Post subject: |
|
|
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 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 |
|
checat Smarty Rookie
Joined: 05 Feb 2004 Posts: 13 Location: Russia, Saint-Petersburg
|
Posted: Fri Feb 06, 2004 8:39 am Post subject: |
|
|
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 |
|
|