Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
10 posts Page 1 of 1
Hi, guys!

I'm using <cms:if my_var />
If my_var happens to have an empty space (not uncommon for left-out spaces in editables),
then it is not empty for the conditional. Any idea of validation?
cms:validator is here
viewtopic.php?f=5&t=8581
But it doesn't cover spaces. Maybe Alpha, but only with latin set.
If there isn't anything out of the box, consider it as suggestion.
Thanks!
http://docs.couchcms.com/tags-reference/not_empty.html

Code: Select all
<cms:if "<cms:not_empty my_var />" >
Dios mio!

Thanks, Tim :) Sometimes it's nice to get back to basics and also have a hand in the world, that can help.

I would like also to leave this as a suggestion for non-Latin character sets.
Code: Select all
<cms:if "<cms:not_empty '< ' />" >Not Empty!</cms:if>
Sorry, Tony. My response didn't really answer your problem with spaces.

The not_empty tag is aimed at richtext regions. It won't help where you accidentally leave some whitespace in an editable region. To protect against accidental whitespace, you could use some php to trim it away.

Code: Select all
<cms:set trimmed_var = "<cms:php>echo trim('<cms:show my_var />');</cms:php>"/>

<cms:if "<cms:not_empty trimmed_var />" >
    Not Empty!
<cms:else/>
    Empty!
</cms:if>


I hope that's a more useful suggestion.
So, I guess the final step would be to abstract away the php by putting it into a Couch tag.
Code: Select all
class TrimWhiteSpace{    
    function trim_whitespace( $params, $node ){
        global $FUNCS, $CTX;
        if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}
        return trim( $params[0]['rhs'] );
        }
    }
$FUNCS->register_tag( 'trim_whitespace', array('TrimWhiteSpace', 'trim_whitespace') );

Add this custom tag to your addons/kfunctions.php file. It removes whitespace from the beginning and end of the string passed to it. It also removes whitespace from an otherwise empty string:
Code: Select all
<cms:if "<cms:trim_whitespace my_var />" >
    <cms:show my_var />
<cms:else />
    Empty!
</cms:if>
I definitely vote to move this brilliant answer to Tips and Tricks.
Thanks!
move this brilliant answer to Tips and Tricks.
Done :)
@Tim, you mentioned
The not_empty tag is aimed at richtext regions. It won't help where you accidentally leave some whitespace in an editable region.

I was looking at the code for cms:not_empty tag and found that it applies trim() to the text remaining after stripping the HTML tags. So, I think, it should suffice for the kind of check we were looking for.

Could you please check and verify?
Thanks.
@KK, You're right. cms:not_empty does work to trim whitespace on text and textarea regions.

What confused me was that I had tested it on a richtext area. When you leave a forgotten space in a richtext region, then cms:not_empty returns false. So it appears that it doesn't trim whitespace in a richtext region because the whitespace is wrapped in html tags.

I mistakenly jumped to the wrong conclusion about the not_empty tag. It seems that it does work for the original purpose. Neither of these ideas will work for richtext, though. Spaces in richtext = not empty.
I was looking at the code for cms:not_empty tag and found that it applies trim() to the text remaining after stripping the HTML tags

My results seem to be at odds with this assertion.

EDIT
OK. I see what's going on. In some situations, the Richtext Editor html-encodes the spaces, so '&nbsp;' doesn't get stripped away.
I would add to this thread. It is not advisable to use not_empty for richtexts at all!

An image stored in richtext field without text also shows as empty with this tag. <img> is stripped, absolutely in line with the tag purpose and description.
10 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

cron