Posts filed under 'Application Development'


Implementing Master Page concept in ColdFusion

One of the functionality I like in asp.net platform is the master page concept, it really helpful to keep the layout clean and gives really good control to developer in terms of placement of content/data on the rendered page. Having worked on aps.net side of it, I figured it’s fairly easy to implement the similar concept in ColdFusion, let me first explain what master page is.
“Master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. You can then create individual content pages that contain the content you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page.” Reference : http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx (can read more about master page..)

How does master page functionality work

a) Create a master page
You need a master page (i.e. the template) with global layout of your web application, there can be multiple templates if you want different look and feel for different section or can have just one global master page for the entire web app, part of the template you will have to also put in placeholder that can be updated by the developer that will be creating functionality for individual pages.

b) Create content page
Content page is the actual page that gets shown to the user; it’s the combination of template + the content relevant to that page. In this page the developer fills in the master page placeholder with page relevant content and there for have full control over what gets rendered to the end user.

Why is master page required in ColdFusion application?

Before I even answer this question, lets walk through the scenario of how page template are created in most of the ColdFusion application, normally the header and footer or other section gets broken into separate file like Header.cfm, footer.cfm etc etc, and the individual content page like index.cfm will include header.cfm on top and then add some html code for the page and finally include footer.cfm something like this.

<cfinclude template="header.cfm"/>
Some content,  static, via db or other means.
<cfinclude template="footer.cfm"/>

So what’s the big deal?

Normally this is not a problem when you start with the project, but slowly and slowly as the number of pages for the site increases you start to realize that header and footer has to change on page by page basis, and you start introducing variables and start having conditional logic in your header and footer pages, or another situation can be that for some special pages you need to add css or javascript because those pages are doing special ajax stuff, but you don’t want to add that to header, because it will get included for all pages and that will slow down the site, so basically start adding the css and javascript withing the content page and after some time your page looks like javascript and css all over the place, plus your header and footer files have ton’s of conditional logic.
Check out http://www.sys-con.com/ and perform view source, scroll through the html and see how css and javascript is pretty much all over the page, and not consolidated in header section, where it should be right?

How does master page help?

By using master page you get multiple benefits,

  • don’t have to break the layout in multiple files like the header.cfm and footer.cfm earlier
  • the layout logic is clean and does not have to be conditional
  • by providing placeholder at proper places you give the developer an opportunity to put in page related content
  • master page maintains the rendered document structure and can help you comply with w3c standards
  • Other benefits are also possible -)

How to implement Master page in coldfusion

Along with this entry I have also posted sample code, that shows standard way of using template and how that can be implemented as master page in coldfusion. The examples are self explanatory but to provide brief overview the concept is to first define master page somewhat like this (just an example).

Download Code

  1. <cfoutput>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <title><cfif isDefined("variables.master_page_title")>#master_page_title#<cfelse>Free Css Layout 1</cfif></title>
  6. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  7. <link rel="stylesheet" type="text/css" href="css/style.css" />
  8. <cfif isDefined("variables.master_page_javascript")>#variables.master_page_javascript#</cfif>
  9. <cfif isDefined("variables.master_page_css")>#variables.master_page_css#</cfif>
  10. </head>
  11. <body>
  12. <div id="container">
  13. <div id="header"><h1><a href="http://www.free-css.com/free-css-layouts.php">Free CSS Layouts</a></h1></div>
  14. <div id="wrapper">
  15. <div id="content">
  16. <cfif isDefined("variables.master_page_body")>#variables.master_page_body#</cfif>
  17. </div>
  18. </div>
  19. <div id="navigation">
  20. <p><strong>Navigation Here</strong></p>
  21. <ul>
  22. <li><a href="http://www.free-css.com/">Free CSS Templates</a></li>
  23. <li><a href="http://www.free-css.com/free-css-layouts.php">Free CSS Layouts</a></li>
  24. </ul>
  25. </div>
  26. <div id="extra">
  27. <p><strong>More stuff here.</strong></p>
  28. <p>sit malesuada lacus pellus parturpiscing. Pellenterdumat maecenatoque cras a magna nibh et quis diam ames et. Laoremvolutpat ac dolor eget eget temper lacus vestibus velit lacus venean. Magnaipsum tellus morbi leo aliquat nulla convallis pellentesque.</p>
  29. </div>
  30. <div id="footer">
  31. <p>Footer</p>
  32. </div>
  33. </div>
  34. </body>
  35. </html>
  36. </cfoutput>

Important thing to note is the cfif tag followed by the variable.name , basically these are the values that can be supplied by individual page developer and that gets merged when final page is rendered

The content page will look like this

  1. <cfsavecontent variable="variables.master_page_body">
  2. <p><strong>Content here.</strong></p>
  3. <p>Sapibulumnibh phasellus nulla egestibulum enim pretium elit tincidunt estiquam ultrisque donectetur. Sedcondimentumsan odio hendrerit proin vitae dignis nibh ac justo id congue. Amesintesque vel curabitae volutpat donec alique nasceleifendimentesque montesque rhoncus quis eros. Vestnunc nonummy</p>
  4. <p>Montegeraliquam sed pede in cursus praesenec vestas rhoncus wisi at wisi. Condisseloborttis enim et ipsum mauristie id felit adipiscipit ac auctortorttitor sempor. Vitantesqueat sempus non sed et mus sit vivamus purus netus hendiment. Pretiuma diam et id tempus dolor por wisi sed volutpat facilisi.</p>
  5. <p>Wisiet sus adipit phasellentum elit condissim consecteturpiscing sapien vivamus et congue. Utvel tris quismod cursus liberos elit nisse curabitur tur parturpis tellenterdum. Semperligula curabitae tellentesque nulla trices vestas ristibulum id justo auctor facinia. Natisdonec consequat nibh pellus.</p>
  6. <p>Vestibusodio euisque id elerisus lacus tincidunt sit malesuada lacus pellus parturpiscing. Pellenterdumat maecenatoque cras a magna nibh et quis diam ames et. Laoremvolutpat ac dolor eget eget temper lacus vestibus velit lacus venean. Magnaipsum tellus morbi leo aliquat nulla convallis pellentesque.</p>
  7. </cfsavecontent>

Another example

  1. <cfset variables.master_page_title="updated page..">
  2.  
  3. <cfsavecontent variable="variables.master_page_javascript">
  4. <script language="javascript" src="http://www.cachefile.net/scripts/prototype/1.6.0.2/prototype.js" type="text/javascript"></script>
  5. <script language="javascript">
  6. Event.observe(window, 'load', function() {
  7. alert("page loaded..");
  8. });
  9. </script>
  10. </cfsavecontent>
  11.  
  12. <cfsavecontent variable="variables.master_page_css">
  13. <style type="text/css">
  14. .style1 {font-size:10px;}
  15. </style>
  16. </cfsavecontent>
  17.  
  18. <cfsavecontent variable="variables.master_page_body">
  19. <p><strong>Content here.</strong></p>
  20. <p>Sapibulumnibh phasellus nulla egestibulum enim pretium elit tincidunt estiquam ultrisque donectetur. Sedcondimentumsan odio hendrerit proin vitae dignis nibh ac justo id congue. Amesintesque vel curabitae volutpat donec alique nasceleifendimentesque montesque rhoncus quis eros. Vestnunc nonummy</p>
  21. <p>Montegeraliquam sed pede in cursus praesenec vestas rhoncus wisi at wisi. Condisseloborttis enim et ipsum mauristie id felit adipiscipit ac auctortorttitor sempor. Vitantesqueat sempus non sed et mus sit vivamus purus netus hendiment. Pretiuma diam et id tempus dolor por wisi sed volutpat facilisi.</p>
  22. <p>Wisiet sus adipit phasellentum elit condissim consecteturpiscing sapien vivamus et congue. Utvel tris quismod cursus liberos elit nisse curabitur tur parturpis tellenterdum. Semperligula curabitae tellentesque nulla trices vestas ristibulum id justo auctor facinia. Natisdonec consequat nibh pellus.</p>
  23. <p>Vestibusodio euisque id elerisus lacus tincidunt sit malesuada lacus pellus parturpiscing. Pellenterdumat maecenatoque cras a magna nibh et quis diam ames et. Laoremvolutpat ac dolor eget eget temper lacus vestibus velit lacus venean. Magnaipsum tellus morbi leo aliquat nulla convallis pellentesque.</p>
  24. </cfsavecontent>

Here you can see the content page have to supply the value to master page placeholder , also to note is that content page does not have to supply all the values, it can omit what it does not care about (that’s why the master page had conditional if logic)

The actual magic of merging master page and template page happens in the onRequestEnd.cfm file where this file basically removes all the previous content and based on setting loads the appropriate template file! Download Code to view files

Conclusion

If the master page approach is used in your web application development, you will end up getting multiple benefits

  • Single template file (though you can multiple template file for certain section of site, all you have to do is create another template file and define a variable on your content page like this
    <cfset request.master_page_template = "the new template/master page name"/> )

  • Using master page you can get rid of empty white space on your page (specially on the very top of generate html page )
  • Master page gives the content page developer flexibility on how to fill in the template
  • Generated html page is clean
  • Don’t have to deal with multiple include files
  • Overall it’s easy to use.

[Update : Most of the Coldfusion MVC framework provides template concept - but the implementation varies]

6 comments December 19th, 2008

Exploring Data push with Instant Messaging

Have you ever come across the idea of building an application or functionality that notifies the user of some event, I can think of various examples?

  • A Real estate side notifying the user that property matching their criteria just got listed
  • A job side notifying user that a new job matches there keywords
  • Application server error alert send to administrator
  • Notifying that stock prices went down or up

I can think of hundreds of different situation where notification functionality can be use, today’s most of the notification either happens though email or by the fact that user will come to certain section of site to get the updated information, not trying to downplay the fact that there are tools like nagios etc that can pull data from SNMP traps and send notification but that’s require you to implement SNMP protocol in your application which is not a trivial task.

With the every growing popularity of Instant messaging where millions of users are now actively using IM as means of communication both in office and personal environment bring a very promising platform for application development that can provide the notification mechanism that once thought was tough to implement.

Though instant messaging application like MSN, Yahoo Messenger, AOL Instant Messenger, Google Talk etc have implemented their own protocol and don’t cross communicate but there is light at the end of the tunnel. Google Talk uses XMPP protocol for messaging and looks like yahoo and AOL may follow the suite in future.
http://florianjensen.com/2008/01/17/aol-adopting-xmpp-aka-jabber/
http://www.process-one.net/en/blogs/article/after_aol_yahoo_is_also_experimenting_with_xmpp/

XMPP (Extensible Messaging and Presence Protocol) aka Jabber, is a light weight XML based messaging protocol for which various libraries exists and can be used for application development. If all the IM application start supporting XMPP the application development for IM platform will become super simple, but that not the case yet and I think it’s going to be a while before something like this will happen.

Currently Google Talk allows anyone to build application that can operate on XMPP protocol and adhere to their policy, similarly http://code.google.com/apis/talk/open_communications.html

Similarly AOL supports OSCAR protocol and have provided a set of library under the project “Open AIM” to communicate with their platform. http://dev.aol.com/aim

Microsoft and Yahoo are also working on provide a platform that allows developers to integrate with their platform.

As of now I can see two platform (Gtalk, AIM) easily available for integration with more coming in future, with what was available I started playing with these platform and developed a prototype/proof of concept as to how IM instant notification can be useful and demonstrate its power.

To start with what I wanted was to create an application where I could send message to users from a web page and similarly when the user respond back I should be able to receive the message in a web application, so the way I proceeded with this was to create a java application that has a build in HTTP server , where I can send messages from my web page that way the java application can get the data and then it sends it over the message to appropriate user as defined in the http payload (JSON message)

Similarly when the message arrives the Java application Posts the message to a web page where I can go ahead receive and parse the data (very simple)
For proof of concept I added the notification functionality to the Indiankey Gigs section, where it now allows the user to subscribe for IM notification, User can add the notifications that they are interested in i.e. looking for ColdFusion gigs or java gigs etc and can interactively chat with the application using their IM client and finally on the server side I have the ColdFusion code that reads the preferences of the user and sends them the listing that match their criteria on real time basis.
http://www.indiankey.com/jobs/ (link to prototype application)

The benefit I see form the proof of concept is that now the user don’t have to constantly keep coming to the job section to see the new listing they can set the keywords and if they are only they will receive the messages that meet there condition, resulting in low server resource usage and allowing the application to push the data with the IM infrastructure!

IM Use Case

3 comments March 28th, 2008


 Subscribe in a RSS reader


CourseLookup.us - Providing course and conference information to enhance your knowledge base.

Tags

Calendar

July 2010
M T W T F S S
« Dec    
 1234
567891011
12131415161718
19202122232425
262728293031  

Posts by Month

Posts by Category