So how does PET actually look like from the eye of the programmer?
I have always understood actual code samples better than lengthy explanations. I guess many programmers feel the same. So for them, here are some basic examples! (Plese don’t forget to check MVC-style Programming out!)
Suppose that you have PET installed and running. In your HTML firectory ($DOCUMENTROOT, that is) you create a file with extension .pet :
index.pet
The file contains something like :
<html> <body> Hello, world! </body> </html>
When you call http://yourdomain/index.pet, the above code will be output.
Okay, that was easy :) Let’s see some saner examples.
| Hello, World-1! | So how does PET actually look like from the eye of the programmer? |
| Basic assignment | Here is a basic assignment. |
| Basic loop | |
| Query parameters | Using query parameters and conditions. |
| Sessions and redirects | Logging in a user using sessions. |
| More advanced examples | These examples were only a sneak-peak to PET. |
Using query parameters and conditions.
... [% IF QUERY.test %] Incoming parameter test is: <b>[% QUERY.test %]</b><br> [% ELSE %] No input parameter. [% END %] ...
That will result in (considering you call it as /index.pet?test=hello) :
... Incoming parameter test is: <b>hello</b><br> ...
If you are familiar with Perl, you might notice that this syntax is the same as that of the Template-Toolkit module (Template.pm). This is no coincidence : PET actually uses Template.pm as its templating engine.
Logging in a user using sessions.
...
[% IF FORM.login %]
[% IF (FORM.username == 'demo') && (FORM.password == 'demo') %]
[% DISP.setSession('loggedin','1') %]
[% DISP.forceRedirect('/members/') %]
[% END %]
[% END %]
...
<form method="post">
<input type="text" value="" name="username" />
<input type="password" value="" name="password" />
<input type="submit" value="Submit" name="login" />
</form>
...I think this example is rather straightforward. If the user presses the “Submit” button, we have FORM.login defined, so the first IF branch executes. If username and password are both equal to “demo”, then we set the sesson variable “loggedin” to “1”, and then redirect the user to the “/members/” area (which, obviously, should check for session.loggedin to see if the previous login was successful).
These examples were only a sneak-peak to PET. If you are familiar with a similar system -- such as JSP, Apache::ASP, PHP, and so on -- you probably get the point. However, PET is much-much more. Please continue reading at Hello, World-2!.