Smarty getting proper values - hints

If you are new to smarty like I am, you may find this info useful:



At times you need to show or hide something in smarty templates based on some value. Smarty has a huge array that hosts pretty much anything you will be interested to test against.



Example:

Currently I was looking how to read off user level and display/hide information based on it. In DB there is a table “cscart_usergroup_descriptions” that has id and smarty has an array of them. So, to show the whole huge array I put in:



(you better use PRE tags around because if you don’t it will look like a block of text that is extremely hard to read)


<br />
<pre><br />
{php}<br />
print_r(get_defined_vars());<br />
{/php}<br />
</pre><br />

```<br />
<br />
This gave me huge array printed out, I quickly found that my variables are in:<br />
<br />
```php
<br />
[usergroup_ids] => Array<br />
                                (<br />
                                    [0] => 0<br />
                                    [1] => 1<br />
                                )<br />

```<br />
<br />
Zero is the default, in my tests all users had at least zero. One, seems to me, is admin (but I can be wrong on this one). 0 & 1 & 2 are not present in the DB table, I guess they are virtual. Then I have 3 and 6 for my groups, now, these 3 & 6 are primary keys in that table. You don't see them here in the array because I logged as admin for now.<br />
<br />
To access in {php} I can do this:<br />
<br />
(it is not evident where "AUTH" comes below, but if you were to see the whole huge array, you would see that it is the parent dimension of the array)<br />
<br />
```php
<br />
{php}<br />
//I would loop it but now just showing how to access each of them:<br />
echo $this->_tpl_vars['auth']['usergroup_ids'][0];<br />
echo $this->_tpl_vars['auth']['usergroup_ids'][1];<br />
{/php}<br />

```<br />
<br />
In smarty I would print out them as:<br />
<br />
```php
<br />
{* Surely, I would loop here too *}<br />
{$auth.usergroup_ids[0]}<br />
{$auth.usergroup_ids[1]}<br />

```<br />
<br />
In short, now you can, perhaps, do something wunderbar... ;)<br />
<br />
What I would do is set {assign} some value to be a flag (zero). I would loop through the group level array and do my own logical comparison to each value in the array. If matched then I would raise the flag (one or more). After the loop I would look at the flag value, if it is raised, then I would do something or not...

10x TexasGuy,



That’s very useful for me.



Crisdim

thanks TexasGuy,

Your hints are very usefull.



Also,

when you edit Content-> Pages and Catalog->Categories there are some option for group controls.

If you look for example in core code

line 36

file: controllers/costumer/categories.php



```php $_condition = ’ AND (’ . fn_find_array_in_set($auth[‘usergroup_ids’], ‘usergroup_ids’, true) . ‘)’; ```