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
Hello all.
First, Couch is awesome. Thank you to KK, cheesypoof, and everyone involved in the enthusiastic development and upkeep of this system. It is absolutely wonderful and extremely generous. :D :D :D
Second, this is not a super-urgent issue. I am not only doing this for myself, but if a solution is figured out, I'm sure it would help a lot of people. It's just one of those little extras that polish a site.

My issue:
I am building a blog and am trying to see if there is a way to give a contextual apostrophe at the end of a name that is pulled from a k_page_title. The page title comes from the 'about.php' and I'll be pulling it for something like <cms:show k_page_title /> 's Blog, or Social, or Portfolio, or Hobbies, or anything like that.

What I need to happen is to see if the last letter of the name is an 's', 'x' or 'z' to only add an apostrophe( ' ), but any other letter to add the apos-s ( 's ).
Is there a cms tag I can use to create that?

Thanks for any help.
Thank you very much for the kind words, scratz :)
What I need to happen is to see if the last letter of the name is an 's', 'x' or 'z' to only add an apostrophe( ' ), but any other letter to add the apos-s ( 's ).
Is there a cms tag I can use to create that?
There is no such tag out of the box, I'm afraid.

However, we can always create our own tag if there in none that suits our purpose :)

Please paste the following in your 'couch/addons/kfunctions.php' file (rename 'kfunctions.example.php' to 'kfunctions.php' if this file is not found).
Code: Select all
function possessive_handler( $params, $node ){
    global $FUNCS;

    if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}

    extract( $FUNCS->get_named_vars(
                array( 'word'=>'',
                     ),
                $params)
    );

    $word = trim( $word );

    return $word .= ( preg_match('/[sxz]$/i', $word ) ? "'" : "'s" );
}
$FUNCS->register_tag( 'possessive', 'possessive_handler' );

And now you have a tag named <cms:possessive> that does exactly what you specified (i.e. add a trailing apostrophe for all words except those ending with 's', 'x' and 'z').

Try out -
Code: Select all
<cms:possessive 'Scratz' /> post.
<cms:possessive 'KK' /> post.

Of course, like all tags you can use variables instead of hardcode strings -
Code: Select all
<cms:possessive k_page_title /> post.

Hope it helps. Do let me know.
KK your mind is a machine. I thank you so much. :lol: It works like a charm!!

So how do I move this string to "tips and tricks" or do I just copy/paste it over there?
I'm glad it helped.
I'll move the post to 'Tips and tricks' for you :)
Hi :)

KK, you are too fast, Sir :lol:

My example:
I suggest to try first check if k_page_title is set:
Code: Select all
<cms:if k_page_title >
</cms:if>

Then let's extract the last letter with little php code. Result will be put in variable 'last_letter':
Code: Select all
<cms:if k_page_title >

<cms:set last_letter="<cms:php>echo mb_substr(trim(<cms:show k_page_title />), -1);</cms:php>" />

</cms:if>

Now, we have the last_letter and can use 'if' to store final result in variable 'page_title'
Code: Select all
<cms:if k_page_title >

<cms:set last_letter="<cms:php>echo mb_substr(trim(<cms:show k_page_title />), -1);</cms:php>" />

   <cms:if (last_letter eq 's') || (last_letter eq 'x') || (last_letter eq 'z') >
      <cms:set page_title = "<cms:concat k_page_title '\'' />" />

   <cms:else />
      <cms:set page_title = "<cms:concat k_page_title '\'s' />" />
   </cms:if>
</cms:if>

And, finally, show our title:
Code: Select all
<title><cms:show page_title /></title>


Let me know, if this is what you want! :D
5 posts Page 1 of 1
cron