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

XF 2.0 XF:set scoping

Ian Hitt

Active member
#1
It appears as though the scoping of xf:set has been changed in XF2? I have an xf:set in a template that I am directly including into page_container, but when I dump that variable in page-container, no dice.
 

Chris D

XenForo developer
Staff member
#2
That will have never worked (wouldn't have worked in XF1, either).

In XF1 the solution was:
Code:
<xen:container var="$var">Some string value</xen:container>
In XF2 the equivalent is:
Code:
<xf:page option="$var" value="$anyValueType" />
or
Code:
<xf:page option="$var">Some string value</xf:page>
 

Jake B.

Well-known member
#4
That will have never worked (wouldn't have worked in XF1, either).

In XF1 the solution was:
Code:
<xen:container var="$var">Some string value</xen:container>
In XF2 the equivalent is:
Code:
<xf:page option="$var" value="$anyValueType" />
or
Code:
<xf:page option="$var">Some string value</xf:page>
Just to be clear, we're including a template from within PAGE_CONTAINER (rather than in the content template) that contains several conditionals that we use a lot, it's working fine in xf1 but doesn't seem to be working in xf2

As an example, in PAGE_CONTAINER just below the doctype line:

Code:
<!doctype html>
<xf:include template="conditionals" />

{{ dump($someVariable) }}
and within "uix_conditionals" we'd have:
Code:
<xf:set var="$someVariable" value="1" />
We use this for things like welcome block, extended footer etc just so we don't have to duplicate conditionals, and we split it into a separate template to keep the PAGE_CONTAINER template cleaner and easier to merge down the line
 

Chris D

XenForo developer
Staff member
#5
<xf:set> would set the variable in the current content or container template and only be accessible in the content or container template it was set in.

<xf:page> would set the variable in the current content template and make it available in the container template.

Just to be clear, we're including a template from within PAGE_CONTAINER (rather than in the content template) that contains several conditionals that we use a lot, it's working fine in xf1 but doesn't seem to be working in xf2

As an example, in PAGE_CONTAINER just below the doctype line:

Code:
<!doctype html>
<xf:include template="conditionals" />

{{ dump($someVariable) }}
and within "uix_conditionals" we'd have:
Code:
<xf:set var="$someVariable" value="1" />
We use this for things like welcome block, extended footer etc just so we don't have to duplicate conditionals, and we split it into a separate template to keep the PAGE_CONTAINER template cleaner and easier to merge down the line
Indeed looks like I misunderstood the original message.

In this case, there is a difference between XF1 and XF2. The main one being that in XF1 a compiled template consisted of the "main" template and then each included template compiled inline within that main template.

In XF2 we compile each template separately.

In other words, whereas the compiled PAGE_CONTAINER template in XF1 would include the compiled "uix_conditionals" template, in XF2 it just includes a reference to it (it's just a call to the renderTemplate() method in the templater).

Although this is a net benefit overall, it does change the variable scope slightly.

I don't have a solution available off-hand though we'll give it some thought. (You might want to consider if there's some alternative approaches you can take in the mean time).
 
Last edited:

Kirby

Well-known member
#6
Does this work?

PAGE_CONTAINER
Code:
<xf:include template="uix_conditionals" />
<xf:set var="$uix_conditionals" value="{{ $xf.app.templater->pageParams.uix_conditionals }}" />
...
<xf:macro template="uix_macros" name="uix_extended_footer" arg-uix_conditionals="{$uix_conditionals}" />
uix_conditionals
Code:
<xf:page option="uix_conditionals.foo" value="1" />
<xf:page option="uix_conditionals.bar" value="0" />
uix_macros
Code:
<xf:macro name="uix_extended_footer" arg-uix_conditionals="!">
	<xf:if is="$uix_conditionals.foo">whatever</xf:if>
</xf:macro>
 
Last edited:

Jake B.

Well-known member
#7
(You might want to consider if there's some alternative approaches you can take in the mean time).
We're planning on moving these into our add-on since it's required anyways so it's not that big of a deal in this instance, but I suppose it could cause issues for other use cases just nothing else I can think of off hand :)
 

Jake B.

Well-known member
#8
Does this work?

PAGE_CONTAINER
Code:
<xf:include template="uix_conditionals" />
...
<xf:macro template="uix_macros" name="uix_extended_footer" arg-uix_conditionals="{$uix_conditionals}" />
uix_conditionals
Code:
<xf:page option="uix_conditionals.foo" value="1" />
<xf:page option="uix_conditionals.bar" value="0" />
uix_macros
Code:
<xf:macro name="uix_extnded_footer" arg-uix_conditionals="!">
    <xf:if is="$uix_conditionals.foo">whatever</xf:if>
</xf:macro>
I'd think that would run into the same issue, the variable isn't available at all in PAGE_CONTAINER with xf:set and I think xf:page only works in the content template