Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
3 posts Page 1 of 1
Hey, <cms:add /> tag is very comfortable to add 2 numbers:
Code: Select all
<cms:add '1' '2' />

For VERY many numbers it's better to have a smarter method with another tag.
Code: Select all
<cms:set total='0' />
<cms:set huge_line = '1,2,3,4,5,6,7,8,9,10,11,12' />
<cms:each huge_line sep=',' as='next_number'>
     <cms:incr total next_number />
<cms:show total />

But both tags don't help for very easy task of getting a sum of, say, just 4 numbers.
it's pretty ugly to add 4 numbers at once with default <cms:add/> tag. It would look like that:
Code: Select all
"<cms:add '1' "<cms:add '2' "<cms:add '3' '4' />" />" />"

Simple mod, placed in kfunctions.php allows to make unlimited adds like this:
Code: Select all
<cms:add '1' '2' '3' '4' />

Now code.
Code: Select all
$FUNCS->add_event_listener( 'alter_tag_add_execute', array('CustomTags', 'add_unlimited') );

class CustomTags{

   function add_unlimited( $tag_name, $params, $node, &$res ){
      if( count($params)<2 ) die( "ERROR: Tag \"".$node->name."\": requires two or more parameters" );
      $arr = array();
      for( $i = 0, $size = count($params); $i < $size; $i++ ){
         $arr[] = $params[$i]['rhs'];
      $res = array_sum( $arr );
      return 1;

If you are using this mod, please let me know. I will feel good about such work knowing it is helpful not only for me alone.

Just found in my old code another clumsy way of calculating multiple numbers, which I used in a form. Want to share it here.
Code: Select all
<cms:set total_submitted_hours="<cms:php>echo( <cms:show frm_hours_1 /> + <cms:show frm_hours_2 /> + <cms:show frm_hours_3 /> + <cms:show frm_hours_4 /> + <cms:show frm_break /> ); </cms:php>" scope='global' />

Of course I am replacing it now with
Code: Select all
<cms:set total_submitted_hours="<cms:add frm_hours_1  frm_hours_2  frm_hours_3 frm_hours_4  frm_break />" scope='global' />

In some cases it is good to visually separate parameters. Sample above can be rewritten like this, which will be the same:
Code: Select all
<cms:set total_submitted_hours="
                            n1 = frm_hours_1 
                            n2 = frm_hours_2 
                            n3 = frm_hours_3
                            n4 = frm_hours_4 
                            nn = frm_break />"
                scope='global' />
Hey, sometimes you want to make a bit more complex calculations. I know what it takes to do it with stock couch tags( not smart ).
Here is a refresher with cms:php tag, to solve the comlex formulae troubles:
Code: Select all
<cms:set var = '5' />
<cms:php>echo( <cms:concat '(' var '+' var '*' var ')' '/' var '-' var /> ); </cms:php>

In code above, good old echo prints result of the expression ( 5 + 5 * 5 )/5-5.

Or the stock couch way( I got lost on quotes):
Code: Select all
<cms:sub "<cms:div "<cms:add var "<cms:mul var var />" />" var />" var />
3 posts Page 1 of 1

Who is online

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

Users browsing this forum: No registered users and 2 guests