Master templates revisited
Remember one of my previous posts,Working with a master template , and the article from Tommy Valand: PHP like templating? The idea is: provide a master HTML template with special tags that are filled in by code. A superb way of separating content and presentation.
The HTML template
You can define an HTML template and store it in a field on a document. On the template are normal HTML tags, combined with special tags that are later processed by the code. It could look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html;charset=utf-8" />
[%meta%]
[%include%]
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="/domino/feed.xml" />
<link rel="shortcut icon" href="/domino/favicon.ico" type="image/x-icon" />
</head>
<body><div id="canvas">
<div id="tools">
<div id="user">[%user%]</div>
<form method="post" action="/domino/search?CreateDocument" id="search"><p>
<input name="Query" value="" /> <input class="button" type="submit" value="Search" />
</p></form>
</div>
<div id="top"><strong><a href="/" id="home">Domino 2.0</a></strong>
Rich Internet Applications with IBM Lotus Notes/Domino</div>
[%nav%]
<div id="trail">[%trail%]</div>
[%content%]
<div id="legal">Powered by IBM Lotus Notes/Domino - [%rendertime%]</div>
</div>
[%script%]
</body></html>
The code
I use a DwSession object which allows to collect information like title, username, breadcrumb trail, navigation etc. and fills this data in the correct tags by processing the master template. Unlike previous article, I now use a faster method with the StringBuffer class, chopping the master file up and replacing the necessary bits. The 'process' method does this job:
Function process(Byval sVal As String) As String
On Error Goto catch
Dim ns As New StringBuffer(200)
Dim ns2 As New StringBuffer(50)
Dim sData As Variant,sChunk As Variant
Dim i As Integer
sData=Split(sVal, "[%")
ns.add sData(0)
For i=1 To Ubound(sData)
sChunk=Split(sData(i), "%]")
Select Case sChunk(0)
Case "path","db"
ns.add path
Case "meta"
ns.add |<title>| & title.collapse(SEPARATOR) & |</title>|
Case "include"
ns.add |<link rel="stylesheet" type="text/css" href="| & path & |css" />|
Case "user"
ns.add doc.username(0)
Case "nav"
ns.add getNav
Case "trail"
'trail goes here
Case "content"
ns.add process(content.collapse(NEWLINE))
Case "rendertime"
ns.add "Page rendered in " & Format((Getthreadinfo(6)-start)/Getthreadinfo(7), "0.000") & " sec."
Case "script"
ns.add |<script type="text/javascript" src="| & path & |mme-min.js"></script>| & NEWLINE
Case Else
ns.add "ERROR: " & sChunk(0)
End Select
ns.add sChunk(1)
Next
process=ns.collapse("")
Goto finally
catch:
Error "Error " & Err & " in DwSession.process(), line " & Erl & ": " & Error$
Resume finally
finally:
End Function
The process function is a part of my DwSession class, one of the core components of my d-works application. Have fun.
Comments
To add a comment, log in or register as new user. It's free and safe.