Python Servlet Engine

"So simple and elegant, yet extremely powerful."
- PSE User

Creating and Using Custom Tags in PSE

Hello World example

First, create a custom tag class in Python:

from pse_handler.tags import CustomTag

class HelloWorld(CustomTag):
    def __str__(self):
        return 'Hello, world!'
Listing 1: Custom Tag definition in HelloWorldTag.py

Add the file to the TagHooksModule in pse.conf:

TagHooksModule = /path/to/HelloWorldTag.py

You need to restart apache for PSE to read the changes. Then use the tag in a PSE template:

<html>

<head>
<title>Hello World Example</title>
</head>

<body>
Here is the custom tag: <HelloWorld/>
</body>

</html>
Listing 2: Example helloworld.pt template

Compare to JSP2:

package jsp2.examples.simpletag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;

public class HelloWorldSimpleTag extends SimpleTagSupport {
    public void doTag() throws JspException, IOException {
	getJspContext().getOut().write( "Hello, world!" );
    }
}
Listing 3: Java source package for custom JSP tag
<%@ taglib prefix="mytag" uri="/WEB-INF/jsp/jsp-taglib.tld" %>
<html>

<head>
<title>Hello World Example</title>
</head>

<body>
Here is the custom tag: <mytag:HelloWorld/>
</body>

</html>
Listing 4: Example helloworld.jsp file

JSP2 also has the notion of "Tag Files." See the contrib directory of the PSE distribution for the example of the "template" custom tag, which closely mimics this feature.

Return to PSE Examples


Copyright ©2003-2005 by Nicholas Borko. All Rights Reserved.