Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
6 posts Page 1 of 1
This may seem small, but avoids php.
Can be used to divide values into parts, like in this example: ref_number and product_title.
'119108 - Some Amazing Product'
Reference #: 119108
Product Title: Some Amazing Product

Code: Select all
<cms:set msg='119108 - Some Amazing Product' />
<cms:each msg sep=' - ' >
   <cms:if k_count='0' >
      <cms:set ref_number=item 'global' />
   <cms:else />
      <cms:set product_title=item 'global' />
   </cms:if>
   <cms:dump />
</cms:each>

Reference #: <cms:show ref_number /><br/>
Product Title: <cms:show product_title />


Also helps when client's CSV is not well structured (not all values separated by columns..)
active topics, google
Free support is never free.. Donate!
It would be nice to know, how to get variable names dynamically. Like var[<cms:show k_count />].
active topics, google
Free support is never free.. Donate!
how to get variable names dynamically.
Sorry couldn't get you. Could you please elaborate on that point some more?
In my snippet above I invented 2 variables:

<cms:set ref_number=item 'global' />
<cms:set product_title=item 'global' />


I did this, because I know for sure, that output of cms:each consists of only 2 vars.

This method doens't work If no information is known beforehand on the size of array.
In such a case, it would be helpful to auto-create new variables.
var1
var2
var3
...etc....
var<cms:show k_count />

Is is a bit more clear? If not, I will try to provide more examples.
active topics, google
Free support is never free.. Donate!
Ok, I get it now :)

Normally we use <cms:set> to set a variable in Couch, e.g. as follows -
Code: Select all
<cms:set my_var='10' scope='global' />

The same statement can also be written using a different tag <cms:put> (using a slightly different syntax) as follows -
Code: Select all
<cms:put var="my_var" value='10' scope='global' />

Though the result of both is the same, the salient difference is that cms:put takes a string as the variable's name. This makes it possible to dynamically craft variable names (which is what you want).

As an example, try running the following code to see what I mean -
Code: Select all
<cms:repeat '4'>
    <cms:put var="my_var<cms:show k_count />" value=k_count  />
    <cms:dump />
</cms:repeat>

In the code above, each iteration of cms:repeat creates a new variable utilizing the k_count variable. Since the k_count variable starts with 0 on the first iteration and then increases with each iteration,, the code above creates variables named my_var0, my_var1, my_var2 and my_var3. Following is the dump -
Code: Select all
repeat
k_count: 0
my_var0: 0

repeat
k_count: 1
my_var0: 0
my_var1: 1

repeat
k_count: 2
my_var0: 0
my_var1: 1
my_var2: 2

repeat
k_count: 3
my_var0: 0
my_var1: 1
my_var2: 2
my_var3: 3

Hope it helps.
@KK, How many more Aces do you have in your sleeve? :lol:

Thank you.
active topics, google
Free support is never free.. Donate!
6 posts Page 1 of 1

Who is online

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

Users browsing this forum: Google [Bot] and 1 guest