Web Hosting Talk







View Full Version : Smarty help, please


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:

$categories=array('cat_2'=>'Test Category',
'cat_4'=>'Test Category 2');

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

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.

orbitz
12-10-2007, 06:36 PM
how exactly are the two categories $cat_2 and $cat_4 assigned in your php?

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.

foobic
12-10-2007, 07:23 PM
I don't use smarty but a quick Google came up with this (http://osdir.com/ml/php.smarty.general/2003-09/msg00063.html), which looks like roughly what you want.

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:

{foreach from=$categories item=value key=index}

{foreach from=$$index item=value2 key=index2}
{$index2}-{$value2}
{/foreach}
<tr><td colspan="2"><hr /></td></tr>
<tr><td width="50%">Please select the main (default) {$value}: </td>
<td width="50%"><select name="{$index}"><option value="0">None</option></select></td></tr>
<tr><td width="50%">Please select additional compatible {$value}s: </td>
<td width="50%"><select name="{$index}_additional" multiple="multiple"></select></td></tr>
<tr><td width="50%">How many times should this option be shown for this system?</td>
<td width="50%"><input type="text" size="1" name="{$index}_num_times"></td></tr>
<tr><td width="50%">Should this option show a simple drop down or checkboxes?</td>
<td width="50%"><select name="{$index}_selection_type"><option value="dropdown">Drop Down</option><option value="checkbox">Checkbox</option></select></td></tr>
{/foreach}

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

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.

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.

orbitz
12-11-2007, 01:37 AM
The example foobic giving is what I hoped you had on your script.

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.

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.