Forum for discussing general topics related to Couch.
12 posts Page 1 of 2
Help, trying to get this to work and it does not...

<cms:set seo_title="<cms:editable name='seo_title' type='text' />" />
<cms:php> $title = "<cms:show seo_title />"; </cms:php>

The PHP variable $title is blank after these lines? There is some text in the couch variable seo_title.

Cheers
Jonathan
Hi,

Could you please post more of your code?
Specifically showing, where and how are you using the PHP variable further down in it.
Basically, the template sets up a PHP variable and then calls a header.php to add all the header stuff for the web page. The variable is used to populate the 'title tag' of the page

One element is the title tag - which is passed as a php variable:

Template:

Code: Select all
<?php
require_once( 'xxx/cms.php' );
?>
<cms:set seo_title="<cms:editable name='seo_title' type='text' />" />
<cms:php> $title = '<cms:show seo_title />'; </cms:php>
<?php
require('includes/head.php');
<?


head.php top:

Code: Select all
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <title><?php echo $title; ?></title>


.... etc...
Thanks.

I think the following discussion should help -
viewtopic.php?f=2&t=10763#p27290

Basically what is needed is the 'global' keyword in PHP code.
Thanks but that does not do it. Global is used for multiple php files and function scope.

For example; if I replace the $title = "<cms:show.... " with $title = "some text";

It all works and has done for many years with Couch and PHP. It's only when I set the $title variable with a <cms:show... (e.g. when couch has to replace the content of the variable $title with a show statement.

For some reason the <cms:show is not displaying or outputting the content of the show before the PHP processes the include statement.

BTW the include PHP statement works as if the file (PHP) were part of the original file and as such the Global scope is not needed. The $title is also not used within any functions so local scope is not affected either.

Cheers
Jonthan
If you remind yourself that PHP code executes before Couch unless it is wrapped in <cms:php> block, then perhaps it would be clear what the problem is.
Taking a second look at your code made me realise that there is a different issue at work as well.
Code: Select all
<cms:php> $title = '<cms:show seo_title />'; </cms:php>
<?php
require('includes/head.php');
<?

In the code above, there are two blocks executing PHP code -
one is the native (<?php .. ?>) and the other is a Couch tag (<cms:php>..</cms:php>).

Now, the way things work in Couch, the native PHP gets the first shot. Only when it has fully executed, that Couch even gets to execute its first instruction.

So, coming to the code referenced above, although the Couch block of PHP comes before the native block, in reality the native part executed first. Which means that the code in your head.php executes before <cms:php> could set the variable.
And so, the following outputs a blank -
Code: Select all
<title><?php echo $title; ?></title>

Alright, so what is the solution?
Though it can be done, it'll get unwieldy pretty soon if you try make native PHP blocks and their Couch counterparts work cooperatively.

The best way would be to forgo using require('includes/head.php'); and, instead, use a normal Couch snippet (with <cms:embed>. It shouldn't be too difficult to port the PHP code within head.php to do things using Couch tags.

Hope this helps.
Yes, I thought there would perhaps be an issue with the php excuting before the <cms:php...

Ok, so the issue is that I have generic header for all pages (logical) and part of the header has to be some SEO stuff (title tag, meta description, etc.). This needs to be edited by the "user" within the UI backend. So rather than hard code the $title (for example) I have added the couch variable seo_title. The issue is 'how' can I pass this variable to the php head.php file so that it will output the correct info in the head section of the html?

So far (with fairly static pages), this is not an issue because you can manually edit the php page and edit the $title variable, and then include the head.php which in turn outputs the correct SEO content for the head. How do we do the same with Couch so that the seo_title (for example) can be a user variable edited in the backend for the page.

I might add this is only because these last pages are created from a template that is repeated for new content and therefore not static pages, but couch created urls.

Cheers
Jonathan
As I suggested in my last post -
The best way would be to forgo using require('includes/head.php'); and, instead, use a normal Couch snippet (with <cms:embed>. It shouldn't be too difficult to port the PHP code within head.php to do things using Couch tags.
Is

Code: Select all
<cms:embed>


The same as:

Code: Select all
<?php require ?>


---

I'd also have to have 2 (sort of embeds) as there are 100's of pages that have the usual header e.g. <?php require...

And I would not want to have to change all these to <cms:embed...

I'd rather have a conditional embed that says something like:

Code: Select all
If variable x = true
  <cms:embed...
Else
  <?php require...
End


Which would save me hours of manually editing existing php files.

Cheers
Jonathan
12 posts Page 1 of 2