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
I needed a random tag and couldn't find the tag in forum. So I wrote it my own.

Put the following code to 'couch/addons/k_functions.php':

Code: Select all
$FUNCS->register_tag('rand', function($params, $node){
    global $FUNCS;

    // Extract parameters and set default values
    $parameters = $FUNCS->get_named_vars(
            'min' => 0,
            'max' => 10
    // Ensure min and max are integers
    $min = intval($min);
    $max = intval($max);
    // Ensure max is greater than min
    if($max <= $min) {
        $max = $min + 10;

    // Return random number between min and max
    return rand($min, $max);

min is 0 and max is 10 by default. The use is simple:

Code: Select all
Random dice: <cms:rand min="1" max="6" />
Random avatar: <img src="<cms:rand min='1' max='70' />">
The presented code has a hidden problem: <cms:rand '10' '10' /> works as <cms:rand '10' '20' /> e.g.. may produce 18 as a value.
1945 - 2023
I suggest that instead of of explicit 'min' and 'max', let us have 'from' and 'to' which suggests a range (and hence can go in either direction). Internally in the code we can figure out which of the two is max and min (because PHP rand() expects a proper sequence of parameters).

Both params can be optional so internally the default values for 'min' can be 0 and for 'max' it can be PHP getrandmax().
Code: Select all
<cms:rand /> // 0 to getrandmax
<cms:rand '10' /> // 10 to getrandmax
<cms:rand from='10' /> // -ditto-
<cms:rand to='10' /> // 0 to 10
<cms:rand from=1 to='10' /> // 1 to 10
<cms:rand from=10 to='1' /> // -ditto-
<cms:rand from=10 to='10' /> // 10 (you asked for it!)

KK wrote: I suggest that instead of of explicit 'min' and 'max', let us have 'from' and 'to' which suggests a range.

That is a good idea:)
I usually just write something like

Code: Select all
<cms:new 'numberBetween' int1='0' int2='2147483647' />

Tag cms:new (I can send) does a few more useful things with randomity, namely exclude certain numbers or select from arrays e.g. random letters —

Code: Select all
<cms:new 'randomDigit' />
<cms:new 'randomDigitNotNull' />
<cms:new 'randomDigitNot' except='5' />

<cms:new 'numberBetween' int1='0' int2='2147483647' />
<cms:new 'randomNumber' nbDigits='' strict='' />
<cms:new 'randomFloat' nbMaxDecimals='' min='0' max='' />

<cms:new 'randomLetter' />
<cms:new 'randomAscii' />

<cms:new 'randomElements' array='["a","b","c"]' count='1' allowDuplicates='' />
<cms:new 'randomElement' array='["a","b","c"]' />

@KK, your suggestion for the cms:rand reflects much better planning. Usage of simple tags should be most natural and intuitive. From and to are quite intuitive and expected.
1945 - 2023
While we are at randoms, I have a few funcs (can send too) to quickly make random stuff:

random-name, -char, -digit, -sign

Code: Select all
<cms:call 'random-name' lenght='7' set='ol'/>

Code: Select all
<cms:call 'random-sign' set='↑↓→←'/>

Solves almost all necessity since length and set are tweakable. All functions correctly support UTF-8 charset, if that's important for your site's language.
1945 - 2023
6 posts Page 1 of 1