Important announcements from CouchCMS team
14 posts Page 2 of 2
Adding a few moments to previous post. Arrays declaration. Arrays are in [] , Objects are in {}

Create a first empty array
Code: Select all
<cms:set person = '[]' scope='global' is_json='1' />

Add to it one object, consisting of a key 'First Name' and a value 'Jane'.
Code: Select all
<cms:set person. = '{"First Name" : "Jane"}' scope='global' is_json='1' />

Alternative way to do the above 2 operations in one go is:
Code: Select all
<cms:set person = '[{"First Name" : "Jane"}]' scope='global' is_json='1' />
active topics, google
Free support is never free.. Donate!
If anyone reads this topic, here is a sample of converting output of listing tag (query, pages, folders etc) to JSON and JSON to ul-li list. viewtopic.php?f=2&t=10262&p=28787#p28787
active topics, google
Free support is never free.. Donate!
KK wrote: recursively iterating through all levels


I was curious and came up with a solution, using a snippet to make things easy.

Sample test 4-level deep array will be created by php just to test things.
Code: Select all
<cms:php>
    $a = array_fill(0, 2, array_fill(0, 2, array_fill(0, 3, array_fill(0, 2, 0))));
    global $CTX;
    $CTX->set('test_array', $a, 'global');
</cms:php>


Then there will be a snippet file, let's say "loop.html", placed in /snippets as usual and with following code (calling itself!):
Code: Select all
<cms:ignore>
/*
    It loops and calls itself to build a multi-dimensional ul-li out of array.
   
    Sample to call this snippet:
   
    <cms:set item = some_array />
    <cms:embed 'loop.html'/> 

*/
</cms:ignore>


<cms:if "<cms:not limit_array_levels />">
    <cms:set limit_array_levels = '10' scope='global'/><cms:ignore>A reasonable limit of nested levels</cms:ignore>
</cms:if>

<ul>
<cms:each item>
    <cms:if "<cms:is_array item />">
    <li><kbd><cms:show key /></kbd>
        <cms:if startcount lt limit_array_levels >
        <cms:embed 'loop.html' startcount="<cms:add startcount '1' />" ></cms:embed>
        <cms:else />
            : Array (<cms:array_count item />)
        </cms:if>
    </li>
    <cms:else />
    <li><kbd><cms:show key /></kbd> : <cms:show item /></li>
    </cms:if>                       
</cms:each>
</ul>


Now with this snippet, all we need to do to build an unordered list ul-li is to call this snippet. For our experimental "test_array" it looks like this:
Code: Select all
<cms:set item = test_array />
<cms:embed 'loop.html' />


Browser shows it as following:
2018-03-02-001.png
2018-03-02-001.png (5.66 KiB) Viewed 655 times


Most practical use-case for me is to pleasantly navigate through dataTable's arguments that it sends over ajax to backend:
2018-03-02-002.png
2018-03-02-002.png (9.19 KiB) Viewed 655 times


There is a built-in option to control how many levels are visible.
Hopefully this little trick with recursion helps me many times.
active topics, google
Free support is never free.. Donate!
Json pretty print tag viewtopic.php?f=8&t=11466
active topics, google
Free support is never free.. Donate!
14 posts Page 2 of 2

Who is online

In total there is 1 user online :: 0 registered, 0 hidden and 1 guest
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 1 guest