Skip Navigation Links

Create a handler to avoid page includes repetition

Posted by adrianohahn on February 4th, 2009. Other posts by adrianohahn

When you’re writing an application probally you’ll need to include header and footer scripts in all your pages. To avoid repetition you can create a handler to do this job.

  1. Create your header and footer scripts and place them into app/views folder.
  2. header.gt

    <h1>My Header Title</h1>
    <h2><%=zget("/request/_moduleTitle")%></h2>
    <h3><%=zget("/request/_moduleSubtitle")%></h3>
    <hr>

    footer.gt

    <hr>
    <h3>My footer content</h2>
  3. Create a groovy script named ModulesHandler.groovy in app/scripts folder. This script will handle all requests that match /modules/something, and render page contents, including the header and footer.
  4. String requestedUri = getRequestedUri(false)
    String modulesPath = "/modules/"
    int indexOfModules = requestedUri.indexOf(modulesPath)
    String module = requestedUri.substring((indexOfModules + modulesPath.length()))
     
    String view = module
    if (module.lastIndexOf("/") != -1) {
    	view = view.substring(module.lastIndexOf("/"))
    }
     
    String parametersPath = "/" + module + "/" + view + "_parameters.gt"
    String viewPath = "/" + module + "/" + view + ".gt"
     
    request.view=parametersPath
    render();
    request.view="header.gt"
    render()
    request.view=viewPath
    render();
    request.view="footer.gt"
    render()
  5. Define a handler configuration in config/zero.config file.
  6. /config/handlers += [
    {
    	"events" : ["GET", "POST"],
    	"handler" : "ModulesHandler.groovy",
    	"conditions" : "/request/path =~ /modules(/[^/]+)+"
    }]
  7. Now you can create your modules pages in app/views. For each module create a folder including a view and a parameters file. In this example I will create a folder named testModule and two scripts: testModule.gt and testModule_parameters.gt.
  8. testModule_parameters.gt

    <%
    zput("/request/_moduleTitle", "My Module Title")
    zput("/request/_moduleSubtitle", "My Module Subtitle")
    %>

    testModule.gt

    <b>This is my page content</b>
  9. You can test the handler accessing the url http://localhost:8080/modules/testModule in your browser.

One Response to “Create a handler to avoid page includes repetition”

  1. Steve Ims Says:

    Interesting post. You can simplify your code even more by leveraging sMash to parse the URI path segments.

    For example, zero.config:
    /config/handlers += [
    {
    "events" : ["GET", "POST"],
    “handler” : “ModulesHandler.groovy”,
    “conditions” : “/request/path == /modules/{module}[/{view}]”
    }]

    Then ModulesHandler.groovy could be written as:
    // Ref: http://www.projectzero.org/sMash/1.1.x/docs/zero.devguide.doc/zero.core/Config.html#Condition_operators

    String module = event.module[]
    String view = event.view[]
    if (!view) {
    view = module
    }

    String parametersPath = “/${module}/${view}_parameters.gt”
    String viewPath = “/${module}/${view}.gt”

    request.view = parametersPath
    render()
    request.view = ‘header.gt’
    render()
    request.view = viewPath
    render()
    request.view = ‘footer.gt’
    render()

Leave a Reply