Problems, need help? Have a tip or advice? Post it here.
6 posts Page 1 of 1
Hello,

In this post http://www.couchcms.com/forum/viewtopic.php?f=2&t=8001&p=13610&hilit=meta+desc#p13610 there is a suggestion from KK on how to make SEO editable fields dynamic
if you can use single words as template titles


I thought that if I strip off the file suffix
Code: Select all
.php
from k_template_name I can use this instead of the method described thus avoiding the necessity of single word template titles.

My problem? I can't work out the php necessary to arrive at the variable template_name_minus_suffix as it would be used to get the meta description content ...
Code: Select all
<meta name="description" content="<cms:get_custom_field template_name_minus_suffix masterpage='globals.php' />">


Thanks!
The normal way to do this would be to explode the string using "." as a delimiter. But it turns out that cms:php strips the period out of template-name.php, returning "template-namephp" instead.

But that’s fine. Just use php as the delimiter.

Code: Select all
<cms:php>
    $pieces=explode("php",<cms:show k_template_name/>);
    $template_name_minus_suffix=$pieces[0];
    echo $template_name_minus_suffix;
</cms:php>

I didn't test it, but I think (hope) you can insert this code directly into your get_custom_field declaration like this:
Code: Select all
<meta name="description" content="<cms:get_custom_field <cms:php>
    $pieces=explode("php",<cms:show k_template_name/>);
    $template_name_minus_suffix=$pieces[0];
    echo $template_name_minus_suffix;
</cms:php> masterpage='globals.php' />">
@potato,
adding to what @tim suggested, you can try out the following as an alternative -

Place (or embed) the following somewhere before you use the 'template_name_minus_suffix' variable -
Code: Select all
<cms:php>
    global $FUNCS, $CTX;
    $path_parts = $FUNCS->pathinfo( $CTX->get('k_template_name') );
    $CTX->set( 'template_name_minus_suffix', $path_parts['filename'], 'global' );
</cms:php>

Now you can use the 'template_name_minus_suffix' variable anywhere you wish e.g.
Code: Select all
<meta name="description" content="<cms:get_custom_field template_name_minus_suffix masterpage='globals.php' />

Hope this helps.
Tim, I think the issue you mentioned is not related to cms:php, but a syntax error on your part. Strings must be quoted.
explode("php",<cms:show k_template_name/>)

explode("php","<cms:show k_template_name/>")
@cheesypoof
You're right. the correct syntax is:
Code: Select all
<cms:php>
    $pieces=explode(".","<cms:show k_template_name/>");
    $template_name_minus_suffix=$pieces[0];
    echo $template_name_minus_suffix;
</cms:php>
thank you all for your help ...

I used this:
Code: Select all
<cms:php>
    global $FUNCS, $CTX;
    $path_parts = $FUNCS->pathinfo( $CTX->get('k_template_name') );
    $path_parts = str_replace('-', '_', $path_parts);
    $CTX->set( 'template_name_minus_suffix', $path_parts['filename'], 'global' );
</cms:php>


I had to replace the hyphens that I tend to use in template names with underscores to make valid couch:editable names. Now this is working well!
6 posts Page 1 of 1