Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
5 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:each>
<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' />

Comfy!
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="
                 <cms:add
                            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 />
@trendoman

I placed the following in kfunctions.php, as you suggested.

$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;
}

}


What I have understood is that, after placing the above mod in the kfunctions.php file it will be possible to add multiple numbers as:
Code: Select all
<cms:add '1' '2' '3' '4' />


I have a clonable template. The template has a single editable as "number", that actually stores a whole number.
What I am doing to achieve the above is:
Code: Select all
<cms:set total='0' scope='global' />
<cms:add total "<cms:pages masterpage='page.php'>'<cms:show fees />'</cms:pages>" />
Total Sum = <cms:show total />


which I know is absolutely wrong. So how do I go about with it?

This is a great mod by what I could muster from your explanation. Only if I could get it to work.

Awaiting your feedback/reply.

Regards,
GenXCoders
Image
where innovation meets technology
Code: Select all
<cms:set total='0' scope='global' />
<cms:pages masterpage='page.php'>
    <cms:set total = "<cms:add total fees />" scope='global' />
<cms:pages />   
   
Total Sum = <cms:show total />
5 posts Page 1 of 1