XF 1.2 Looping in CSS or template

Dad.

Well-known member
Hey all,

I know you can loop in XenForo, but what I can't seem to figure out is how best to go about it.

Say I have a style property @some_list and I want to give it a comma delimited value of 1,2,3,4

How can I loop through it?

Also, is there a way to do like in_array in XenForo? What I mean is, lets say those 1,2,3,4 are node IDs and in template node_category_level_1 for example, can I do like:

Code:
<if is="in_array({$category.node_id}, @some_setting">some value</if>

Thanks a lot!
Mike
 
Setting the style property to be a comma separated list will cause it to be a sing of a comma separated list, not an array for you to do an in_array() on. What are you attempting to accomplish?
 
Setting the style property to be a comma separated list will cause it to be a sing of a comma separated list, not an array for you to do an in_array() on. What are you attempting to accomplish?
Yes I know, so I want the string converted to an array.

I want to be able to loop through a style property set list. (In this example, do something special if the node category is in the style property list)
 
Yes I know, so I want the string converted to an array.

I want to be able to loop through a style property set list. (In this example, do something special if the node category is in the style property list)
I don't think there is a way to do this... you need to turn the comma delimited string into an array to be able to do anything with it and unfortunately there isn't any functions or helpers in the Xenforo template system at the moment which can do that. (From what I could see, I took a look through both /library/Xenforo/Templates/Compiler.php and /library/Xenforo/Templates/Helpers/Core.php).

HTML:
<xen:if is="in_array(1, array(@test))">some value</xen:if>
The only thing which I thought might have worked was the above, I tried this as well as some variants on it but it only seems to create an array out of the first value in test, for example if -at-test was "1,2,3" then the code below would only return true for in_array(1, array(-at-test)) not and not in_array(2, array(-at-test)).

So the only way to do this would be to request a function such as explode be added to the list of allowed functions within conditionals. (Found in Compiler.php), or the ability for arrays to be used in style properties.

Code:
    $allowedFunctions = 'is_array|is_object|is_string|isset|empty'
            . '|array|array_key_exists|count|in_array|array_search'
            . '|preg_match|preg_match_all|strpos|stripos|strlen|trim'
            . '|ceil|floor|round|max|min|mt_rand|rand|explode|';

Once I had added 'explode' to this list I was able to do this - which works :).
HTML:
<xen:if is="in_array(1, explode(',', @test))">some value</xen:if>

Of course this would mean that the functionality would only work in later releases of Xenforo..., so the only other option would be to create an add-on that you install along side all your styles which would split the string into an array.

(There might be another solution...! but I couldn't find any within the $allowedFunctions or other functions available to templates.).
 
Last edited:
@shadrxninga, brilliant answer, I am satisfied with that albeit disappointed. Thank you for such a clear and thorough response.

Our styles have always been plugin-independent, and we might need to change that. But for now, I will use javascript.

Would love to here from XenForo if there is a reason explode is not an allowed function, just curious really I don't know if its a security reason or something of the sort.
 
Back
Top Bottom