You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ve...@jakarta.apache.org on 2004/10/25 23:03:11 UTC

[Jakarta-Velocity Wiki] New: VelocityDebuggingTemplate

   Date: 2004-10-25T14:03:11
   Editor: TimWhite <ti...@cyface.com>
   Wiki: Jakarta-Velocity Wiki
   Page: VelocityDebuggingTemplate
   URL: http://wiki.apache.org/jakarta-velocity/VelocityDebuggingTemplate

   no comment

New Page:

I've worked a lot with the PHP templating system, Smarty, and I've adapted it's handy debugging console for Velocity.

 Essentially, if you put the code below into a template called "debug.vm", and you #parse it at the end of the template you want to debug (after the </html> tag), it will pop up a window with all of the context variables in it, so you can see what is being passed to your template.  (Thanks to Dan Chapman for working on this as well).

 Enjoy!

Tim White

#macro (printRow $name $value)
	#set ($rowLength = $value.length()/57+1)
	#if ($rowLength > 15)
		#set ($rowLength = 15)
	#end
	#if (($velocityCount % 2) == 0)
		_velocity_console.document.write("<tr
bgcolor=#fafafa>");
	#else
		_velocity_console.document.write("<tr
bgcolor=#eeeeee>");
	#end
	_velocity_console.document.write("<td valign=top><tt><font color=maroon>$" + "$name" + "</font></tt></td>");
	#if ($rowLength == 1)
		_velocity_console.document.write("<td><tt><font
color=green>$value</font></tt></td></tr>");
	#else
		_velocity_console.document.write("<td><tt><font
color=green><textarea rows=$rowLength cols=57>");
		_velocity_console.document.write("$value");
	
_velocity_console.document.write("</textarea></font></tt></td></tr>");
	#end
#end

<script language=javascript>
	_velocity_console = window.open("",true,"width=680,height=600,resizable,scrollbars=yes");
	_velocity_console.document.write("<html><head><title>Velocity
Debug Console</title></head><body bgcolor=#ffffff>");
	_velocity_console.document.write("<table border=0 width=100%>");
	_velocity_console.document.write("<tr bgcolor=#000000><th colspan='2'><b><font color=#FFFFFF>Velocity Debug Console</font></b></td></tr>");
	_velocity_console.document.write("<tr bgcolor=#cccccc><td colspan='2'><b>Request Attributes:</b></td></tr>");
	#foreach ($name in $request.getAttributeNames())
		#printRow($name $request.getAttribute($name).toString().replaceAll("\n", "\\n"))
	#end 
	_velocity_console.document.write("<tr bgcolor=#cccccc><td colspan='2'><b>Request Parameters:</b></td></tr>");
	#foreach ($name in ${request.getParameterNames()})
		#printRow($name $request.getParameter($name).toString().replaceAll("\n", "\\n"))
	#end
	_velocity_console.document.write("<tr bgcolor=#cccccc><td colspan='2'><b>Session Attributes:</b></td></tr>");
	#foreach ($name in ${session.getAttributeNames()})
		#printRow($name $session.getAttribute($name).toString().replaceAll("\n", "\\n"))
	#end
	_velocity_console.document.write("</table>");
	_velocity_console.document.write("</body></html>");
	_velocity_console.document.close();
</script>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org