Hi,
I’m kind of a ModX ’newbie’ so forgive me if this is may seem trivial.
I am developing an application and I want to seperate out the backend PHP logic coding and the front end template/javascript design. So I decided to use ModX so I could get 1 person on the template HTML design and Javascript and the other person doing the PHP classes for the backend data collection and processing.
So for example, say I have a resource file or a template in ModX and within that I want to call a Php file that resides somewhere on my server and returns something (let’s just say "hello world" to be really original) and display it within a page.
So let’s say the php file resides in www.mydomainname.com/code/helloWorld.php
Now within ModX let’s say I have a normal HTML template:
<html>
<head></head>
<body>
<!-- in here I want to call the helloWorld.php file and have it display "hello world" -->
</body>
</html>
So what’s the best way to do this?
Thanks
Peter
-
☆ A M B ☆
- 24,524 Posts
Create a snippet (in the Manager, go to Elements -> Manage Elements, then in the Snippets tab click the New Snippet link)
<?php
include "code/helloWorld.php";
return;
?>
Or, if your helloWorld.php is really trivial, just put the code directly in the snippet
<?php
return "Hello, World!";
?>
Then in your HTML call the snippet like this:
http://www.sottwell.com/how-snippets-work.html
There are some circumstances where you might need to use:
<?php
return include "code/helloWorld.php";
?>
But, really, the best thing is often to paste the PHP code into a snippet so the code will be stored in the DB rather than the file system.
One caveat is that the code can’t contain interspersed HTML. If there is HTML output to be returned, it should be assigned to a variable and returned from the snippet. Here’s an example.
Wrong:
<?php
$count = 6;
?>
<p>Count = <?php echo $count; ?></p>
<p>end of data</p>
Right:
<?php
$output = '';
$count = 6;
$output .= '<p>Count = ' . $count . '</p>';
$output .= '<p>end of data</p>';
return $output;
?>
-
☆ A M B ☆
- 3,112 Posts
Quote from: BobRay at Nov 10, 2009, 11:41 PM
But, really, the best thing is often to paste the PHP code into a snippet so the code will be stored in the DB rather than the file system.
Uhm... isn’t this against the upgrade method flexibility?
If we store the code as the file system, then we only just over write the files.
As we speak as a developer (although I’m speaking as a newbie), we don’t see that as a problem. But after we hand over the system to the client, we should smartly avoid the ’forever call’ of web maintenance, shouldn’t we?
Even different views and discussions have been posted several times.
Sorry, I’m not trying to hijack the thread, but the issue’s always been arise from the first timer snippet author.
Rico
Genius is one percent inspiration and ninety-nine percent perspiration.
Thomas A. Edison
MODx is great, but knowing how to use it well makes it perfect!
www.virtudraft.com
Security, security, security! |
Indonesian MODx Forum |
MODx Revo's cheatsheets |
MODx Evo's cheatsheets
Author of
Easy 2 Gallery 1.4.x,
PHPTidy,
spieFeed,
FileDownload R,
Upload To Users CMP,
Inherit Template TV,
LexRating,
ExerPlan,
Lingua,
virtuNewsletter,
Grid Class Key,
SmartTag,
prevNext
Maintainter/contributor of
Babel
Because it's hard to follow all topics on the forum, PING ME ON TWITTER
@_goldsky if you need my help.
Quote from: goldsky at Nov 11, 2009, 12:49 AM
Quote from: BobRay at Nov 10, 2009, 11:41 PM
But, really, the best thing is often to paste the PHP code into a snippet so the code will be stored in the DB rather than the file system.
Uhm... isn’t this against the upgrade method flexibility?
If we store the code as the file system, then we only just over write the files.
As we speak as a developer (although I’m speaking as a newbie), we don’t see that as a problem. But after we hand over the system to the client, we should smartly avoid the ’forever call’ of web maintenance, shouldn’t we?
Even different views and discussions have been posted several times.
Sorry, I’m not trying to hijack the thread, but the issue’s always been arise from the first timer snippet author.
It’s a good question (if I’m understanding you). As long as the snippet has a unique name (i.e. not called Ditto or Wayfinder, etc.), it will be preserved on upgrade either way. Some developers prefer to leave the snippet in a file for version control and/or using a favorite editor to work on it. OTOH, if it’s in a file, it’s more of a pain to modify the snippet code in the Manager.
IIRC, OpenGeek likes to keep the code in files in the root directory and use one custom snippet to "include" any of the snippet files (
http://modxcms.com/forums/index.php?topic=18872.40).
You can certainly do it however you like.