• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 Widget Context

#1
Xenforo 2.0
Beta 7
Digital Ocean Droplet

Hi All,

Is there any way to make Thread/Posts widgets context sensitive?

So that for example if I place a new thread widget in a category sidebar, can I make it only shows threads from that category? it appears that configuring forum limit in the widget admin pushes the same forum threads and posts to the widget, immaterial of where it is placed. - Or maybe I've missed something obvious...?

I'm hoping I don't need to modify templates to achieve this.

Many thanks

Chris
 
#3
Hi Again,

I've tried inserting Chris D's example code into an HTML widget. This then renders in the Category sidebar.

Trouble is, my XF template code isn't working.

Code:
<xf:if is="$forum.node_id == 15">
    <xf:widget key="bmNewThread" />
</xf:if>
15 is the correct node ID and the bmNewThread widget renders if it's not inside the if clause - so am wondering if $forum.node_id is in scope at this point.

Clearly doing something wrong here as I assumed (wrongly?) that inserting a modified version of Chris D's example above, should work.

Any help would be great.

Thanks

Chris
 
Last edited:

Chris D

XenForo developer
Staff member
#6
Sorry I misread the "HTML widget" bit for some reason.

The description that comes up when that position is selected says that the "$category" is received as context though admittedly what this actually means isn't clear (the wording has been adjusted for the next release).

The code you would actually need is:
HTML:
<xf:if is="$context.category.node_id == 15">
    <xf:widget key="bmNewThread" />
</xf:if>
 

Chris D

XenForo developer
Staff member
#8
Actually, you're right.

It used to be there, but widgets can now be assigned to multiple positions so the description was dropped as there wasn't really room for it.

The position description is listed on the widget list, but some of the descriptions might be so long that they're clipped.

Looks like we still need to make this clearer :)
 

Chris D

XenForo developer
Staff member
#11
You can dump the context variable to see what's contained within:
Code:
{{ dump($context) }}
Though you'll find in most cases it just contains the one thing, e.g. in this case it will just contain the $category entity.

The other params you can't really dump to get a list of them but generally it will be just whatever data is needed for the widget to display.

Do you have something specific in mind that you need in this widget or another?
 
#12
You can dump the context variable to see what's contained within:
Code:
{{ dump($context) }}
Thanks, might be useful to make that ^ the default contents of the content section in the HTML widget. Give newbies a head start.

Do you have something specific in mind that you need in this widget or another?
We've just added iFramely to our in dev XF (see attached). Works very well for all those sites that aren't covered in BB media sites. Much quicker than creating BB media codes for each of the 20 plus sites we want to cover.

With this in mind, it would have been great to have added the iFramely code to a widget, which is several degrees of difficulty easier than altering templates or installing plugins - and probably a lot less prone to breaking following upgrades. Much more targeted too.

For this to be feasible I think you'd need to expose an ever present page object, available in all templates, widgets and crucially client side too. It wouldn't need to be the recursive monster you see in many MS cloud offerings, but adding simple things like tag objects, and hierarchical node structure showing name and type etc, would add lots of possibilities - as well having a consistent place to find the current view, controller, url etc.

1509388843963.png
 
Last edited:
#13
Hello @Chris D
im in the "category_view" template and try to add a widget abovte the forum node. Now i nee a unique variable.
If i try "<xf:widget key="{$category.title}" />" i get the message "Line 27: Template tag attribute "key" must be a literal string." and i cant use spaces in the widget key.
And "<xf:widget key="category_{$category.node_id}" />" is also not possible.
Is there a way to handle it with a variable?

at the moment I have solved it as follows, but i woud like the other way so i must not change the template every new category:
HTML:
            <xf:if is="$category.node_id == 3">        <xf:widget key="projektweit_3" />    </xf:if>
            <xf:if is="$category.node_id == 52">    <xf:widget key="altis_life_52" />    </xf:if>
            <xf:if is="$category.node_id == 78">    <xf:widget key="ark_78" />            </xf:if>
            <xf:if is="$category.node_id == 132">    <xf:widget key="pen_paper_132" />    </xf:if>
            <xf:if is="$category.node_id == 28">    <xf:widget key="off_topic_28" />    </xf:if>
            <xf:if is="$category.node_id == 22">    <xf:widget key="teambereich_22" />    </xf:if>
 
Last edited: