Hello, World-1!

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.

Summary
Hello, World-1!So how does PET actually look like from the eye of the programmer?
Basic assignmentHere is a basic assignment.
Basic loop
Query parametersUsing query parameters and conditions.
Sessions and redirectsLogging in a user using sessions.
More advanced examplesThese examples were only a sneak-peak to PET.

Basic assignment

Here is a basic assignment.

...
[% a = 1 %]
[% a = a+1 %]
[% a %]
...

That will result in :

2

You can also write :

...
[% SET a = 1 %]
[% SET a = a+1 %]
[% GET a %]
...

Basic loop

...
[% FOREACH i = [1..5] %]
[% i %]<br>
[% END %]
...

That will result in :

...
1<br>
2<br>
3<br>
4<br>
5<br>
...

Query parameters

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.

Sessions and redirects

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>
...

Explanation

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).

More advanced examples

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!.

While PET can be used as some sort of an “active pages” system, that is clearly not the recommended usage.
PET has a built-in capability called “filtering”.
Close