Conditional Statements for XenForo 2

Conditional Statements for XenForo 2

If I want block both a forum that thread how? No luck with this:
Use containerKey instead of contentKey for nodes:

Code:
<xf:if is="in_array($xf.reply.template, ['forum_list', 'forum_view', 'thread_view', 'thread_view_type_poll', 'whats_new', 'whats_new_posts']) && $xf.reply.contentKey != 'thread-123' && $xf.reply.containerKey != 'node-456'">.

If I entered this ^^ correctly, it blocks all ads not just the one intended.
then ask Andy. I have no idea how his addon works (i.e. what variables are available). It was just a guess.
 
<xf:if is="in_array($xf.reply.template, ['forum_list', 'forum_view', 'thread_view', 'thread_view_type_poll', 'whats_new', 'whats_new_posts']) && $xf.reply.contentKey != 'thread-123' && $xf.reply.containerKey != 'node-456'">.
This works.

Will see if Andy can help on ad below post. Thanks @nocte !
 
One last question I SWEAR :)

With this:

&& $xf.reply.contentKey != 'thread-123'">

or this

&& $xf.reply.containerKey != 'node-456'">

Can you put multiples in one expression?

like this:

&& $xf.reply.contentKey != 'thread-123, thread-456'">

or this:

&& $xf.reply.containerKey != 'node-456, node-789'">

?
 
Can you put multiples in one expression?
you can copy from your code above (in_array($xf.reply.template, ['forum_list', 'forum_view', 'thread_view', 'thread_view_type_poll', 'whats_new', 'whats_new_posts'])). So you have to use something like:

!in_array($xf.reply.containerKey, ['node-456', 'node-789'])

Notice the ! at the beginning (for "not").
 
Hey there, I was wondering how can I show something to users who are allowed to start a thread in a specific section?

I would like to show a button on the thread list page to users who have permission to start a thread and hide the button to users who do not have permissions to start a thread in that specific section.

Thanks kindly!
 
Hello, If the user does not have permission to create a discussion in a specific forum then he will not see the button.
Use usergroups permissions, that's it.
 
Hello, If the user does not have permission to create a discussion in a specific forum then he will not see the button.
Use usergroups permissions, that's it.
Um, not really. I'm using a custom floating post thread button. I'm trying to show this button to users who have permission to create threads in that specific section itself :)
 
Hello,

i search for a conditional like this:

if actual member has thread in forum X
content
if not
other content


(check if a member posted one or more thread(s) (not posts, he should be a thread starter) in a specifical forum or sub forum eg.1)

... to work in a widget (HTML).


Have anyone an hint for me? :)
 
Last edited:
i search for a conditional like this:

To the bests of my knowledge that is not available out of the box.

You can add a template function via addon.

Code event listener: templater_setup

Code in your Listener.php:
PHP:
public static function templaterSetup(\XF\Container $container, \XF\Template\Templater &$templater)
{
   $templater->addFunction('has_thread_in_forum', function($templater, &$escape, $user, $forum_id)
   {
      // check if $user has visible thread in forum $forum_id
   });
}

Usage:
HTML:
<xf:if is="has_thread_in_forum($xf.visitor, 123)">
    <!-- User has thread -->
<xf:else />
    <!-- User has no thread -->
</xf:if>

But keep in mind, that you have to check dynamically for threads in that forum (unless you implement some kind of caching), which adds at least 1 extra query per page view.
 
Last edited:
@nocte
Ths helped me to find the way. Thanks. :)

Next question:
Is there a conditional to identify a widget by the widget key? I use the new posts widget more than one time, and I have to style two of them and here I need a condional to differ betwen 3 new posts widgets.

Stuff like:

HTML:
<xf:if is="$xf:widegt key == 'widgetkeyname'">
content
</xf:if>


Ideas? :)
 

I am not sure, if I fully understand the purpose, but how about this approach:

You create one or more widgets (e.g. widget_1, widget_2) and don't select any location.

Then create your widgets (as HTML widgets) with the locations were you want to display them and include the other ones like so:

HTML:
<xf:if is="YOUR_CONDITION">
    <xf:widget key="widget_1" />
<xf:else />
    <xf:widget key="widget_2" />
</xf:if>
 
The purpose is, this use case:

3 "latest posts" Xemforo core widgets like this:
  • one displays ALL new posts from all forums, but not forumB and forumC , widget key ist newpostA
  • one displays new posts from forumB, widget key is newpostB
  • and another one displays new posts from forumC, widget key is newpostC
Thats the easy part.

Now is the problem, that all three widgets the widget title link hard set to a side with ALL new posts - but thats only for newpostA a good idea.

I will now for newpostB a own title link to the forum the widget displays the posts from and for newpostC the similar thing but for another forum.

My idea was now, to make a template change (as a template addon) for the core widget template to change the widget title links based on the widget keys for newpostA and newpostB to the correct forum links.


I hope you understand what I mean, Iam not an english man. ;)
 
I hope you understand what I mean

o.k. I see. That is easy: make a template modification for the template widget_new_posts.

search for:
<a href="{$link}" rel="nofollow">{$title}</a>

replace with:
HTML:
                    <xf:if is="{$widget.key} == 'my_id'">
                        <a href="SOME_LINK" rel="nofollow">SOME_TITLE</a>
                    <xf:elseif is="{$widget.key} == 'my_other_id'" />
                        <a href="SOME_OTHER_LINK" rel="nofollow">SOME_OTHER_TITLE</a>
                    <xf:else />
                        <a href="{$link}" rel="nofollow">{$title}</a>
                    </xf:if>
 
Ok, so I was on the right way - thanks for your hints! :)

I mean Xenforo should do some work on there widgets to make them round with missed functions. But thats a other theme.
 
There is a litle problem...

I tryed this:
HTML:
                    <xf:if is="{$widget.key} == '20'">
                        <a href="{{ link('forums/plauderecke.117/') }}"" rel="nofollow">New Posts B</a>
                    <xf:else />
                        <a href="{$link}" rel="nofollow">{$title}</a>
                    </xf:if>
witch is the widget-ID number.

And I try this:
HTML:
                    <xf:if is="{$widget.key} == 'newpostB'">
                        <a href="{{ link('forums/plauderecke.117/') }}"" rel="nofollow">New Posts B</a>
                    <xf:else />
                        <a href="{$link}" rel="nofollow">{$title}</a>
                    </xf:if>
witch is the widget key from one widget.

I also try this:
HTML:
                    <xf:if is="{$xf__global.widget.key} == '20'">
                        <a href="{{ link('forums/plauderecke.117/') }}"" rel="nofollow">New Posts B</a>
                    <xf:else />
                        <a href="{$link}" rel="nofollow">{$title}</a>
                    </xf:if>

and this:
HTML:
                    <xf:if is="{$xf__global.widget.key} == 'newpostB'">
                        <a href="{{ link('forums/plauderecke.117/') }}"" rel="nofollow">New Posts B</a>
                    <xf:else />
                        <a href="{$link}" rel="nofollow">{$title}</a>
                    </xf:if>

The TMS found the correct place and replace it with my code the correct way. But...

In both cases, it uses the "else" content. I double check the widget ID and KEY - both where correct.


So I think there is some kind of more magic needed now. ;)
 
Last edited:
witch is the ID-number of the widget.
The ID does not matter in that case. Use the key.

First: Are you sure, you created a "new posts" widget (not a "new threads" widget for example)?

Second: try to insert {{ dump(vars()) }} in your templates, when you want to know what variables are available.
 
Top Bottom