Conditional Statements for XenForo 2

Conditional Statements for XenForo 2

Anyone know how to hide a widget if someone is viewing XFMG (Xenforo media gallery)? I don;t want the widget to show when in the gallery, just the forums.
 
Anyone know how to hide a widget if someone is viewing XFMG (Xenforo media gallery)? I don;t want the widget to show when in the gallery, just the forums.
Unless I misunderstand you'd just uncheck the widget to be in the widget control.

What widget are you trying to hide?
 

Attachments

  • Screen Shot 2024-02-01 at 4.53.33 PM.webp
    Screen Shot 2024-02-01 at 4.53.33 PM.webp
    25.3 KB · Views: 0
You can exclude certain templates by adding them to this code:

!in_array($xf.reply.template, ['xfmg_media_index','templat_X'])

Or you can exclude the whole XFMG section with this coce:

$xf.reply.section != 'xfmg'

(y)
 
@Paul B and others.
Instead of creating a new thread, I thought it's better to ask here.

Is there a Xenforo date/time conditional that I can use only for the d-m php date format?

I need this for a "On This Day" feature, so e.g. on every 31st of May, the conditional would show the content. Similarly for other selected dates that I'd choose to display content.

E.g. on every 04.07 it shows a message for Independence Day
For every 24.12 it shows a message for Xmas eve etc....
 
Last edited:
Found it !

I remembered to have set another setting based on other conditions.
So basically you just need to declare a new variable $OnThisDay that calls the time function with the format you wish; and then just create the simple conditional to check.

Code:
<xf:set var="$OnThisDay" value="{{ date($xf.time, 'd m') }}" />

<xf:if is="$OnThisDay == '30 05'">
    On 30 May this event happened......
</xf:if>

I hope someone finds this useful.
 
Last edited:
Is there a way to detect javascript? So if Javascript is disabled, we can show a message or something?
You don't need a template condition for this, just use the <noscript> html tag ;)

 
You don't need a template condition for this, just use the <noscript> html tag ;)


Hm, would be ideal if it was possible with conditionals too so we can disable some functions until they turn it on.
 
Hm, would be ideal if it was possible with conditionals too so we can disable some functions until they turn it on.
AFAIK, Unfortunately you cannot know how a browser renders a page until it does so. Template conditions are basically PHP Code, so they don't know much about the browser. You can bypass that limitation e.g. if you set a cookie via javascript and check for it's existence. But you cannot do that on the 1st page load: First PHP has to build the page, then the browser has to render it (and execute javascript) and then you can do something with it.
 
I mess with templates so infrequently - how do I display all the variables available in a template? Globally?

If this could be put at the top of the resource that would be grouse.,
 
I mess with templates so infrequently - how do I display all the variables available in a template? Globally?
You can use dump(). A lot of output you will get if you insert this in your template: {{ dump($xf) }}.

You can do the same with other variables that are used in your template. But I am not aware of a way to display a list of all available variables..
 
Is there a way to hide something only from mobile devices?
Use media queries...
Less:
@media (max-width: @xf-responsiveWide)
{
    // your stuff displayed only in wide screens, so hidden in narrow and medium screens
}

@media (min-width: @xf-responsiveMedium)
{
    //your stuff displayed only above medium screen, so hidden in narrow screens
}

@media (max-width: @xf-responsiveNarrow)
{
    //your stuff displayed only on narrow screens
}

etc...
 
Last edited by a moderator:
@media (max-width: @xf-responsiveWide) { // your stuff displayed only in wide screens, so hidden in narrow and medium screens }

@media (max-width: @xf-responsiveWide) { // your stuff displayed only in wide screens, so hidden in narrow and medium screens }
I created a widget with html and css code called in extra.less ... I should only show it to fixed devices, since it can't be seen well on smartphones and is enlarged. so I need a way to either hide it or adjust the width in mobile browsers
 
so I need a way to either hide it or adjust the width in mobile browsers
Try this:
Less:
.block [data-widget-key="youo_widget_key"]
{
    @media (max-width: @xf-responsiveNarrow)
    {
        display: none;
    }
}
 
hello,
im trying to hide a group of buttons from non registered/unconfirmed users...
which ive done, thank you.

oh wait no no now there are errors hmm....im not surprised really lol. what i tried was to make a custom template modification and just wrap the whole template that inserts them in the conditional, which i used: <xf:if is="{{!$xf.visitor.isMemberOf(1)}}">

then im getting these errors from various places the buttons are located:
  • ErrorException: Template error: [E_USER_WARNING] Cannot call method isMemberOf on a non-object (NULL)
  • src/XF/Template/Templater.php:1268
im trying to learn some anyway because i really dont understand the simple proper ways...like how can i do it with CSS or can i put "display conditions" like $xf.visitor.user_id && $xf.visitor.user_state == 'valid'in extra.less template? etc etc i tried putting the conditional in there and it only hides it for everyone and ignores the thing. or how can i put the only that element in the conditional etc....

regards
 
To show content to members only it's simply: <xf:if is="$xf.visitor.user_id"> .

To use the same thing in a less template it's: [data-logged-in="true"] .
 
Back
Top Bottom