XF 1.2 Template conditional

dieg0

Active member
Hello,

I new to xF conditionals and it is taking me quite a while to turn the following code into one

Code:
in_array($this->_params['thread']['node_id'], XenForo_Application::get('options')->get('dppa_insidepost_forceforums'))

And this is what I trying right before I gave up

Code:
<xen:if is="(in_array($this->_params['thread']['node_id'], XenForo_Application::get('options')->get('dppa_insidepost_forceforums'))">

I figured I would be better off asking here :unsure:
 
Yeah the syntax available to templates is a fair bit simpler than PHP, but it amounts to the same thing.

Any keys available in $this->_params are available to the template directly...

e.g. $this->_params['thread'] in your controller or view class would be, simply, {$thread} in XenForo template syntax.

That's a worthy difference to note, also. Whereas variables in PHP are defined, such as: $var in XenForo they would be defined as {$var} and whereas your PHP may be: $var['something'] in XenForo templates it would be: {$var.something}

So there's quite a bit of difference.

Another thing, is, you can't access PHP classes (e.g. XenForo_Application) directly in templates. But certain things like the visitor object e.g. {$visitor} and options e.g. {$xenOptions} are always available to all templates.

Saying all that, there are some PHP functions (not many) that are available to templates, one of those is in_array... so... To translate your code from PHP to XenForo template syntax it would simply be:

Code:
<xen:if is="in_array({$thread.node_id}, {$xenOptions.dppa_insidepost_forceforums})">

That should do the trick nicely.
 
Helo Chris,

I don't get any more errors from the template system however the condition is never true. Maybe I don't have access to $xenOptions.dppa_insidepost_forceforums from the template?

Thanks
 
$xenOptions is always available to templates.

Try adding this somewhere:

{xen:helper dump, $xenOptions.dppa_insidepost_forceforums}

That will dump the contents of that variable onto the screen. If it doesn't contain the expected results then it's likely to be an issue with how the option is set up in the admin CP (or maybe you're using the wrong ID or similar?).

To further prove that the options are indeed available to the template you can do:

{xen:helper dump, $xenOptions}

That will dump the entire {$xenOptions} array.
 
hello,

This is what I got from {xen:helper dump, $xenOptions.dppa_insidepost_forceforums}

Code:
array(63) {
  [0] => string(1) "3"
  [1] => string(2) "93"
  [2] => string(2) "78"
  [3] => string(1) "4"
  [4] => string(1) "5"
  [5] => string(2) "58"
  [6] => string(3) "124"
  [7] => string(2) "56"
  [8] => string(3) "119"
  [9] => string(2) "62"
  [10] => string(3) "100"
  [11] => string(1) "8"
  [12] => string(2) "54"
  [13] => string(2) "64"
  [14] => string(1) "9"
  [15] => string(2) "81"
  [16] => string(2) "10"
  [17] => string(3) "101"
  [18] => string(3) "123"
  [19] => string(3) "118"
  [20] => string(3) "104"
  [21] => string(2) "11"
  [22] => string(2) "68"
  [23] => string(2) "69"
  [24] => string(3) "117"
  [25] => string(2) "70"
  [26] => string(2) "84"
  [27] => string(2) "76"
  [28] => string(2) "30"
  [29] => string(2) "32"
  [30] => string(3) "108"
  [31] => string(3) "103"
  [32] => string(2) "83"
  [33] => string(2) "40"
  [34] => string(2) "42"
  [35] => string(2) "74"
  [36] => string(2) "72"
  [37] => string(3) "113"
  [38] => string(3) "115"
  [39] => string(2) "82"
  [40] => string(2) "73"
  [41] => string(3) "110"
  [42] => string(3) "111"
  [43] => string(3) "112"
  [44] => string(3) "114"
  [45] => string(2) "60"
  [46] => string(2) "52"
  [47] => string(2) "97"
  [48] => string(2) "98"
  [49] => string(3) "121"
  [50] => string(2) "39"
  [51] => string(2) "77"
  [52] => string(2) "99"
  [53] => string(2) "22"
  [54] => string(2) "24"
  [55] => string(3) "120"
  [56] => string(2) "96"
  [57] => string(3) "106"
  [58] => string(3) "105"
  [59] => string(2) "80"
  [60] => string(2) "79"
  [61] => string(3) "122"
  [62] => string(3) "107"
}

however {xen:helper dump, $thread.node_id} returns NULL o_O
 
What template are you using that code in?

From your PHP example, I assumed that the thread record was being sent to your template in $this->_params

Not all variables are available to all templates.
 
Try passing the variable using something like <xen:container var="$threadNodeId">{$thread.node_id}</xen:container>
Do you min doing something like this
Code:
<xen:if is="in_array(<xen:container var="$threadNodeId">{$thread.node_id}</xen:container>, {$xenOptions.dppa_insidepost_forceforums})">
What template are you using that code in?

From your PHP example, I assumed that the thread record was being sent to your template in $this->_params

Not all variables are available to all templates.
ad_above_top_breadcrumb
 
You'd need to set the container var in a template that does have the $thread record available.

So you could put this in your thread view template:

<xen:container var="$threadNodeId">{$thread.node_id}</xen:container>

Which would make the node ID available outside of the thread_view template using {$threadNodeId}.

However, you might just want to try:

{$forum.node_id}

The $forum record is already added to the container elsewhere in the code. So {$forum.node_id} should work fine.

Code:
<xen:if is="in_array({$forum.node_id}, {$xenOptions.dppa_insidepost_forceforums})">
 
Top Bottom