Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
9 posts Page 1 of 1
There is nothing quite like helpful as parsing strings with cms:each.

However, for many years CouchCMS had it wrong, outputting k_count starting with '0', which is counter intuitive for end-users. How 'count' can be zero in a non-empty string?
Also, it was always a overly-complicated task to get total number of elements in a separated string, since coders must introduce a new variable etc.., not to mention that total count of elements was not available before the cms:each ends its routine.

Respecting the default behavior( actually, respecting your existing codes with cms:each ) I made a simple tweak that allows to use system variable k_total_count or k_count_total ( 100% identical to make it easy to remember without looking into docs ) inside the cms:each loop on every pass.

If you, just like me, have common sense and like counting to start from '1' then use new parameter startcount and make it startcount='1'( similar to the same existing parameter in cms:repeat ). This way the last k_count and k_total_count will 100% match.
Code updated.
Inside the cms:each tag now available following variables:
Shows totals:
k_count, k_count_total, k_total_count, k_total, k_total_records
Shows current iteration:
k_step, k_current_record
Hi Trendoman,

cheers for this - it has been very useful.

This was our previous code to attempt to get an equivalent of k_total_records.

Code: Select all
<cms:each keywords sep=','>
   <cms:if k_count = '1'><cms:set max = '1' 'global'/></cms:if>
   <cms:if k_count = '2'><cms:set max = '2' 'global'/></cms:if>
   <cms:if k_count = '3'><cms:set max = '3' 'global'/></cms:if>
   <cms:if k_count = '4'><cms:set max = '3' 'global'/></cms:if>
</cms:each>


but now we can just replace the variable 'max' with k_total_records.

Thanks!
You are welcome :)


A sample code to implode a string with adding comma and space. Now possible the same tactic as in <cms:pages /> with
Code: Select all
<cms:if k_paginated_bottom = '0' >, </cms:if>


Code: Select all
<cms:each keywords sep=',' as='key' >
   <cms:show key /><cms:if k_current record lt k_total_records >, </cms:if>
</cms:each>
Important Update: This mod has influenced the CouchCMS development and now variables are available outright in default instance of <cms:each /> tag: k_total_items , k_count, k_first_item, k_last_item.
It is advisable to review every instance of your code that has <cms:each /> tag and rewrite it using native variables instead of modded ones. NB: It seems that <cms:each /> is used by the CouchCMS itself in addons, and it could be a source of issues as reported here https://www.couchcms.com/forum/viewtopi ... 994#p30994 so it is best to *not* use this mod at all, because it is not needed anymore.
Hi trendoman,
Exact Question : How can we remove that last comma in json as we can remove in cms:pages using
Code: Select all
<cms:if "<cms:not k_paginated_bottom/>">,</cms:if>


I am easily getting that in cms:pages
but i need that in cms:each :)
Code: Select all
<cms:set msg='hello|world|how|do|you|do' />
<cms:each msg sep='|' >
    <cms:show item />, <br>
</cms:each>

Output i will be getting is
Code: Select all
hello,
world,
how,
do,
you,
do,


now i want to remove that last comma.
Code: Select all
hello,
world,
how,
do,
you,
do



Thanks a lot in advance.
Hello Manu,

This post has influenced the Couch core and now cms:each supports more features without using this addon (do update your install though with the version from GitHub).

At the moment, the solution with comma -
Code: Select all
<cms:set msg='hello|world|how|do|you|do' />
<cms:each msg sep='|' >
    <cms:show item />
    <cms:if k_last_item = '0'>, <br></cms:if>
</cms:each>

Basically, k_last_item in cms:each bears the same sense as k_paginated_bottom in cms:pages. So following syntax can be used as well -
<cms:if "<cms:not k_last_item />">,</cms:if>


To see if your installation is already up to date, use -
Code: Select all
<cms:set msg='hello|world|how|do|you|do' />
<cms:each msg sep='|' >
    <cms:dump />
</cms:each>


Check if k_first_item / k_last_item vars are available.
Thank you It Worked.

:)

Response time in this forum is amazing. ;)
Manu wrote: Response time in this forum is amazing. ;)

You are welcome. Enjoy while it lasts :)

As of 2020, these are regular and still my custom parameters that modded <cms:each> has -
var, as, sep, key, startcount, token, is_json, is_regex, skip_empty, backwards, limit, count_only
One day Couch will swallow them all.. :mrgreen:
9 posts Page 1 of 1