Problems, need help? Have a tip or advice? Post it here.
13 posts Page 1 of 2
I'm trying to add Couch to my existing site. I've used includes and passed variables for the HTML title. I'd like to continue doing this but I'm having what is likely a very simple issue, just not figuring it out. Basically, what I'd like to do on a blog template is have a stock HTML title or the listing and the post title as the HTML title on a post page. I thought the following would work:

<cms:if k_is_page>
<cms:php>
$pageTitle = "<cms:show k_page_title /> <br />";
echo($pageTitle);
</cms:php>
<cms:else />
<cms:php>
$pageTitle="Stock title";
</cms:php>
</cms:if>

Then have the HTML title echo $pageTitle. It's not working. I can't echo $pageTitle anywhere except in the HEAD tag. I'm sorry in advance for such a simple question, but Something isn't making sense.
Thanks
deleted
Thank, I guess I didn't explain myself clear enough. I'm using server-side includes for the header, footer, sidebar, etc. To enable my current site to have original HTML titles, I set a PHP variable at the beginning of the page ($pageTitle). This is then used in header.php. I understand that inner-mixing Couch into my existing site isn't a best practice, I see the ability to do this as a strength of Couch. We can code/design a site and then add in the components we need rather than rebuilding the entire site around a CMS.

I did figure out a work around and thought I'd post it incase someone else runs into the same problem. Rather than passing the couch variable to PHP, I just put the following in header.php:

<cms:if k_is_page>
<title><cms:show k_page_title /></title>
<cms:else />
<title><?php echo($pageTitle); ?></title>
</cms:if>

It works, but it only works on CouchCMS pages. So I end up with header.php and CMSheader.php. I was hoping to make the decision about the title, define $pageTitle as either the k_page_title or something else I type into the code of the page and pass that to header.php. So if anyone has and idea of how I can pass a CMS variable to a PHP variable, I'd really appreciate it.
Hi,

You said -
It works, but it only works on CouchCMS pages

Am I right in understanding that not all your templates are Couch managed and so the code does not work on those templates that are not handled by Couch?

Can you please confirm (or clarify if I am wrong)?

Thanks.
Correct...I have other static pages that are not couch templates. .
deleted
Yes, I believe that would work. I would prefer to pass the variable if possible. I'm foreseeing ways this might be handy in the future to pass Couch info to other functions or attributes of a page.

Anyone know if there is a way to do that?
See, the way Couch works, when a template is executed -
1. First all the native PHP code (i.e. code within <?php ?> blocks) gets executed
2. And it is only then that the Couch tags get executed.
The Couch tags can in turn execute PHP code but this can only be done using Couch <cms:php>..</cms:php> tags (and not the native <?php ?> tags).

As you can see, this sequence of execution makes it impossible to use Couch tags to set variables used by native PHP statments because by the time Couch tags come into action, PHP statements have already finished execution.

Normally, this limitation is not a problem as we can use <cms:php> tags to make Couch invoke PHP.
In your case, things are complicated because you want the same code to work with both Couch-managed templates as well as with non-mangaged ones.
So, we'll have to find a different tack.

As already discussed, we cannot use Couch to set native PHP staements. However, we can do the reverse i.e. make PHP set Couch tags.
This is how, I think, we can solve the problem.

You've already said that following code placed in header.php works when called from a Couch managed template -
Code: Select all
<cms:if k_is_page>
    <title><cms:show k_page_title /></title>
<cms:else />
    <title><?php echo($pageTitle); ?></title>
</cms:if>

The code above however (understandably) fails when header.php is included by non-Couch managed templates as they have no notion about the <cms:> tags.

To solve this, we can simply use PHP to figure out if the template is Couch-managed or not and output the code only if it is. The non-managed templates can use normal PHP. So our code in header.php can now become -
Code: Select all
// header.php
<?php if( defined('K_COUCH_DIR') ): ?>
    <cms:if k_is_page>
        <title><cms:show k_page_title /></title>
    <cms:else />
        <title><?php echo($pageTitle); ?></title>
    </cms:if>
<?php else: ?>
    <title><?php echo($pageTitle); ?></title>
<?php endif; ?>

This way a single header.php can be used for both kinds of templates like this -
Code: Select all
<?php $pageTitle="Stock Title"; ?>
<?php include( 'header.php' ); ?>

Does this do what you want? Please let me know.
I belive it does. Thanks so much for you help and for the excellent explaination of the execution order. I hadn't resized that. Thanks again.
You are welcome :)
13 posts Page 1 of 2