by
KK » Sun Jan 17, 2016 4:27 pm
I tried doing this, but not only did it make my code completely unreadable due to the errors it caused, but it required me to change lots of my code as it is all dependent on other things.
Now that it is established that using <cms:php> is the only available way forward, let us try and remove the problems you mentioned to make things easier.
First of all, let us work on the existing PHP code that you have.
Chances are that it would all be in a global scope. If so, refactor it to move all the code to within a function and then make a call to that function e.g. if the original was -
- Code: Select all
<?php
blah..
blahh $page_id..
..
..
bla..
?>
Refactor it to make it as follows -
- Code: Select all
<?php
// move all the relevant code to within a function ..
function my_func( $page_id ){
blah..
blahh $page_id..
..
..
bla..
}
// invoke the function ..
my_func( 120 );
?>
You might have to make some adjustments but those should be straightforward as we'd be dealing straight with PHP (as opposed to through Couch).
Once you have the existing PHP code working, we'll only have to port the single line function call to Couch as follows -
- Code: Select all
<?php
// move all the relevant code to within a function ..
function my_func( $page_id ){
blah..
blahh $page_id..
..
..
bla..
}
?>
<cms:php>
// invoke the function from within Couch ..
my_func( <cms:show k_page_id /> );
</cms:php>
As you can see, the bulk of the code remains in PHP with only a single line invoked from Couch. This would make it very simple to debug things if they go wrong as there is not much left to debug.
Also, in case you happen to use a PHP debugger this approach would make it possible to place breakpoints in the PHP code.
Please try this method and do let us know if it helps.