XF 2.2 How to create a custom variables? That I can re-use on templates / template mod / advertising?

rdn

Well-known member
How to create several custom variables that I can re-use on templates / template mod / advertising?
All those variables will just store NODE IDs.

Instead of manually adding this kind of conditional:
<xf:if is="in_array($__globals.forum.node_id,['1','2','3','4','5'])">
<xf:if is="!in_array($__globals.forum.node_id,['1','2','3','4','5'])">

I need to create some custom conditional out of it, like:
<xf:if is="$private_forums">
<xf:if is="$public_forums">
<xf:if is="$optional_forums">

So it's easier for me to alter node ID for each variable on one place.
Thanks a lot.
 
Solution
I add it on PAGE_CONTAINER template, is that okay?
This might or might not work, depending on where you want to access the param.

XenForo template engine works like this:
The template to be used is defined in the controller by code like
PHP:
return $this->view($viewClass, $viewTemplate, $viewParams);

The defined template $viewTemplate (which might be smth. like thread_view does get rendered and while it is being rendered, all sub-templates/macros do get included.

Afterwards, template PAGE_CONTAINER does get rendered which does get the HTML of the rendered template as variable $content.

While rendering PAGE_CONTAINER, other templates (like the one for ad position...
The syntax is: <xf:set var="$array" value="{{ ['value_1', 'value_2', 'value_3'] }}" />.

However, in general, that will only be available to the template in which it is set.
To make vars globally available you would need to set them in code.
 
  • Like
Reactions: rdn
Hmm, I just tried

Code:
<xf:page option="myNodeIds" value="{{ [1, 2, 3] }}" />
in ad position Forum overview: Top and could dump the contents of myNodeIds in PAGE_CONTAINER. so seems to work just fine (for me)?
 
  • Wow
Reactions: rdn
Hmm, I just tried

Code:
<xf:page option="myNodeIds" value="{{ [1, 2, 3] }}" />
in ad position Forum overview: Top and could dump the contents of myNodeIds in PAGE_CONTAINER. so seems to work just fine (for me)?
Great, so with your sample code, I can make my conditionals like this:
<xf:if is="in_array($__globals.forum.node_id,$myNodeIds)">
<xf:if is="!in_array($__globals.forum.node_id,$myNodeIds)">

or?
<xf:if is="in_array($__globals.forum.node_id.myNodeIds)">
<xf:if is="!in_array($__globals.forum.node_id.myNodeIds)">

or?
<xf:if is="in_array($__globals.forum.node_id,[$myNodeIds])">
<xf:if is="!in_array($__globals.forum.node_id,[$myNodeIds])">

Please guide :)
 
I don't quite understand what you are trying to do so it is a bit difficult to answer ;)

Maybe you could explain a bit more what exactly you would like to achieve?

Assuming you've set page param myNodeIds prior to acccessing it anywhere you could use it in conditions like

Code:
<xf:if is="in_array($nodeIdVariable, page_param('myNodeIds'))">
 
The scenario is this...
I have 3 kind of forums/nodes:
  1. Public
  2. Private
  3. Optional
Those 3 group, I served different ads.
I add manually the ad units on XF Advertising option.
Each ad unit I manually include the in_array($__globals.forum.node_id

Include once the JS code via Template Modification.
Each template mod also I include in_array($__globals.forum.node_id

I would like to update the NODE IDs on each group in one place if possible, to make it easier, less issues.
So I'm thinking of adding those custom variable set on PAGE_CONTAINER template.

Like:
  1. $Public_Node
  2. $Private_Node
  3. $Optional_Node
 
I add it on PAGE_CONTAINER template, is that okay?
This might or might not work, depending on where you want to access the param.

XenForo template engine works like this:
The template to be used is defined in the controller by code like
PHP:
return $this->view($viewClass, $viewTemplate, $viewParams);

The defined template $viewTemplate (which might be smth. like thread_view does get rendered and while it is being rendered, all sub-templates/macros do get included.

Afterwards, template PAGE_CONTAINER does get rendered which does get the HTML of the rendered template as variable $content.

While rendering PAGE_CONTAINER, other templates (like the one for ad position container_header) do get included.

So if you define a page param right at the top of PAGE_CONTAINER you can use it in templates that are included by PAGE_CONTAINER; however you can't use it in templates like thread_view (and templates included by those templates) as those are rendered before rendering of PAGE_CONTAINER has started.

So for your specific question if depends on the ad location where you would to use the param:
  • container_breadcrumb_top_above
  • container_breadcrumb_top_below
  • container_header
  • container_sidenav_above
  • container_sidenav_below
  • container_content_above
  • container_content_below
  • container_sidebar_above
  • container_sidebar_below
  • container_breadcrumb_bottom_above
  • container_breadcrumb_bottom_below
would work - others (eg. those within specific templates) would not.

A generic approach purely with templates that should always work:

Create a template my_node_ids
Code:
<xf:page option="publicNodeIds" value="{{ [1, 2, 3] }}" />
<xf:page option="privateNodeIds" value="{{ [4, 5, 6] }}" />
<xf:page option="optionalNodeIds" value="{{ [7, 8, 9] }}" />

and include this everywhere you need your checks like
Code:
<xf:include template="my_node_ids" />

Afterwards you could use conditions like
Code:
<xf:if is="in_array($__globals.forum.node_id, page_param('publicNodeIds'))">

Though this isn't terribly efficient and kinda hacky, using an Add-on would be cleaner

You could also just use a configuration array in config.php
PHP:
$config['rdn_publicNodeIds'] = [1, 2, 3];

This could be accessed in any templates via
Code:
<xf:if is="in_array($__globals.forum.node_id, $xf.app.config.rdn_publicNodeIds)">
 
Last edited:
Solution
In short, I need an addon to make it work with all the templates / ad position / template mod?
No. See my previous answer, you can achieve this just with templates.
But I probably would not do it this way (eg. via include templates).
 
Last edited:
  • Like
Reactions: rdn
You could also just use a configuration array in config.php
PHP:
$config['rdn_publicNodeIds'] = [1, 2. 3];
This could be accessed in any templates via
Code:
<xf:if is="in_array($__globals.forum.node_id, $xf.app.config.rdn_publicNodeIds)">
This is the best approach, and it works great on my testing.
Thank you so much.
 
Top Bottom