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)



```php


{php}
print_r(get_defined_vars());
{/php}

```



This gave me huge array printed out, I quickly found that my variables are in:



```php

[usergroup_ids] => Array

(

[0] => 0

[1] => 1

)

```



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.



To access in {php} I can do this:



(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)



```php

{php}

//I would loop it but now just showing how to access each of them:

echo $this->_tpl_vars[‘auth’][‘usergroup_ids’][0];

echo $this->_tpl_vars[‘auth’][‘usergroup_ids’][1];

{/php}

```



In smarty I would print out them as:



```php

{* Surely, I would loop here too *}

{$auth.usergroup_ids[0]}

{$auth.usergroup_ids[1]}

```



In short, now you can, perhaps, do something wunderbar… :wink:



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) . ‘)’; ```