Not a bug Bitfields not working in <xf:if>

DragonByte Tech

Well-known member
Affected version
2.0.0 RC2
Code:
<xf:if is="$instance.options.enableshoutsound AND ($xf.visitor.dbtech_vbshout_settings & 16384)">

public:dbtech_shout_shoutbox - Line 49: Expected valid expression.

XF\PrintableException: Template public:dbtech_shout_shoutbox error: Line 49: Expected valid expression. src\XF\Mvc\Entity\Entity.php:1058

Stack trace:
#0 src\XF\DevelopmentOutput\Template.php(100): XF\Mvc\Entity\Entity->save()
#1 src\XF\DevelopmentOutput\Template.php(204): XF\DevelopmentOutput\Template->import('public:dbtech_s...', 'DBTech/Shout', '<xf:css src="db...', Array, Array)
#2 src\XF\Template\Templater.php(556): XF\DevelopmentOutput\Template->watchTemplate(Object(DBTech\Shop\XF\Template\Templater), 'public', 'dbtech_shout_sh...')
#3 src\XF\Template\Templater.php(519): XF\Template\Templater->getTemplateData('public', 'dbtech_shout_sh...')
#4 src\XF\Template\Templater.php(1229): XF\Template\Templater->getTemplateCode('public', 'dbtech_shout_sh...')
#5 C:\git\dbtech-universal_vbshout\universal_vbshout\upload\src\addons\DBTech\Shout\Application\Template.php(199): XF\Template\Templater->renderTemplate('dbtech_shout_sh...', Array)
#6 C:\git\dbtech-universal_vbshout\universal_vbshout\upload\src\addons\DBTech\Shout\Model\Shoutbox.php(710): DBTech\Shout\Application\Template->renderTemplate('dbtech_shout_sh...')
#7 C:\git\dbtech-universal_vbshout\universal_vbshout\upload\src\addons\DBTech\Shout\Model\Shoutbox.php(929): DBTech\Shout\Model\Shoutbox->render(Array, false, false)
#8 C:\git\dbtech-xf2shout\xf2shout\upload\src\addons\DBTech\Shout\XF\Widget\Shoutbox.php(57): DBTech\Shout\Model\Shoutbox->renderWrapper(Array)
#9 src\XF\Template\Templater.php(1506): DBTech\Shout\XF\Widget\Shoutbox->render()
#10 internal_data\code_cache\templates\l1\s1\public\forum_list.php(209): XF\Template\Templater->widgetPosition('forum_list_abov...', Array)
#11 src\XF\Template\Templater.php(1230): XF\Template\Templater->{closure}(Object(DBTech\Shop\XF\Template\Templater), Array)
#12 src\XF\Template\Template.php(24): XF\Template\Templater->renderTemplate('forum_list', Array)
#13 src\XF\Mvc\Renderer\Html.php(48): XF\Template\Template->render()
#14 src\XF\Mvc\Dispatcher.php(332): XF\Mvc\Renderer\Html->renderView('XF:Forum\\Listin...', 'public:forum_li...', Array)
#15 src\XF\Mvc\Dispatcher.php(303): XF\Mvc\Dispatcher->renderView(Object(XF\Mvc\Renderer\Html), Object(XF\Mvc\Reply\View))
#16 src\XF\Mvc\Dispatcher.php(44): XF\Mvc\Dispatcher->render(Object(XF\Mvc\Reply\View), 'html')
#17 src\XF\App.php(1863): XF\Mvc\Dispatcher->run()
#18 src\XF.php(328): XF\App->run()
#19 index.php(13): XF::runApp('XF\\Pub\\App')
#20 {main}

This is blocking me from working on the Shoutbox, so if this is a valid bug it would be great if you could post the file modification(s) needed to fix this :)

Thanks!
 
I don’t recall us having support for bitwise operators in XF1 either (unless I’m mistaken). I don’t think this is something we’re trying to support otherwise.

It’s possible we could consider it in the future if not right now though.

However, for now, this is probably a good candidate for adding a function to the User entity which could simply take the bit value and return the result of that expression compared against your field value.

HTML:
<xf:if is="$xf.visitor.hasDbTechShoutBit(16384)">
    // blah
</xf:if>

That could even check if the option you check is enabled too.
 
I don’t recall us having support for bitwise operators in XF1 either (unless I’m mistaken). I don’t think this is something we’re trying to support otherwise.
Code:
<xen:if is="{$instance.options.enableshoutsound} AND {$visitor.dbtech_vbshout_settings} & 16384">
This works perfectly fine in XF1 :)

I tried with every combination of { } or no { } to try to trick this into working :P


Fillip
 
They're not valid operators -- we don't support all of the operators in PHP (and we support some slightly different ones). But I'm not sure if we should be aiming to support them in most cases.

To be honest, given the example code, I think that's terrible for readability and I don't actually want to encourage it. You should really have some sort of lookup map if you're going to do a bitfield. So I agree about this being a method on the User entity, though I disagree that you should be passing an integer/number into it. Presumably this is where you'd pass a string in and use a lookup map.

Regardless though, if we did do this, I don't really foresee it being in 2.0. You're talking about some additional operator stuff in the lexer and parser. That's not really an ideal thing to be changing in an RC.
 
As alluded to above, we're not really considering this as a viable change in the short term, potentially not in the long term either. You may notice that we generally have avoided using them in XF as they make things harder to work with, though they do also have their benefits. Not necessarily implying you don't use them anymore, that's certainly your design decision, but I'm also not sure we've seen them used all that often, and as we don't either that's pretty much why we don't explicitly feel the need to add extra support for them.

I also agree with Mike, using the getter approach is probably preferable overall, though you could provide a mapping of the bitfields to improve readability while still having the benefits of them, e.g.
HTML:
$xf.visitor.hasDbTechShoutPerm('view')
Or just split everything up into distinct methods (similar to what we do with XF:
Code:
$xf.visitor.canViewDbTechShout();

Anyway, entirely up to you but I wouldn't expect us to support bitwise stuff in templates in the foreseeable future, unfortunately.
 
Anyway, entirely up to you but I wouldn't expect us to support bitwise stuff in templates in the foreseeable future, unfortunately.
No bother, it's largely legacy code anyway since it's based on code that was running on vB3/vB4, so at some point in the future it'd be good to go through and replace it with something better :)

For now, the getter works perfectly fine :)


Fillip
 
Top Bottom