Portal Home > Knowledgebase > Articles Database > Smarty help, please
Smarty help, please
Posted by ThatScriptGuy, 12-10-2007, 03:39 PM |
Hopefully a smarty guru can come and help me with this today - It's not a horribly complicated problem...
I have a PHP page that assigns a lot of data to a smarty template. One of those things is an array, which looks like this:
[id]=>name
Besides assigning that array, the page also assigns a lot of variables, which are arrays themselves. Let me try to demonstrate this:
Besides the $categories array, two more arrays are also assigned. They are $cat_2 and $cat_4
Now, what *should* be happening is this: When smarty creates the page, it makes a separate section for each category. In this case, a section will be created for Test Category and Test Category 2
In each section, there is an HTML dropdown, which should show the options from the relevant variable (In this case, $cat_2 or $cat_4)
My problem is this: I'm not sure how to access the variables from smarty using the array index from $categories. I know that smarty has some ability to use variable variables, but in all of my tinkering, I haven't been able to figure out how to tell it to take the indexes of $categories and look up the array data for that index(variable)
I hope I've been clear enough in this post. This would be very simple to accomplish using just PHP, but I'm trying to get in the habit of templatizing (<--new word) everything.
Any help here would be appreciated. I suppose if nobody else knows, though, it wouldn't hurt to make *just this page* straight PHP
Kevin
|
Posted by ThatScriptGuy, 12-10-2007, 04:04 PM |
I should elaborate that the reason I cannot just tell smarty to always look for $cat_2 and $cat_4 is because these categories, variables, and arrays are all dynamically created at runtime. Each system will be different and have different categories, etc.
Again - Any help is very very appreciated.
|
Posted by orbitz, 12-10-2007, 06:36 PM |
how exactly are the two categories $cat_2 and $cat_4 assigned in your php?
|
Posted by ThatScriptGuy, 12-10-2007, 07:20 PM |
Not sure what you're asking here.
They are created by the use of variable variables in PHP.
|
Posted by foobic, 12-10-2007, 07:23 PM |
I don't use smarty but a quick Google came up with this, which looks like roughly what you want.
|
Posted by ThatScriptGuy, 12-10-2007, 07:33 PM |
Thanks for the link foobic, but as far as I can tell, that's not quite what I'm looking for.
Let me try to simplify this a bit more (I might be making this too complicated - If that's the case, please just let me know)
In Smarty, we have the following arrays:
$categories
cat_2|Test Category
cat_4|Test Category 2
cat_282|Test Category 3
We also have these arrays:
$cat_2
2|Test Part
8|Test Part 2
9|Test Part 3
$cat_4
3|Test Part 4
5|Test Part 6
$cat_282
11|Test Part 9
15|Test Part 10
Now, what I'm looking to have done is something like this:
So, basically, for each category in the $category array, I want smarty to look up the variable of the key and then perform another foreach loop on it.
In PHP this is a very simple task (and would look much like the code above) but I just cannot figure out variable variables in Smarty.
Hope it's a little bit clearer now.
Kevin
|
Posted by foobic, 12-10-2007, 07:55 PM |
Isn't that almost exactly what the code snippet I linked is doing though? You apparently can't use the $$var dereference syntax in smarty, so you'd need to create the data in a single parent array:
$category_data[$cat_2]
2|Test Part
8|Test Part 2
9|Test Part 3
$category_data[$cat_4]
3|Test Part 4
5|Test Part 6
etc. and pass in $category_data.
|
Posted by ThatScriptGuy, 12-10-2007, 08:02 PM |
So it is - I just didn't see the correlation there.
I think I know where I need to go from here.
Thanks guys.
|
Posted by orbitz, 12-11-2007, 01:37 AM |
The example foobic giving is what I hoped you had on your script.
|
Posted by ThatScriptGuy, 12-11-2007, 01:38 AM |
I ended up giving up on this method this evening. The use of variable variables, while fun, was just not a good way to go. A couple of arrays solved all of my issues here.
|
Posted by jstanden, 12-11-2007, 06:18 PM |
Often you can do what you wanted with Smarty's {assign} function.
{assign var=temp value=$array.assoc_index}
That lets you split up arrays or $$var type variables conditionally.
|
Add to Favourites Print this Article
Also Read