tag:help-archives.hannonhill.com,2010-02-09:/discussions/velocity-formats/49-velocity-adding-unwanted-space-charactersCascade CMS: Discussion 2012-07-30T15:21:43Ztag:help-archives.hannonhill.com,2010-02-09:Comment/102762092011-09-28T19:33:25Z2011-09-28T19:33:25ZVelocity adding unwanted space characters<div><p>Any line breaks or extra spaces in your Velocity code will cause
the browser to interpret that as a space character when rendering
the HTML. For lack of a better way to say it, that's just how HTML
works.</p>
<p>In order to have no space between the member's name and the
comma, you'll need to ensure that there is no space between those
two things in your Velocity code.</p>
<p>I would solve this issue by doing one of the following
(untested):</p>
<pre>
<code>$_SerializerTool.serialize($_XPathTool.selectSingleNode($contentRoot, "//page-xhtml"), true)
#set ( $members = $_XPathTool.selectNodes($contentRoot, "//system-index-block[@type='content_type']/system-page[not(contains(path,'_internal'))]") )
#foreach ($member in $members)
#set ( $href = $member.getChild('path').text )
#set ( $title = $member.getChild('title').text )
<p>
#set ( $department = $_XPathTool.selectSingleNode($member, "dynamic-metadata[name='Department']/value").text.replaceAll("&", "&amp;") )
#if ( $department != '' )
<b><a href="${href}">${title}</a></b>, $department
#else
<b><a href="${href}">${title}</a></b>
#end
</p>
#end</code>
</pre>
<p>Or (untested):</p>
<pre>
<code>$_SerializerTool.serialize($_XPathTool.selectSingleNode($contentRoot, "//page-xhtml"), true)
#set ( $members = $_XPathTool.selectNodes($contentRoot, "//system-index-block[@type='content_type']/system-page[not(contains(path,'_internal'))]") )
#foreach ($member in $members)
#set ( $href = $member.getChild('path').text )
#set ( $title = $member.getChild('title').text )
#set ( $output = "<b><a href="${href}">${title}</a></b>" )
<p>
#set ( $department = $_XPathTool.selectSingleNode($member, "dynamic-metadata[name='Department']/value").text.replaceAll("&", "&amp;") )
#if ( $department != '' )
#set ( $output = $output + ", ${department}" )
#end
${output}
</p>
#end</code>
</pre>
<p>The difference between the two examples is slightly different.
In the first I am simply repeating the code needed to output the
name in order to put the comma directly after it when I need to. In
the second I am creating a string to which I append the comma when
I need to.</p>
<p>IMO, six of one, half a dozen of the other. The only advantage
I'd see in one over the other is that in the second example you'll
only need to update how you output the name in one place if it ever
changes.</p>
<p>Hope this helps. Let us know!</p></div>Charlie Holdertag:help-archives.hannonhill.com,2010-02-09:Comment/102762092011-09-28T19:34:46Z2011-09-28T19:38:01ZVelocity adding unwanted space characters<div><p>Oops. I clearly do have an error in the second example. You'll
want to be careful of the double quotes in your HTML if you use
double quotes for your string wrappers.</p>
<p>This should be okay to use in the second example:<br>
<code>#set ( $output = '<b><a
href="${href}">${title}</a></b>' )</code></p></div>Charlie Holdertag:help-archives.hannonhill.com,2010-02-09:Comment/102762092011-09-28T19:46:07Z2011-09-28T19:46:07ZVelocity adding unwanted space characters<div><p>Thanks Charlie, this worked. I had to modify your second
suggestion a bit, because I'd simplified my script before posting
it, & it includes a conditional on the board member's name as
well as department. the final script looks like this:</p>
<pre>
<code>$_SerializerTool.serialize($_XPathTool.selectSingleNode($contentRoot, "//page-xhtml"), true)
#set ($members = $_XPathTool.selectNodes($contentRoot, "//system-index-block[@type='content_type']/system-page[not(contains(path,'_internal'))]"))
#foreach ($member in $members)
#set ($output = '<p>')
#if ($_XPathTool.selectSingleNode($member, "dynamic-metadata[name='Executive']/value").text == 'yes')
#set ($output = $output + '<img alt="Executive Committee member" src="/facultycouncil.missouri.edu/images/gold-bullet.gif"/>')
#end
#if ($_XPathTool.selectSingleNode($member, "dynamic-metadata[name='ShowLink']/value").text == 'yes')
#set ($output = $output + '<b><a href="' + $member.getChild('path').text + '">' + $member.getChild('title').text + '</a></b>')
#else
#set ($output = $output + '<b>' + $member.getChild('title').text + '</b>')
#end
#set ($department = $_XPathTool.selectSingleNode($member, "dynamic-metadata[name='Department']/value").text.replaceAll("&", "&amp;"))
#if ($department != '')
#set ($output = $output + ', ' + $department)
#end
#set ($output = $output + '</p>')
${output}
#end</code>
</pre>
<p>I quoted only the literals, so as not to worry about quote
marks.</p></div>Shivtag:help-archives.hannonhill.com,2010-02-09:Comment/102762092011-09-28T19:54:57Z2011-09-28T19:54:57ZVelocity adding unwanted space characters<div><p>Glad I could help!</p></div>Charlie Holder