Is there a way to have PHP code within a chunk, TV or template and force MODX to output the finished webpage with PHP code intact (unexecuted)?
I have been trying, but it seems that MODX converts it comments:
<?php echo "Hello world!"; ?>
becomes
<!--?php echo "Hello world!"; ?-->
My goal is using MODX as a backend to generate static webpages, which would load much quicker. But the landing page needs to execute one PHP script to fine tune the marketing pitch per viewer, so I can't have MODX commenting out my PHP calls.
Take a look at the Syntax Highlighter and FixedPre extras. I use them together at the site in my sig.
-
☆ A M B ☆
- 24,524 Posts
I don't think that's what he has in mind, Bob. He wants MODX to generate a flat .php file that will be mixed HTML and PHP. The request will be for that .php file, not MODX, so the inline PHP is needed for functionality. Actually I fail to see the purpose in this.
Some people are not necessarily interested in using a snippet for every little thing, and possess the habit of mixing PHP code with HTML.
Of course there is the security reason and whatnot, and separation philosophy and whatnot, but I think if it is possible in MODx to allow little PHP code here and there, just as how JS code is allowed, that would be dandy and appreciated by some people. Unless these people are not considered worth caring about...
TinymceWrapper: Complete back/frontend content solution.
Harden your MODX site by
passwording your three main folders:
core, manager, connectors and renaming your
assets (thank me later!)
5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
I might be confused, but the OP wrote that the PHP code should be "unexecuted" (and the only way to do that is to entify it), but the final note suggests that maybe the PHP code should execute to create the static pages.
There's no way to do this in MODX, since MODX will strip out any non-entified PHP code, but if that's the intention, there are two ways I can think of to do it.
First, you could use a snippet with cURL to retrieve the finished pages and save them.
Second, I think you could use a plugin attached to OnWebPagePrerender, or OnWebPageComplete, to save the static page using $modx->resource->_output.
Finally, if Susan is correct, that you want your static pages to contain PHP code that will be executed, you can save them as .php files and link to them directly. MODX's routing system only responds to pages that are not found, so if you're page are found, they'll go right to the browser and any PHP in them should execute. You'd have to put your own custom placeholder on each page -- not a MODX placeholder, something like {{php_code_here}} -- where the PHP code goes, and replace it yourself with the chunk content when you save them, either manually, or with str_replace() if you're saving them with a snippet or plugin.
That's the only way I can think of to get the other MODX tags on the page to execute and still have your PHP code in there untouched.
@sottwell - the overall goal is to remove MODX from serving pages and use it as a backend to simply generate static html pages which will be stored on the server. These stored pages will then be served to our website visitors (quicker response for our global audience). Because the page generating is separate from the page serving I am also afforded the luxury of time to perform additional optimizations such as minification of the html. And MODX is slightly more secure as it is not sitting on the front facing website. The process works beautifully except for the one landing page that uses PHP.
As I said above, you'd need to inject the PHP code into the page as it's saved. As long as MODX is not serving the page, the PHP code will execute. If the PHP code requires MODX to function, you'd have to instantiate MODX at the top of the PHP code.
@BobRay - Sorry if my explanation was not clear. I have a PHP script that basically fetches each resource from MODX, then stores them as a static html file in the root of our domain. These stored pages are what gets served to website visitors.
But my primary landing page uses PHP to change the marketing pitch and hero image based on the referral source. So I would need the PHP code for inserting these changes left intact when MODX created the webpage.
I could manually build the landing page, but then I lose the advantages of a CMS in regard to updating designs, navigation, etc.
I might have to try the str_replace() idea after fetching the file from MODX. Thanks.
Sorry I am a very slow typist.
@mrcycling Can you share how you have this set up? Some code example...