Template Processing

This is a discussion on "Template Processing" within the Classic ASP section. This forum, and the thread "Template Processing are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Classic ASP

Notices


Closed Thread
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 9th, 2003, 01:33
vor vor is offline
Junior Member
Join Date: Aug 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Template Processing

I'm writing another template reading. This time around I'm making the template file XML based. The basis of this class is to open a xml and process the custom tags "template" and "item".

The only draw back is that all the HTML you use in the template has to follow XML rules. Case sensitive and close every tag. But the bright side is that it makes it easy for any none technical people to modify the template and do little to no asp code change.

Currently, this class can only process a multi-dimensional array. But I will be adding a Auto Request.Form and Recordset processing. Then finish this bad boy with a send html/text email from the processed template.

Thought some of you might want to take the basic concept and build the rest on your own or what not. Either way, On With the CoDe..

ASP CLASS AND USAGE CODE
Code: Select all
<%
'vTemplate by Pedro "VoR" Sousa

dim tmpl
set tmpl = new vTemplate
if not tmpl.Load(Server.MapPath("tmpl_form.xml")) then
	Response.write "Unable to load template. Either file not found, or there is an error in the xml file."
else '++++++++++++++++++++++++++++++++++++++++
	dim ary(1)
	ary(0) = Array("item1","item 1 text")
	ary(1) = Array("item2","item 2 text")
	call tmpl.ProcessArray("tmp1",ary)
	Response.write tmpl.Output()
end if
tmpl.Close()
set tmpl = nothing

class vTemplate
	'##########################################
	'Private Global Variables
	'##########################################
	private xml
	
	'##########################################
	'Main Functions
	'##########################################
	private sub class_initialize()
		set xml = Server.CreateObject("Microsoft.XMLDOM")
		xml.async = false
	end sub'-----------------------------------------------
	
	private sub class_terminate()
		set xml = nothing
	end sub'-----------------------------------------------
	
	public function Load(path)
		Load = xml.Load(path)
	end function'------------------------------------------
	
	public sub Close()
		set xml = nothing
	end sub'-----------------------------------------------
	'##########################################
	'Processing Function
	'##########################################
	'Get the Template Output after processing
	public function Output()
		Output = xml.documentElement.xml
		'Clean up some unwanted text
		Output = replace(output,"<template>","")
		Output = replace(output,"</template>","")
		Output = replace(output,"<TemplateDocument>","")
		Output = replace(output,"</TemplateDocument>","")
	end function'------------------------------------------
	
	'Process a MultiDimensional Array
	public function ProcessArray(tname,ary)
		ProcessArray = false
		if not isArray(ary) then exit function
		dim tnode,nl,n
		set tnode = FindTemplate(lcase(tname))
		if not tnode is nothing then 'If template Node found
			RemoveAttribs(tnode) 'Lets get ride of all attributes.
			set nl = tnode.getElementsByTagName("item")
			if nl.length > 0 then
				for each n in nl
					if n.getAttribute("id") <> "" then
						call ProcessItem(n,FindArrayValue(n.getAttribute("id"),ary))
					end if
				next
			end if
			set nl = nothing
		end if
		set tnode = nothing
	end function'------------------------------------------

	'##########################################
	'Helper Functions
	'##########################################
	'Find a Template Element by ID
	private function FindTemplate(tname)
		set FindTemplate = nothing
		dim n,nl: set nl = xml.getElementsByTagName("template")
		if nl.length > 0 then
			for each n in nl
				if lcase(n.getAttribute("id")) = tname then set FindTemplate = n : exit for
			next
		end if
		set nl = nothing
	end function'------------------------------------------
	
	'Search through a MultiDimensional Array for ID-Value pair.
	private function FindArrayValue(id,ary)
		FindArrayValue = ""
		dim x
		for x = 0 to ubound(ary)
			if ary(x)(0) = id then FindArrayValue = ary(x)(1) : exit function
		next
	end function'------------------------------------------
	
	'Remove all the attrbutes of a node
	private sub RemoveAttribs(node)
		dim a
		for each a in node.attributes
			node.removeAttributeNode(a)
		next
	end sub'-----------------------------------------------
	
	'Process the Item Node
	private sub ProcessItem(node,text)
		dim content,txtnode
		'If no data is sent, Lets grab and default info stored in the item
		if text <> "" then content = text else content = node.getAttribute("default")
		'Now Replace Node with text
		call node.parentNode.replaceChild(xml.createTextNode(content),node)
	end sub'-----------------------------------------------
end cla
XML TEMPLATE FILE
Code: Select all
<?xml version="1.0"?>
<TemplateDocument>
<html>
	<head>
		<title>vTemplate by Pedro "vor" Sousa</title>
	</head>
<body>

<template id="tmp1">
	<table border="1" cellpadding="9">
		<tr><td><item id="item1" default="default value1"/></td></tr>
		<tr><td><item id="item2" default="default value2"/></td></tr>
		<tr><td><item id="item3" default="Using Default Value"/></td></tr>
	</table>
</template>

</body>
</html>
</TemplateDocument>

  #2 (permalink)  
Old Sep 9th, 2003, 07:50
Reputable Member
Join Date: Aug 2003
Location: United Kingdom
Posts: 158
Thanks: 0
Thanked 0 Times in 0 Posts
In your 'Output' function why have you used 'Output = replace(output,"","")' four times, won't one have done?

u2o
  #3 (permalink)  
Old Sep 9th, 2003, 08:24
Rob's Avatar
Rob Rob is offline
Head Admin & CEO

SuperMember
Join Date: Jul 2003
Location: at my desk
Age: 34
Posts: 2,951
Blog Entries: 7
Thanks: 7
Thanked 3 Times in 3 Posts
Send a message via MSN to Rob Send a message via Skype™ to Rob
<blockquote id="quote"><font size="1" face="geneva, verdana, arial" id="quote">quote:<hr height="1" noshade id="quote"> In your 'Output' function why have you used 'Output = replace(output,"","")' four times, won't one have done?
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

Actually, that code does nothing, but I think Vor's intention is for you to fill in the processing section yourself!!

I actually have what I consider to be a much better templating system than this (Not to knock Vor's way, but mine has been kicking around for ages, and has been built upon for a few years now), which will process either a standard HTML template, or an XHTML compliant template and allowing for dynamic content anywhere in the template including the Head section... header content and footer (In fact anywhere).... I will get round to posting it as soon as I get time... I am up to deadline on 3 sites at the moment and am a bit pushed!
__________________
Rob - SEO Specialist
Owner & Founder of Webforumz.com

I am currently unavailable for private work
  #4 (permalink)  
Old Sep 10th, 2003, 00:01
Anonymous User
Guest
Posts: n/a
Giving it away now are we!? I'll be looking for it. My version is outdated now. :razz:
Closed Thread

Tags
template, processing

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Word Processing ITG Job Opportunities 0 Dec 4th, 2007 09:58
Ah form processing doing my head in! 1840dsgn PHP Forum 7 Jul 17th, 2007 23:14
problems with form processing tooie Classic ASP 3 Apr 26th, 2006 17:34
More questions on web processing scripts a.jenery PHP Forum 5 Mar 2nd, 2006 14:56
Form processing javabean kinjiro PHP Forum 0 Aug 9th, 2004 14:18


All times are GMT. The time now is 21:24.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43