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

XF 1.2 Looping in CSS or template

Discussion in 'Styling and Customization Questions' started by Mike Creuzer, Nov 21, 2013.

  1. Mike Creuzer

    Mike Creuzer 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:

    <if is="in_array({$category.node_id}, @some_setting">some value</if>
    Thanks a lot!
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    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?
  3. Mike Creuzer

    Mike Creuzer Well-Known Member

    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)
  4. shadrxninga

    shadrxninga Well-Known Member

    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).

    <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.

        $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 :).
    <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: Nov 22, 2013
    Mike Creuzer likes this.
  5. Milano

    Milano Well-Known Member

  6. Mike Creuzer

    Mike Creuzer Well-Known Member

    @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.

Share This Page