Problems, need help? Have a tip or advice? Post it here.
9 posts Page 1 of 1
Hi,
I try to implement a SCSS Plugin which I have written for another CMS to get work in Couch.
It looks that the Couch php-tag have problems to identify that a closing curly bracket ist opend in a php-part before.

Code: Select all


        
// Update CSS file.
        file_put_contents($CSS, $buffer);
    }
}
 catch (Exception $e) {
</
cms:php>
    <dialog open>
        <article>
            <header>
                <strong>SCSS-Exception abgefangen</strong>
            </header>
            <p>
                <cms:php>
                    echo $e->getMessage() 
                
</cms:php>
            </p>
        </article>
    </dialog>
<
cms:php>
}
</
cms:php>


This is what Whoops give me out:
Bildschirmfoto 2023-07-24 um 11.59.00.jpg
Bildschirmfoto 2023-07-24 um 11.59.00.jpg (678.15 KiB) Viewed 735 times

Have I to write the code in a different manor?

Cheers
In short, this is not a problem of Couch or its tags. You are simply writing PHP code wrongly.

Tag <cms:php> evals the enclosed content as if it was a complete functional code, i.e. does not allow 'breaking', similar to PHP itself. Moreover, it seems, the use of this tag is completely unnesessary in this use-case — there are no other couch tags that need to be executed before giving way to PHP code.

You are thus advised to use a HEREDOC structure to echo the error message within a HTML. And study various approaches to the combo of HTML and PHP code (heredoc is only one approach of many). If you want to have couch tags in HTML sometime in future, a good practice is to set up a couch variable from within PHP or capture the PHP output to a variable (and test if its empty later).
In the regular PHP its allowed to stop and resatrt the parsing if you want to output HTML like:
As example:
Code: Select all
<?php
if($var):
?>
<span>Text</span>
<?php
endif
?>

Here is it also written in the official PHP documentaion: https://www.php.net/manual/en/language. ... hpmode.php
Does the Couch parser not work identically?

Cheers
PHP needs a closing tag ?> in your example akin to Couch's closing bracket > for both the opening <cms:if> tag and the closing </cms:if> tag. PHP's parser a bit more advanced though, I accept. I guess, Couch's parser is more restricted due to the chosen XML scheme. Following code is identical to your example though —
Code: Select all
<cms:if var>
<span>Text</span>
</cms:if>

<!-- could look very similar too: -->
<cms:if
var
>
<span>Text</span>
</cms:if
>

However Couch as a 'higher' level code must have correct code between the tags to evaluate it correctly in case of <cms:php>. It does not concatenate content of multiple tags, it can't evaluate a single closing curly brace }, because that's not a valid PHP code (you already know this).

Following should do —

ex. 1
Code: Select all
<cms:php debug='0'>
  try {
   throw new Exception("Some error here!");
  }
  catch(Exception $e) {
    echo 'Message for <cms:show k_user_title />: ' .$e->getMessage();
  }
</cms:php>
<br>

ex. 2
Code: Select all
<cms:php debug='0'>
  try {
   throw new Exception("Some error here!");
  }
  catch(Exception $e) {
    global $CTX;
    $CTX->set('error', $e->getMessage(), 'global');
  }
</cms:php>
<cms:if error><cms:show error /></cms:if>
<br>

ex. 3
Code: Select all
<!-- separately saved markup -->
<cms:func 'show-error' msg='' user=k_user_title><h3>Oops!</h3><p>Alert for <cms:show user /> <pre><cms:show msg /></pre></p></cms:func>

<cms:php debug='0'>
  try {
   throw new Exception("Some error here!");
  }
  catch(Exception $e) {
    global $CTX;
    $CTX->set('error', $e->getMessage(), 'global');
  }
</cms:php>
<cms:if error><cms:call_ex 'show-error' msg=error user='Admin'/></cms:if>
<br>


I hope you can spot the difference among them and adapt to your use case. :)
Okay I understand that the couch php parser can not concenate php-code if its not working alone.
But now I have another problem with the couch php, I try to generate a variable and run into the error
ERROR! ATTRIB_VALUE: Invalid first char "<" (line: 1 char: 25)


Code: Select all
<cms:set template_name = <cms:php>explode('.''<cms:show k_template_name />')[0]</cms:php> />
<
cms:show template_name /> 

I I try to wrap the content of the var with quotes I got an syntax error, unexpected end of file.
I need this to generate a class based on the template-name but without the suffix.

I'm sorry, but it is really annoying to write simplest php in Couch basiclly because I don't understand the concept behind or I don't know.
Intermixing PHP with Couch tags, though possible, can be tricky.
You may find it easier to code as follows where I have avoided this kind of intermixing by using $CTX->set()/get() in place of <cms:set /><cms:show />
Code: Select all
<cms:php>
    global $CTX;
   
    $template_name = $CTX->get( 'k_template_name' );
    $template_name = explode( '.', $template_name)[0];
    //echo( $template_name );
    $CTX->set( 'template_name', $template_name, 'global' );
</cms:php>

<cms:show template_name />

Hope this helps.
You should learn to write correct PHP. Semicolon, echo — these expected things were missing.
Code: Select all
<cms:set template_name = "<cms:php>echo explode('.', '<cms:show k_template_name />')[0];</cms:php>" />


Dealing with complex matters requires careful study or a loyal, forgiving and caring expert on your side, who you'd sincerely thank. I advise to purchase couch support for 3 months from @KK for your next question. :)


KDJFS wrote: Okay I understand that the couch php parser can not concenate php-code if its not working alone.
But now I have another problem with the couch php, I try to generate a variable and run into the error
@kk
Thanks for showing me the solution with a field.

@trendoman:
I can writing PHP but the PHP which is needed in Couch is a little bit oldschoolish. If you have a statement immediately folowed by a closing tag you can drop the semicolon: https://www.php.net/manual/en/language. ... ration.php

And the echo is also owed my normall writing of shorthand php witch is <?= for echo.
So my problems are more owed by <cms:php>. in the meanwhile it works mostly immediatly.

By the way thank to both of you for helping me!
KDJFS wrote: If you have a statement immediately folowed by a closing tag you can drop the semicolon:

Try to evaluate a code without a semicolon, that'll be an error as in — https://www.php.net/manual/en/function.eval

Apart from that the passed code must be valid PHP. This includes that all statements must be properly terminated using a semicolon. 'echo "Hi!"' for example will cause a parse error, whereas 'echo "Hi!";' will work.


If PHP changes how their eval works, that would help and work without a semicolon. It's the PHP's requirement for now. :)
9 posts Page 1 of 1