Skip to content

Creating View Layouts Using Subroutines

thofrey edited this page Apr 1, 2014 · 5 revisions

Table of Contents

  1. Example
  2. Attachments

Example

Subroutines were introduced into the framework with version 1.5, giving developers an easy way to create a "just do it" section of code, a reusable procedure to be executed immediately without being added to the event queue. Here we will examine how a subroutine can be used to generate a consistent site layout.

Consider a default home event in the configuration file:

    <event-handler event="home" access="public">
        <view-page name="home" contentArg="content" />
        <execute subroutine="doPageLayout" />
    </event-handler>

We render the contents of the home page into the content arg called content, then execute a subroutine doPageLayout.

    <subroutines>
        <subroutine name="doPageLayout">
            <notify listener="newsListener" method="getNews" resultArg="news" />
            <view-page name="sidebar" contentArg="content" append="true"/>
            <view-page name="layout"  />
        </subroutine>
    </subroutines>

In this subroutine, we will retrieve some news from the news listener, generate a sidebar (which will display the news that was returned by the listener), appending this content to that generated from the home event. Finally we'll stuff this generated content into a final page layout to maintain a consistent look and feel to our site.

In this example, the body of the layout page is pretty simple:

    <body>
        <div>
            <h1>Layout Using Subroutines</h1>
            <cfif event.isArgDefined("content")>
                <cfoutput>#event.getArg("content")#</cfoutput>
            </cfif>
        </div>
    </body>

We test for the presence of the content argument in the event object, then display it if it exists. A typical layout page would also contain links the site style sheets, javascript includes, menus and graphics, etc. Using this approach allows the developer to separate the content generated by an event from the design and layout used site-wide, making changes to the site appearance a much easier task.

Attachments

Clone this wiki locally