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

XF 2.0 Arrays and properties

Russ

Well-known member
#1
So this works just fine.

Code:
{{ in_array($node.node_id, ['1', '2']) ? 'true' : 'false' }}
This does not:

Code:
{{ in_array($node.node_id, ['{{ property('customproperty1') }}', '{{ property('customproperty2') }}']) ? 'working' : 'stillworking' }}
customproperty1 and customproperty2 being just custom style properties that are # fields.

So... is there a way of having a single custom style property that you could fill in: '1', '2', '3' in the style property itself and load it into the array? Or do you have to have a box for each item you want? I think XF1 was the same way but just checking.
 

Chris D

XenForo developer
Staff member
#2
You don’t need any curlies around the properties; never nest them it’s not needed. You don’t need the ' around them either.
 

Russ

Well-known member
#3
You don’t need any curlies around the properties; never nest them it’s not needed. You don’t need the ' around them either.
Thanks got it, I hate conditionals :).

The last question (for now :)), is there a way to have a single box, say a string:

Code:
{{ in_array($node.node_id, [property('customproperty')]) ? 'working' : 'stillworking' }}">
And in that property filled out: 1,2,3

which would translate over to the array, in testing it appears that it will only work for the first digit even though multiple numbers are given.
 

Chris D

XenForo developer
Staff member
#5
Short answer, no, it's not possible.

But from Beta 2 onwards (coming very soon... ;)) you can now do this:
Code:
{{ in_array($node.node_id, property('customproperty')|split) ? 'working' : 'stillworking' }}">
We've added a split filter (the |split bit) which, with no arguments, will take a comma separated string and split it into an array. It is sort of the opposite of our join filter which will turn an array into a comma separated string.

You can pass other arguments in too:
Code:
$value|split('nl')
This will split a string on any new lines (supporting both \r and \n types) which is similar to how we split things like the list of disallowed usernames option etc.

Code:
$value|split('anything you like')
The previous two examples (nl and the default ,) are special cases and deal with things like users typing 1, 2,3,4, 5 (some with spaces, some with not) so they are a bit more robust. The final example literally allows you to set anything as the delimiter. Behind the scenes this is passed to PHP's explode function.
 

Russ

Well-known member
#6
@Chris D
Revisiting this finally...

Code:
{{ in_array($node.node_id, property('customproperty')|split) ? 'working' : 'stillworking' }}
Is outputting:

Code:
Template errors
Template public:node_list_category: Filter split is unknown (src/XF/Template/Templater.php:762)
Template public:node_list_category: Filter split is unknown (src/XF/Template/Templater.php:762)
Template public:node_list_category: Filter split is unknown (src/XF/Template/Templater.php:762)
Template public:node_list_category: Filter split is unknown (src/XF/Template/Templater.php:762)
Any idea why it may be causing it to throw errors? Beta 7, using the: node_list_category
 

Chris D

XenForo developer
Staff member
#7
Ah, a bug!

Seems when we added the split filter, we actually forgot to enable it.

You can add this yourself if you like by editing src/XF/Template/Templater.php and finding:
PHP:
'replace' => 'filterReplace',
And adding below it:
PHP:
'split' => 'filterSplit',