This console will display a popup window with information about all of the variables available in the Velocity Context. It can be an invaluable debugging tool.

To use, either add this pair of macros to your template, or better yet, to your VM_Global_library.vm file. Then from the template you want to debug, you can simply add #showDebugPopup() to the bottom of the template you want to debug, and it will pop up a window with debugging information. You'll need to remove that invocation before you go to production. (smile)

Here is the latest version, which uses the ContextTool. You must have access to the Context Tool as $context for this to work. There is a version below this that does not require that. This newer version also removes any variables that are invalid VTL.

##
## Macro - printRow
##         This macro is used by #showDebugPopup to display velocity debugging information
##
#macro (printRow $name $value)
        #set ($rowLength = $value.length()/57+1)
        #if ($rowLength > 15)
                #set ($rowLength = 15)
        #end
        #if ($velocityCount)
		#if (($velocityCount % 2) == 0)
			_velocity_console.document.write("<tr bgcolor=#fafafa>");
		#else
			_velocity_console.document.write("<tr bgcolor=#eeeeee>");
		#end
	#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

##
## Macro - showDebugPopup
##         This macro shows a Velocity debugging popup
##
#macro (showDebugPopup)
<script language=javascript>
        _velocity_console = window.open("",true,"width=680,height=600,resizable,scrollbars=yes");
        _velocity_console.document.write("<html><head><title>Velocity Debugging 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>Velocity Context Values:</b></td></tr>");
        #foreach ($name in ${context.getKeys()})
		#if($context.get($name))
			#set($nameString = $name)
			#set($invalidMsg = "INVALID VTL: ")
			#if($name.matches(".*[:|{|}| ].*"))
				#set($nameString = $invalidMsg.concat($nameString))
			#end
			#if($name.matches("[_|0-9].*"))
				#set($nameString = $invalidMsg.concat($nameString))
			#end
			#if(!$nameString.matches("INVALID VTL:.*"))
				#printRow($nameString $context.get($name).toString().replaceAll("\n", "\\n").replaceAll("\r", ""))
			#end
		#end
        #end
        _velocity_console.document.write("</table>");
        _velocity_console.document.write("</body></html>");
        _velocity_console.document.close();
</script>
#end

This version doesn't require the ContextTool:

#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").replaceAll("\r", ""))
	#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").replaceAll("\r", ""))
	#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").replaceAll("\r", ""))
	#end
	_velocity_console.document.write("</table>");
	_velocity_console.document.write("</body></html>");
	_velocity_console.document.close();
</script>
  • No labels