1
frankblack
Reading and computing group-ids with smarty
  • 2007/10/5 9:50

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


Perhaps there is a much more elegant way? I needed this for myself and did not found a better solution. The following code is for XOOPS 2.0.17

1. edit class/theme.php and add after line 199 the following code:
'xoops_getgroups' => $xoopsUser->getGroups(),


2. in the template it looks like this:
<{if array_intersect('1'$xoops_getgroups)}>if user is in group 1, do anything<{/if}>


Addendum: Of course it is bad if you need an array and AFAIK there is no way to make an array INSIDE a template. You have to assign the array in the PHP-file to the template. Thanks to google I have found a function where you can build arrays inside a template.

1. save the following code as file function.assign_array.php in class/smarty:

/*
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     function
 * Name:     assign_array
 * Version:  1.0
 * Author:   Jens Lehmann 
 * Credits:  Monte Ohrt 
 * Purpose:  assign an array to a template variable
 * Input:    var       =  name of the array
 *           values    =  list of values (seperated by delimiter)
 *           delimiter =  value delimiter, default is ","
 *
 * Examples: {assign_array var="foo" values="bar1,bar2"}
 *           {assign_array var="foo" values="bar1;bar2;bar3" delimiter=";"}
 * -------------------------------------------------------------
 */
function smarty_function_assign_array($params, &$smarty)
{
    
extract($params);

  if(empty(
$delimiter)) {
    
$delimiter ',';
  }

    if (empty(
$var)) {
        
$smarty->trigger_error("assign_array: missing 'var' parameter");
        return;
    }

    if (!
in_array('values'array_keys($params))) {
        
$smarty->trigger_error("assign_array: missing 'values' parameter");
        return;
    }

    
$smarty->assign($varexplode($delimiter,$values) );
}

/* vim: set expandtab: */

?>


2. the template looks now like this:
<{assign_array var="gruppen" values="1,2"}><{if array_intersect($gruppen$xoops_getgroups)}>if there is an intersection, do anything<{/if}>


It seems to be that other array-functions like in_array etc.pp. are working also with this code.

Enjoy trying.

2
nachenko
Re: Reading and computing group-ids with smarty
  • 2007/10/5 10:39

  • nachenko

  • Quite a regular

  • Posts: 356

  • Since: 2005/1/18


Hi, FrankBlack.

I wrote a hack like this some time ago, don't know what's exactly your problem:

I added this code in header.php about line 100:
Quote:
$xoopsTpl->assign( 'xoops_usergroups', $xoopsUser->getGroups() );



And this is the result in the Smarty console:

Quote:
{$xoops_usergroups} Array (4)
0 => 1
1 => 2
2 => 15
3 => 16


See? It gives an array. What method did you use to attach it to the template? Did you use $xoopsTpl->assign() or did you use any other method?

3
frankblack
Re: Reading and computing group-ids with smarty
  • 2007/10/5 11:15

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


I guess adding the snippet in theme.php or header.php makes no difference. I wanted to compute the array quite simple. And the simplest way from the viewpoint of a theme designer (without touching any php files) was to do this INSIDE the templates.

So I came up with the added smarty-function for arrays.

That's all. I know that there must be better solutions out there, but I did not find any.

4
iHackCode
Re: Reading and computing group-ids with smarty

another example. (its not using groupid though, but can be changed easily)
https://xoops.org/modules/newbb/viewtopic.php?topic_id=32938&viewmode=flat&order=ASC&start=0#forumpost157003

using Dave's code, i use something like this in the templates and themes
<{if in_group('somegroupname')}>
<
b>htmlb>
<{/if}>
CBB / LatestNews / Publisher / XM-Spotlight

(ノ◕ヮ◕)ノ*:・゚✧

5
frankblack
Re: Reading and computing group-ids with smarty
  • 2007/10/5 12:47

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


Or only smarty-based (found on myxoops.org):
<{php}>
global 
$xoopsuser;
if (
in_array("1",($xoopsUser)?$xoopsUser->getGroups():array(XOOPS_GROUP_ANONYMOUS))) {
<{/
php]>
.. if 
user is in the group

<{php}> } else { <{/php}>
.. for 
everybody else ...

<{
php}> } <{/php}>

Login

Who's Online

358 user(s) are online (241 user(s) are browsing Support Forums)


Members: 0


Guests: 358


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits