Embedding Nasty Templates, Trick

22 02 2008

If you have a template that, becuase of its nature/strucutre/purpose is not valid XML… yes it does happen… (sherlog search snippets), you can insert them into a comment and then a CDATA and retrieve them from the DOM. Yes, HTML comments are in the DOM.

   render : function(templateName,contextObject)  {
  			if ( ! Template.templateCache[templateName] ) {
  			 var templateNode = document.getElementById(templateName);
  				 var firstNode = templateNode.firstChild;
  				 if ( firstNode && ( firstNode.nodeType == 8 || firstNode.nodeType == 4)) {
  				 	template = templateNode.firstChild.data.toString();

  				 } else {
  				 	template = templateNode.innerHTML.toString();
  				 }
  				 Template.templateCache[templateName] = TrimPath.parseTemplate(template,templateName);
  			}
  			return Template.templateCache[templateName].process(contextObject);
  	},

 

nodeType 4 is CDATA, and nodeType 8 is a comment. However, IE5 doesn’t support the node.data property… so I could just do some regex to strip the CDATA or comment tags from converting the element to text.

Using the approach makes it possible to have valid XHTML even with templates for other formats embedded inside the document

About these ads

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

Join 103 other followers

%d bloggers like this: