Identifying and using variables in templates, advertising positions, navigation entries, and widgets

Identifying and using variables in templates, advertising positions, navigation entries, and widgets

We've already discussed how varaiables are used in conditional statements.

However, there is a difference in the syntax used for templates and advertising positions, and navigation and widget display conditions.


In templates and advertising positions, the syntax takes this form for true:
HTML:
<xf:if is="$var">
HTML:
<xf:if is="$var == 'thing'">
HTML:
<xf:if is="in_array($var, ['thing', 'other_thing'])">

And this form for false:
HTML:
<xf:if is="!$var">
HTML:
<xf:if is="$var != 'thing'">
HTML:
<xf:if is="!in_array($var, ['thing', 'other_thing'])">


For navigation entries and widgets, the form is this for true:
HTML:
$var
HTML:
$var == 'thing'
HTML:
in_array($var, ['thing', 'other_thing'])

And this for false:
HTML:
!$var
HTML:
$var != 'thing'
HTML:
!in_array($var, ['thing', 'other_thing'])

If values are used in place of strings, then the ' ' are omitted, like this <xf:if is="$var == 1"> , or this in_array($var, [1,2]) .


Taking a real example of a logged in member only being able to view content, it would be this in a template or advertising position:
HTML:
<xf:if is="$xf.visitor.user_id">
    Show content to logged in member
</xf:if>

This for a navigation entry or widget:
HTML:
$xf.visitor.user_id


Another example to only show content if node 2 is being viewed would be this for templates and advertising positions:
HTML:
<xf:if is="$xf.reply.containerKey == 'node-2'">
    Show content in node 2
</xf:if>

And this for navigation entries and widgets:
HTML:
$xf.reply.containerKey == 'node-2'


A statement such as this would prevent a widget from being shown in styles with IDs 2, 4, and 8:
HTML:
!in_array($xf.style.style_id, [2,4,8])
A useful function is being able to pass variables from a template to the container, so they are available globally.

Take the thread ID for example, that typically isn't available outside the thread_view template.

We can make it available elsewhere by adding this to the thread_view template:
HTML:
<xf:page option="threadID">{$thread.thread_id}</xf:page>

What that code does is take the thread ID value from $thread.thread_id and assign it to a new variable called threadID.
You can name the option value whatever you want but ensure it does not clash with an existing variable.


That then makes it possible to call the thread ID in the PAGE_CONTAINER template using the option value as the var, which in this case is {$threadID}.

Expanding on that, conditional statements can be used to target a specific thread:
HTML:
<xf:if is="$threadID == 2">
    Content for thread 2 only
</xf:if>

Or multiple threads:
HTML:
<xf:if is="in_array($threadID, [2,8,20])">
    Content for threads 2, 8, and 20
</xf:if>


Where this really becomes useful though is in ad templates.
The thread ID can be utilised using $__globals.threadID.

Which allows you to target specific threads with different ads using a single ad template like this:
HTML:
<xf:if is="$__globals.threadID == 2">
    Content for thread 2
</xf:if>

<xf:if is="$__globals.threadID == 8">
    Content for thread 8
</xf:if>

Or this:
HTML:
<xf:if is="$__globals.threadID == 2">
    Content for thread 2
<xf:elseif is="$__globals.threadID == 8" />
    Content for thread 8
<xf:else />
    Content for other threads
</xf:if>
Back
Top Bottom