Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

bitflags with smarty?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
skold
Smarty Rookie


Joined: 31 Jul 2005
Posts: 12

PostPosted: Tue Oct 18, 2005 8:23 pm    Post subject: bitflags with smarty? Reply with quote

im reworking one of my sites to use flag based permissions.. and im having trouble with smarty. id like to know if its even capable of this before i make some convoluted workaround.

basically what id like to do is something like this:

{if ($s_flags & 0x00002)}
etc
{/if}

ive confirmed that the $s_flags variable is set properly, but smarty doesnt seem to be capable of working with bitflags. is this true? or is there some other way to get it working the way i want?

thanks
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Oct 19, 2005 7:28 am    Post subject: Re: bitflags with smarty? Reply with quote

skold wrote:
Code:
{if ($s_flags & 0x00002)}
etc
{/if}


works for me.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
skold
Smarty Rookie


Joined: 31 Jul 2005
Posts: 12

PostPosted: Wed Oct 19, 2005 6:22 pm    Post subject: Reply with quote

Can you show me your code? It's not working for me.

First off, I'm running 2.6.10, so thats not it Smile

Here's some code:

Code:

    $smarty->assign ("s_flags",      "$s_flags");

if ($s_flags & 0x00002) {
alert ("etc");
}


and in the tpl:

Code:
{if ($s_flags & 0x00002)}
etc
{/if}


the alert box pops up fine, so i know the flag is assigned properly.. but i get nothing from the template.

Also,

Code:
{if ($s_flags)}
etc
{/if}


does work, and 'etc' appears on the page, so I know the variable is being set properly, it just seems that smarty isnt able to see whether or not it has the flag set..

Is there some option I need to set somewhere for this to work?

Thanks
Back to top
View user's profile Send private message
skold
Smarty Rookie


Joined: 31 Jul 2005
Posts: 12

PostPosted: Wed Oct 19, 2005 7:02 pm    Post subject: Reply with quote

Ok, well as far as I can tell this just isn't possible. It seems smarty doesn't support this..

The best workaround I've found so far is to use the {php} tag:

Code:

   {php} global $s_flags; if ($s_flags & 0x00080) { {/php}
         ...etc...
   {php} } {/php}


Hopefully that helps anyone else with the same problem..

If it IS possible to do this the way I originally tried, I'd love to hear how Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Oct 19, 2005 7:06 pm    Post subject: Reply with quote

Surely you want:

$smarty->assign ("s_flags", $s_flags);

instead of:

$smarty->assign ("s_flags", "$s_flags");

which assigns a string value.
Back to top
View user's profile Send private message
skold
Smarty Rookie


Joined: 31 Jul 2005
Posts: 12

PostPosted: Wed Oct 19, 2005 9:30 pm    Post subject: Reply with quote

good point, but it still doesn't work..

Code:

    $s_flags = $_SESSION['s_flags'];
    $smarty->assign ("s_flags",   $s_flags);


Code:

{if ($s_flags & 0x00001)}
etc
{/if}


that produces no output?
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Oct 20, 2005 8:30 am    Post subject: Reply with quote

skold wrote:
Can you show me your code?


Code:
{assign var=s_flags value=3}
{if ($s_flags & 0x0002)} ... {/if}


gives the three dots. but

Code:
{assign var=s_flags value=1}
{if ($s_flags & 0x0002)} ... {/if}


doesn't give them.
just as expected.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
skold
Smarty Rookie


Joined: 31 Jul 2005
Posts: 12

PostPosted: Fri Oct 21, 2005 11:01 am    Post subject: Reply with quote

Well, somethings terribly wrong then.

Code:
{assign var=s_flags value=3}
{if ($s_flags & 0x0002)} ... {/if}


didnt give any output

Code:
{assign var=s_flags value=3}
{if !($s_flags & 0x0002)} ... {/if}


that did, however

so its obviously not working, at least for me. the question is, why?
Back to top
View user's profile Send private message
skold
Smarty Rookie


Joined: 31 Jul 2005
Posts: 12

PostPosted: Mon Dec 05, 2005 7:08 pm    Post subject: Reply with quote

im still having this exact problem, this time with a new application im making from scratch with smarty.

heres the code in question:
Code:

       $smarty->append (items, array ("id" => $row[id], "name" => $row[name], "description" => $row[description], "category" => $row[category], "price" => $row[price], "flags" => $row[flags], "ts" => $row[ts]));

Code:

    {if ($items.flags & 0x00001)}
yes
    {/if}


as before, it doesnt work, although it seems it should. my {php} tag workaround mentioned above works fine, and i know the variable is being assigned properly, so what gives?
Back to top
View user's profile Send private message
skold
Smarty Rookie


Joined: 31 Jul 2005
Posts: 12

PostPosted: Mon Dec 05, 2005 7:33 pm    Post subject: Reply with quote

after a little more experiementing, i found that this works:

Code:
{assign var="flags" value=$items.flags}
{assign var="flag" value=0x00001}
    {if $flags & $flag}
yes
    {/if}


but the 2 line block i pasted above still wont... why? Sad
Back to top
View user's profile Send private message
Joaquin
Smarty n00b


Joined: 19 Jan 2006
Posts: 2

PostPosted: Tue Jan 31, 2006 8:51 am    Post subject: Reply with quote

Skold,

Should you (or anyone else) still struggle with this 'weird behaviour', you might want to consider this function plugin I wrote:

Code:
<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.got_bits.php
 * Type:     function
 * Name:     got bits
 * Purpose:  performs a bitwise AND on 2 params
 * -------------------------------------------------------------
 */
function smarty_function_got_bits($params, &$smarty)
{
   $foo = $params['mask'];
   $bar = $params['bmap'];

   if ($foo & $bar)
   {
      return "checked";
   }
   else
   {
      return "";
   }
}

?>
This rendered undesireable results, while:

Code:
<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     function.got_bits.php
 * Type:     function
 * Name:     got bits
 * Purpose:  performs a bitwise AND on 2 params
 * -------------------------------------------------------------
 */
function smarty_function_got_bits($params, &$smarty)
{
   $foo = intval($params['mask']);
   $bar = intval($params['bmap']);

   if ($foo & $bar)
   {
      return "checked";
   }
   else
   {
      return "";
   }
}

?>

Worked exactly as it should work (at least for me, and I'm just using INTs)

In other words: explicitly converting the two variables to INT solved my problem.

Don't know if it helps for you, but it might shed some light on why it seems to behave irradically for you...

regards
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP