Forum for discussing general topics related to Couch.
8 posts Page 1 of 1
Why does this not print the variable within my pages:
Code: Select all
<cms:php>
$answer = 'X';
</cms:php>
<cms:php>
echo 'Answer:'.$answer;
</cms:php>

Still a newb, I guess!

To understand that, consider each <cms:php> block as a normal PHP function in terms of variable scopes e.g. your Couch code -
Code: Select all
<cms:php>
    $answer = 'X';
</cms:php>

<cms:php>
    echo 'Answer:'.$answer;
</cms:php>

would be equivalent of the following raw PHP code -
Code: Select all
function test(){
    $answer = 'X';
}
test();

function test2(){
    echo 'Answer:'.$answer;
}
test2();

Now, I am sure, you can see above that our PHP code would not output the variable as you are expecting.
To make them work, you'll have to do the following -
Code: Select all
function test(){
    global $answer;
    $answer = 'X';
}
test();

function test2(){
    global $answer;
    echo 'Answer:'.$answer;
}
test2();

Your Couch code would need exactly the same changes. Try the following -
Code: Select all
<cms:php>
    global $answer;
    $answer = 'X';
</cms:php>

<cms:php>
    global $answer;
    echo 'Answer:'.$answer;
</cms:php>

Hope this will help in understanding <cms:php> better.

So VERY enlightening!

YET, here I am, stuck again! I would REALLY love to see some definitive docs on the use of php with <cms:php> and cms variables.

I'm trying to manipulate a string and put it into a cms variable, and have worked HOURS on it. There's not more than a couple words about the $CTX->set() function anywhere on couchcms or the rest of the web. Here's what I have now just trying to get target to change value (ultimately the value in $var1):

Code: Select all
<cms:set target 'initial' />   
<cms:php>
   $var1 = 'test';
   $CTX->set('target' , 'xxx');
</cms:php>
<br />Target:<cms:show target /><br />


It crashes on the $CTX-> line with:
Code: Select all
Fatal error: Call to a member function set() on null in /home/mysite.com/couch/tags.php(2931) : eval()'d code on line 2


Besides being meaningless to me, where is "line 2" exactly so I could pinpoint my bad code easily?

Check out this topic. viewtopic.php?f=4&t=10044&p=23551&hilit=couch+variables+in+php#p23558
In short, $CTX is an pre-existing object of couchcms and your php function should be aware of its existance and refer to it, otherwise you are trying to call a fresh and empty variable "$CTX"..

Adding to what @trendoman correctly pointed out,
perhaps this sample code would make things clearer -
Code: Select all
<cms:set target='Hello' />   

<cms:php>
    global $CTX;
   
    // retrieve variable
    $var = $CTX->get( 'target' );
   
    // modify value
    $var = $var . ' World!';
   
    // set new value
    $CTX->set( 'target', $var );
</cms:php>

<br />Target: <cms:show target /><br />

Thanks so much for the continuing education, guys! Even with the global $CTX; I was still having problems. I "assumed" that I could use cms: commands WITHIN the cms:php confine. It appears that was wrong to some degree and that the $CTX->get() is to be used for retrieving cms variables as in this demo:
Code: Select all
<cms:set target1 = 'initial value' />   
<cms:php>
   global $CTX;
   $var1 = 'new value';
   $CTX->set('target1' , $var1);
   echo '<br />FAIL Inside the cms:php:<cms:show target1 />';
   echo '<br />WORKS Using $CTX in cms:php:' . $CTX->get('target1');
</cms:php>
<br />WORKS Outside the cms:php:<cms:show target1 /><hr />

I'm still foggy on why some cms commands can be used within cms:php and others can't because this works INSIDE the cms:php:
Code: Select all
echo '<br />Other CMS Inside the cms:php:<cms:show k_user_access_level />';

As you have noticed, we sure can use Couch tags within <cms:php>.
As to why then the <cms:show> wasn't working as expected in your code, you'll have to understand the execution flow of <cms:php>. Allow me to explain.

The tag works in two passes:
1. First it executes all the Couch tags within its enclosed contents to get a final PHP 'source code' to be evaluated as PHP
2. The output received above is then passed on to PHP interpretor to get the results.

Consider the following example -
Code: Select all
<cms:php>
    <cms:repeat '3'>
        echo '<cms:show k_count />';
    </cms:repeat>
</cms:php>

Here the <cms:php> tag in the first pass executes all enclosed Couch tags so after this pass the effective PHP code that is finally executed becomes as follows -
Code: Select all
<cms:php>
    echo '0';
    echo '1';
    echo '2';
</cms:php>

As you can see above, at the point the actual PHP code is executed, there are no Couch tags in there.
With that understood, I think it should be easy to see what is happenning in your code -
Original code -
Code: Select all
<cms:set target1 = 'initial value' />  
<cms:php>
   global $CTX;
   $var1 = 'new value';
   $CTX->set('target1' , $var1);
   echo '<br />FAIL Inside the cms:php:<cms:show target1 />';
   echo '<br />WORKS Using $CTX in cms:php:' . $CTX->get('target1');
</cms:php>

After first pass -
Code: Select all
<cms:set target1 = 'initial value' />  
<cms:php>
   global $CTX;
   $var1 = 'new value';
   $CTX->set('target1' , $var1);
   echo '<br />FAIL Inside the cms:php:initial value';
   echo '<br />WORKS Using $CTX in cms:php:' . $CTX->get('target1');
</cms:php>

And that would be the PHP code finally getting executed.
As you can see, the <cms:show> used inside would only print out the value of the variable as it was *before* the $CTX->set code changed it.

Anyway, that said, may I request you to let me know exactly what you are trying to achieve using the <cms:php> tack?
It has its own utility but you can quickly find yourself, as seems to be happening, befuddled trying to juggle two languages together.

For most part, the regular Couch tags suffice. For something not addressed, a better way would be to create a new custom Couch tag yourself.
Take a look at the 'Tips and trick' section and you'll find several addons created by our users that do just this.
8 posts Page 1 of 1
cron