View previous topic :: View next topic |
Author |
Message |
skold Smarty Rookie
Joined: 31 Jul 2005 Posts: 12
|
Posted: Tue Oct 18, 2005 8:23 pm Post subject: bitflags with smarty? |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Oct 19, 2005 7:28 am Post subject: Re: bitflags with smarty? |
|
|
skold wrote: | Code: | {if ($s_flags & 0x00002)}
etc
{/if} |
|
works for me. |
|
Back to top |
|
skold Smarty Rookie
Joined: 31 Jul 2005 Posts: 12
|
Posted: Wed Oct 19, 2005 6:22 pm Post subject: |
|
|
Can you show me your code? It's not working for me.
First off, I'm running 2.6.10, so thats not it
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 |
|
skold Smarty Rookie
Joined: 31 Jul 2005 Posts: 12
|
Posted: Wed Oct 19, 2005 7:02 pm Post subject: |
|
|
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 |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Oct 19, 2005 7:06 pm Post subject: |
|
|
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 |
|
skold Smarty Rookie
Joined: 31 Jul 2005 Posts: 12
|
Posted: Wed Oct 19, 2005 9:30 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Oct 20, 2005 8:30 am Post subject: |
|
|
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 |
|
skold Smarty Rookie
Joined: 31 Jul 2005 Posts: 12
|
Posted: Fri Oct 21, 2005 11:01 am Post subject: |
|
|
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 |
|
skold Smarty Rookie
Joined: 31 Jul 2005 Posts: 12
|
Posted: Mon Dec 05, 2005 7:08 pm Post subject: |
|
|
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 |
|
skold Smarty Rookie
Joined: 31 Jul 2005 Posts: 12
|
Posted: Mon Dec 05, 2005 7:33 pm Post subject: |
|
|
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? |
|
Back to top |
|
Joaquin Smarty n00b
Joined: 19 Jan 2006 Posts: 2
|
Posted: Tue Jan 31, 2006 8:51 am Post subject: |
|
|
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 |
|
|