<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NVelocity Tutorial &#8211; FoxDevelop</title>
	<atom:link href="https://www.foxdevelop.com/category/c-en/c-library/nvelocity-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.foxdevelop.com</link>
	<description>Independent Software Developer Studio</description>
	<lastBuildDate>Fri, 14 Aug 2026 03:49:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>

<image>
	<url>https://www.foxdevelop.com/wp-content/uploads/2026/05/fox-svgrepo-com-1.png</url>
	<title>NVelocity Tutorial &#8211; FoxDevelop</title>
	<link>https://www.foxdevelop.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Escape characters and #if conditionals</title>
		<link>https://www.foxdevelop.com/2026/08/14/escape-characters-and-if-conditionals/</link>
					<comments>https://www.foxdevelop.com/2026/08/14/escape-characters-and-if-conditionals/#respond</comments>
		
		<dc:creator><![CDATA[jack]]></dc:creator>
		<pubDate>Fri, 14 Aug 2026 03:49:30 +0000</pubDate>
				<category><![CDATA[NVelocity Tutorial]]></category>
		<guid isPermaLink="false">https://www.foxdevelop.com/?p=8524</guid>

					<description><![CDATA[Backslash Escape + #if Conditional Syntax 1. Backslash \ Escape Rules Pre‑defined variable：#set($dog = "woof woof") VTL Code Rendered Output Explanation $dog woof woof Normal variable resolution \$dog $dog Single ...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Backslash Escape + #if Conditional Syntax</p>



<h4 class="wp-block-heading">1. Backslash \ Escape Rules</h4>



<p class="wp-block-paragraph">Pre‑defined variable：<code>#set($dog = "woof woof")</code></p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>VTL Code</th><th>Rendered Output</th><th>Explanation</th></tr></thead><tbody><tr><td><code>$dog</code></td><td>woof woof</td><td>Normal variable resolution</td></tr><tr><td><code>\$dog</code></td><td>$dog</td><td><strong>Single backslash</strong>: escapes the following <code>$</code>, disables variable parsing and prints <code>$</code> literally</td></tr><tr><td><code>\\$dog</code></td><td>\woof woof</td><td>First <code>\</code> escapes second <code>\</code> → outputs <code>\</code>;<br>remaining <code>$dog</code> resolves normally</td></tr><tr><td><code>\\\$dog</code></td><td>\$dog</td><td><code>\\</code> outputs <code>\</code>, <code>\$</code> outputs <code>$</code></td></tr><tr><td><code>\\\\$dog</code></td><td>\woof woof</td><td><code>\\</code> outputs <code>\</code>, <code>\\</code> outputs <code>\</code>;<br><code>$dog</code> resolves variable</td></tr></tbody></table></figure>



<ol class="wp-block-list">
<li>Velocity variable names <strong>must start with a letter</strong>，<code>$10.0</code> will not be treated as variable and outputs as‑is；so writing <code>Microsoft $10.0 USD</code> in text requires no extra escaping；</li>



<li><strong>Consecutive backslashes cancel out in pairs</strong>；only the <code>\$</code> combination can escape dollar‑sign；</li>



<li>To display <code>$variableName</code> literally on page, standard syntax：<code>\$dog</code>.</li>
</ol>



<h4 class="wp-block-heading">2. #if Branch Conditional Syntax</h4>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#set($ages = 22)</span>
<span class="hljs-comment">#if ($ages &lt;13)</span>
    &lt;h1&gt;You are a child&lt;/h1&gt;
<span class="hljs-comment">#elseif ($ages &lt;18)</span>
    &lt;h1&gt;You are a teenager&lt;/h1&gt;
<span class="hljs-comment">#elseif ($ages &lt;35)</span>
    &lt;h1&gt;You are a young adult&lt;/h1&gt;
<span class="hljs-comment">#elseif ($ages &lt;60)</span>
    &lt;h1&gt;You are middle‑aged&lt;/h1&gt;
<span class="hljs-comment">#else</span>
    &lt;h1&gt;You are elderly&lt;/h1&gt;
<span class="hljs-comment">#end</span></code></span></pre>


<h5 class="wp-block-heading">Execution Result</h5>



<p class="wp-block-paragraph"><code>$ages=22</code>, satisfies <code>22 &lt; 35</code> → output：<code>&lt;h1&gt;You are a young adult&lt;/h1&gt;</code></p>



<h5 class="wp-block-heading">Notes</h5>



<ol class="wp-block-list">
<li>You <strong>must close block with <code>#end</code></strong>, missing it will trigger parsing error；</li>



<li>Keyword：<code>#elseif</code>（<strong>do NOT write as #else if</strong>, that is invalid syntax）；</li>



<li>Supported operators：<code>&lt; > &lt;= >= == != &amp;&amp; ||</code> and more；</li>



<li>Matching rule: evaluate conditions top‑down. Once one branch matches, subsequent branches are skipped.</li>
</ol>



<p class="wp-block-paragraph">Escape characters and #if conditionals</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.foxdevelop.com/2026/08/14/escape-characters-and-if-conditionals/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Extension methods</title>
		<link>https://www.foxdevelop.com/2026/08/14/extension-methods-2/</link>
					<comments>https://www.foxdevelop.com/2026/08/14/extension-methods-2/#respond</comments>
		
		<dc:creator><![CDATA[jack]]></dc:creator>
		<pubDate>Fri, 14 Aug 2026 03:43:58 +0000</pubDate>
				<category><![CDATA[NVelocity Tutorial]]></category>
		<guid isPermaLink="false">https://www.foxdevelop.com/?p=8508</guid>

					<description><![CDATA[1. Backend Person Entity Code (C#) Set object variables in C# backend code 2. VTL Template Code &#038; Rendering Notes 1. $pTest.getMyName() ✅ Velocity parameter‑less method call: parentheses () can ...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"></p>



<h4 class="wp-block-heading">1. Backend Person Entity Code (C#)</h4>


<pre class="wp-block-code"><span><code class="hljs language-cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>
{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span> name;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Name
    {
        <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> name; }
        <span class="hljs-keyword">set</span> { name = <span class="hljs-keyword">value</span>; }
    }
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> Age
    {
        <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> age; }
        <span class="hljs-keyword">set</span> { age = <span class="hljs-keyword">value</span>; }
    }

    <span class="hljs-comment">//Parameter‑less constructor</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Person</span>(<span class="hljs-params"></span>)</span>{}
    <span class="hljs-comment">//Parameterized constructor</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Person</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> name, <span class="hljs-keyword">int</span> age</span>)</span>
    {
        <span class="hljs-keyword">this</span>.name = name;
        <span class="hljs-keyword">this</span>.age = age;
    }

    <span class="hljs-comment">//Parameter‑less instance method</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> <span class="hljs-title">getMyName</span>(<span class="hljs-params"></span>)</span>
    {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.name;
    }

    <span class="hljs-comment">//Multi‑parameter instance method</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> <span class="hljs-title">JoinStr</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> str1, <span class="hljs-keyword">string</span> str2</span>)</span>
    {
        <span class="hljs-keyword">return</span> str1 + str2;
    }
}</code></span></pre>


<p class="wp-block-paragraph">Set object variables in C# backend code</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript">Person pTest = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Tom"</span>, <span class="hljs-number">22</span>);
context.Put(<span class="hljs-string">"pTest"</span>, pTest);</code></span></pre>


<h4 class="wp-block-heading">2. VTL Template Code &#038; Rendering Notes</h4>


<pre class="wp-block-code"><span><code class="hljs language-php">Method call&lt;br /&gt;
$pTest.getMyName();

<span class="hljs-comment">#set($a = "hello")</span>
<span class="hljs-comment">#set($b = "foxdevelop")</span>

$pTest.JoinStr($a, $b);</code></span></pre>


<h5 class="wp-block-heading">1. <code>$pTest.getMyName()</code></h5>



<ul class="wp-block-list">
<li>Call object <strong>parameter‑less method</strong> <code>getMyName()</code></li>



<li>Return <code>Tom</code></li>
</ul>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Velocity parameter‑less method call: parentheses <code>()</code> can be omitted, <code>$pTest.getMyName</code> is equivalent to <code>$pTest.getMyName()</code></p>
</blockquote>



<h5 class="wp-block-heading">2. <code>$pTest.JoinStr($a, $b)</code></h5>



<ul class="wp-block-list">
<li>Invoke method with two string parameters</li>



<li>VTL parses <code>$a</code>, <code>$b</code> first, pass actual arguments <code>"hello"</code>, <code>"foxdevelop"</code></li>



<li>Return concatenated result: <code>hellofoxdevelop</code></li>
</ul>



<h4 class="wp-block-heading">Summary</h4>



<ol class="wp-block-list">
<li><strong>Method name is case‑sensitive</strong><br>C# <code>getMyName()</code> ≠ <code>GetMyName()</code>, template must match source code casing.</li>



<li><strong>Pass variables directly</strong><br>No extra quotes needed, <code>$pTest.JoinStr($a,$b)</code> is correct; do not write <code>$pTest.JoinStr("$a","$b")</code> (will pass literal string &#8220;$a&#8221;)</li>



<li><strong>Difference between properties and methods</strong></li>
</ol>



<ul class="wp-block-list">
<li><code>$pTest.Name</code> → access property (getter)</li>



<li><code>$pTest.getMyName()</code> → invoke method</li>
</ul>



<ol class="wp-block-list">
<li><strong>Return values output automatically</strong><br>Directly write <code>$object.method()</code> inside template, Velocity prints return value automatically; use <code>#set</code> to capture result if output is unwanted:</li>
</ol>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#set($result = $pTest.JoinStr($a,$b))</span></code></span></pre>


<h4 class="wp-block-heading">3. Final Page Render Output</h4>


<pre class="wp-block-code"><span><code class="hljs">Method call
Tom;

hellofoxdevelop;</code></span></pre>


<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Extension methods</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.foxdevelop.com/2026/08/14/extension-methods-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Supplementary notes on variable boundaries</title>
		<link>https://www.foxdevelop.com/2026/08/14/supplementary-notes-on-variable-boundaries/</link>
					<comments>https://www.foxdevelop.com/2026/08/14/supplementary-notes-on-variable-boundaries/#respond</comments>
		
		<dc:creator><![CDATA[jack]]></dc:creator>
		<pubDate>Fri, 14 Aug 2026 03:30:51 +0000</pubDate>
				<category><![CDATA[NVelocity Tutorial]]></category>
		<guid isPermaLink="false">https://www.foxdevelop.com/?p=8492</guid>

					<description><![CDATA[Variable preset: #set ($valok = "hello foxdevelop") Variable Boundary ${} vs . Access Distinction Code Rendered Output Explanation $valok hello foxdevelop Basic variable output $valok.com hello foxdevelop.com Velocity tries to ...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Variable preset: <code>#set ($valok = "hello foxdevelop")</code></p>



<h4 class="wp-block-heading">Variable Boundary <code>${}</code> vs <code>.</code> Access Distinction</h4>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Code</th><th>Rendered Output</th><th>Explanation</th></tr></thead><tbody><tr><td><code>$valok</code></td><td>hello <code>foxdevelop</code></td><td>Basic variable output</td></tr><tr><td><code>$valok.</code>com</td><td>hello <code>foxdevelop</code>.com</td><td>Velocity tries to call property/method <code>cn</code> on object <code>valok</code>; string has no <code>cn</code> method, <strong>raw variable plus suffix gets printed</strong></td></tr><tr><td><code>${valok}.com</code></td><td>hello <code>foxdevelop</code>.com</td><td><code>{}</code> locks variable boundary. After variable <code>valok</code> renders, literal <code>.com</code> is appended (recommended concatenation syntax)</td></tr><tr><td><code>$!valok.com</code></td><td>hello<code>foxdevelop</code>.com</td><td><code>$!</code> silent reference: behaves like <code>$valok</code> if variable exists; outputs empty string instead of raw <code>$xxx</code> when missing</td></tr><tr><td><code>${valok.com}</code></td><td><strong>blank</strong></td><td>Attempts to read property <code>valok.com</code>. String has no <code>com</code> property/method, variable resolves to nothing. Use <code>$!{valok.com}</code> for empty‑string output</td></tr></tbody></table></figure>



<p class="wp-block-paragraph"><br><code>${valok}.cn</code> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Variable plus suffix concatenation<br><code>${valok.cn}</code> <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Access property cn under object valok</p>



<h4 class="wp-block-heading">Single / Double Quotes &amp; Variable Concatenation</h4>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Code</th><th>Rendered Output</th><th>Explanation</th></tr></thead><tbody><tr><td><code>what$valok</code></td><td>whathello <code>foxdevelop</code></td><td>Direct concatenation, Velocity auto‑detects variable boundary</td></tr><tr><td><code>what$"valok"</code></td><td>what$&#8221;valok&#8221;</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Invalid syntax. <code>$</code> cannot be immediately followed by quote, variable cannot be parsed</td></tr><tr><td><code>what"$valok"</code></td><td>what&#8221;hello <code>foxdevelop</code>&#8220;</td><td>Quotes are plain text inside template, they do not trigger string parsing; variable renders normally</td></tr><tr><td><code>what'$valok'</code></td><td>what&#8217;hello <code>foxdevelop</code>&#8216;</td><td>Same as above, single quotes are ordinary literal characters</td></tr><tr><td><code>$valokwhat</code></td><td>$valokwhat</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Variable unrecognizable! Velocity treats <code>valokwhat</code> as full variable name, prints raw text when not found</td></tr><tr><td><code>${valok}what</code></td><td>hello <code>foxdevelop</code>what</td><td><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Curly braces lock boundary, fixes variable‑text merging issue</td></tr></tbody></table></figure>



<h4 class="wp-block-heading">Summary</h4>



<ol class="wp-block-list">
<li><strong>Always use <code>${variableName}</code> when variable merges with adjacent text</strong><br>Wrong: <code>$valokwhat</code>；Correct: <code>${valok}what</code></li>



<li><strong>Append literal suffix: use <code>${valok}.cn</code> instead of <code>${valok.cn}</code></strong></li>



<li><strong><code>$!variable</code> silent reference</strong>: outputs blank if variable missing, raw <code>$xxx</code> will not appear</li>



<li><strong>Single / double quotes inside template HTML are plain characters</strong>；<strong>Only inside #set assignments</strong> do single quotes (no variable parsing) and double quotes (parse variables) behave differently</li>
</ol>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#set($a = "$valok") // Double quotes: resolve variable, a=hello bamn</span>
<span class="hljs-comment">#set($a = '$valok') // Single quotes: skip variable resolution, a=$valok</span></code></span></pre>


<p class="wp-block-paragraph">Supplementary notes on variable boundaries</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.foxdevelop.com/2026/08/14/supplementary-notes-on-variable-boundaries/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Iterate collections</title>
		<link>https://www.foxdevelop.com/2026/08/14/iterate-collections/</link>
					<comments>https://www.foxdevelop.com/2026/08/14/iterate-collections/#respond</comments>
		
		<dc:creator><![CDATA[jack]]></dc:creator>
		<pubDate>Fri, 14 Aug 2026 03:15:56 +0000</pubDate>
				<category><![CDATA[NVelocity Tutorial]]></category>
		<guid isPermaLink="false">https://www.foxdevelop.com/?p=8476</guid>

					<description><![CDATA[Velocity #foreach Iterate over Collections &#038; Objects 1. Iterate over String List C# Back‑end Code Velocity Template Code Execution result: Loop through each string in the list and output with ...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Velocity #foreach Iterate over Collections &#038; Objects</p>



<h4 class="wp-block-heading">1. Iterate over String List</h4>



<h5 class="wp-block-heading">C# Back‑end Code</h5>


<pre class="wp-block-code"><span><code class="hljs language-cs">List&lt;<span class="hljs-keyword">string</span>&gt; strList = <span class="hljs-keyword">new</span> List&lt;<span class="hljs-keyword">string</span>&gt;() {<span class="hljs-string">"dsd"</span>,<span class="hljs-string">"sdfsd"</span>,<span class="hljs-string">"asdfsd"</span>,<span class="hljs-string">"asdfsdfsd"</span>};
context.Put(<span class="hljs-string">"strList"</span>, strList);</code></span></pre>


<h5 class="wp-block-heading">Velocity Template Code</h5>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#foreach($str in $strList)</span>
    $str &lt;br/&gt;
<span class="hljs-comment">#end</span></code></span></pre>


<p class="wp-block-paragraph">Execution result: Loop through each string in the list and output with line breaks.</p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="406" height="184" src="https://www.foxdevelop.com/wp-content/uploads/2026/08/image-16.png" alt="" class="wp-image-7102" srcset="https://www.foxdevelop.com/wp-content/uploads/2026/08/image-16.png 406w, https://www.foxdevelop.com/wp-content/uploads/2026/08/image-16-300x136.png 300w" sizes="(max-width: 406px) 100vw, 406px" /></figure>



<h5 class="wp-block-heading">Syntax Explanation</h5>



<p class="wp-block-paragraph"><code>#foreach(loopVariable in collection)</code> starts the loop, <code>#end</code> closes the loop.</p>



<h4 class="wp-block-heading">2. Iterate over List of Custom Entity Classes</h4>



<h5 class="wp-block-heading">1. Person Entity (C#)</h5>


<pre class="wp-block-code"><span><code class="hljs language-cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>
{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span> name;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Name
    {
        <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> name; }
        <span class="hljs-keyword">set</span> { name = <span class="hljs-keyword">value</span>; }
    }
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> age;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> Age
    {
        <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> age; }
        <span class="hljs-keyword">set</span> { age = <span class="hljs-keyword">value</span>; }
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Person</span>(<span class="hljs-params"></span>)</span>{}
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Person</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> name,<span class="hljs-keyword">int</span> age</span>)</span>
    {
        <span class="hljs-keyword">this</span>.name = name;
        <span class="hljs-keyword">this</span>.age = age;
    }
}</code></span></pre>


<h5 class="wp-block-heading">2. Back‑end Assemble List</h5>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-keyword">List</span>&lt;Person&gt; pList = <span class="hljs-keyword">new</span> <span class="hljs-keyword">List</span>&lt;Person&gt;();
pList.Add(<span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Jack"</span>, <span class="hljs-number">1</span>));
pList.Add(<span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Ben"</span>, <span class="hljs-number">11</span>));
pList.Add(<span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Kevin"</span>, <span class="hljs-number">100</span>));
pList.Add(<span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Simon"</span>, <span class="hljs-number">500</span>));
pList.Add(<span class="hljs-keyword">new</span> Person(<span class="hljs-string">"Lulu"</span>, <span class="hljs-number">20</span>));
context.Put(<span class="hljs-string">"pList"</span>, pList);</code></span></pre>


<h5 class="wp-block-heading">3. Template Property Access Syntax</h5>



<p class="wp-block-paragraph">Template code:</p>


<pre class="wp-block-code"><span><code class="hljs language-xml">#foreach($per in $pList)
<span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>$per.name<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>$per.age<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
#end</code></span></pre>


<p class="wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Cannot read data! Errors or blank output</strong></p>



<h4 class="wp-block-heading">Underlying Principle</h4>



<p class="wp-block-paragraph">Velocity rules for accessing object properties:<br><code>$per.xxx</code> automatically looks for <strong>public Get method <code>GetXxx()</code></strong></p>



<ul class="wp-block-list">
<li><code>$per.name</code> → Attempts to call <code>per.getName()</code></li>



<li><code>$per.age</code> → Attempts to call <code>per.getAge()</code></li>
</ul>



<p class="wp-block-paragraph">Your entity:</p>



<ul class="wp-block-list">
<li>Private fields: <code>name</code>, <code>age</code></li>



<li><strong>Public properties start with uppercase: <code>Name</code>, <code>Age</code></strong>, corresponding methods <code>GetName()</code>, <code>GetAge()</code></li>
</ul>



<p class="wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" />Correct template syntax:</p>


<pre class="wp-block-code"><span><code class="hljs language-xml">#foreach($per in $pList)
<span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>$per.Name<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>$per.Age<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
#end</code></span></pre>


<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">Note: C# convention uses uppercase first‑letter for properties; Java entity properties are usually lowercase. This is a common case‑sensitivity pitfall for beginners.</p>
</blockquote>



<h4 class="wp-block-heading">3. Built‑in Variables for #foreach</h4>



<p class="wp-block-paragraph">Variables available directly inside loops:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Variable</th><th>Function</th></tr></thead><tbody><tr><td><code>$foreach.index</code></td><td>Current index, <strong>starts at 0</strong></td></tr><tr><td><code>$foreach.count</code></td><td>Current sequence number, <strong>starts at 1</strong></td></tr><tr><td><code>$foreach.first</code></td><td>True if it is the first record (boolean)</td></tr><tr><td><code>$foreach.last</code></td><td>True if it is the last record (boolean)</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">Example:</p>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#foreach($per in $pList)</span>
    $foreach.count , Name: $per.Name
<span class="hljs-comment">#end</span></code></span></pre>


<h4 class="wp-block-heading">4. Render as HTML Table</h4>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">table</span> <span class="hljs-attr">border</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cellpadding</span>=<span class="hljs-string">"0"</span> <span class="hljs-attr">cellspacing</span>=<span class="hljs-string">"0"</span>&gt;</span>
#foreach($per in $pList)
    <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>$per.Name<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>$per.Age<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
#end
<span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span></code></span></pre>


<h4 class="wp-block-heading">Summary</h4>



<ol class="wp-block-list">
<li><code>#foreach(variable in collection)</code> + <code>#end</code> is Velocity standard loop syntax;</li>



<li>For ordinary List iteration, output <code>$variable</code> directly;</li>



<li>Iterate entity objects: <code>$object.propertyName</code> <strong>match property case‑sensitivity</strong>, never access private fields;</li>



<li>C# entities should use uppercase first‑letter for properties, keep consistent naming in templates;</li>



<li>When data shows blank, first check <strong>property case and existence of get accessor</strong>.</li>
</ol>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">Iterate collections</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.foxdevelop.com/2026/08/14/iterate-collections/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Comments</title>
		<link>https://www.foxdevelop.com/2026/08/14/comments/</link>
					<comments>https://www.foxdevelop.com/2026/08/14/comments/#respond</comments>
		
		<dc:creator><![CDATA[jack]]></dc:creator>
		<pubDate>Fri, 14 Aug 2026 03:09:51 +0000</pubDate>
				<category><![CDATA[NVelocity Tutorial]]></category>
		<guid isPermaLink="false">https://www.foxdevelop.com/?p=8460</guid>

					<description><![CDATA[1. Distinction of Template Comments Syntax Type Features &#60;!-- HTML comment --&#62; Native HTML comment Output to final webpage source code, visible when viewing page source in browser ## Single‑line ...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">1. Distinction of Template Comments</h3>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Syntax</th><th>Type</th><th>Features</th></tr></thead><tbody><tr><td><code>&lt;!-- HTML comment --&gt;</code></td><td>Native HTML comment</td><td><strong>Output to final webpage source code</strong>, visible when viewing page source in browser</td></tr><tr><td><code>## Single‑line comment</code></td><td>Velocity single‑line comment</td><td>Recognized only by the engine, <strong>not rendered to page</strong>, effective till end of line</td></tr><tr><td><code>#* Multi‑line comment content *#</code></td><td>Velocity multi‑line comment</td><td>Supports line breaks, <strong>not rendered to page</strong></td></tr></tbody></table></figure>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">Development suggestion: Use <code>##</code> / <code>#* *#</code> for business‑related code comments to avoid leaking comment content in page source.</p>
</blockquote>



<h3 class="wp-block-heading">2. Variable Naming Rules</h3>



<p class="wp-block-paragraph">Official basic naming rules</p>



<ol class="wp-block-list">
<li>The first character of variable <strong>must be an English letter</strong> (a‑z / A‑Z)</li>



<li>Subsequent allowed characters: letters, digits <code>0‑9</code>, underscore <code>_</code>, hyphen <code>-</code></li>
</ol>



<h4 class="wp-block-heading">Hyphen <code>-</code> causes operator ambiguity</h4>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#set($name-d = "test")</span></code></span></pre>


<p class="wp-block-paragraph">It throws runtime error: <code>Encountered "name" ... Was expecting one of:</code><br><strong>Cause: Velocity interprets <code>-</code> as subtraction operator!</strong><br>Engine parsing logic: <code>$name - d</code>, treated as variable <code>$name</code> minus variable <code>d</code>, leading to syntax conflict.</p>



<p class="wp-block-paragraph">Two solutions:</p>



<ol class="wp-block-list">
<li><strong>Recommended: use underscore naming</strong> (no ambiguity, common convention)</li>
</ol>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#set($name_d = "hello world")</span></code></span></pre>


<ol start="2" class="wp-block-list">
<li><strong>Use curly braces to define variable boundary</strong> (compatible with hyphen‑containing names)</li>
</ol>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#set(${name-d} = "hello world")</span>
${name-d}</code></span></pre>


<h4 class="wp-block-heading">Valid / Invalid Examples Comparison</h4>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment"># <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/2705.png" alt="✅" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Valid</span>
<span class="hljs-comment">#set($name_d = "underscore")</span>
<span class="hljs-comment">#set($name12d = "letter start plus number")</span>
<span class="hljs-comment">#set(${name-1_2d} = "hyphen requires curly braces")</span>

<span class="hljs-comment"># <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/274c.png" alt="❌" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Direct hyphen without curly braces → parse error</span>
<span class="hljs-comment">#set($name-d = "Error!")</span>
<span class="hljs-comment">#set($name-1_2d = "Error!")</span></code></span></pre>


<h3 class="wp-block-heading">3. #set Syntax Specification</h3>



<p class="wp-block-paragraph">Standard writing (spaces around brackets are flexible, consistent style is suggested)</p>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#set($variableName = "value")</span></code></span></pre>


<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">String values must be wrapped in double quotes; keep spaces around equal sign for better readability.</p>
</blockquote>



<p class="wp-block-paragraph">Comments</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.foxdevelop.com/2026/08/14/comments/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Template reuse</title>
		<link>https://www.foxdevelop.com/2026/08/14/template-reuse/</link>
					<comments>https://www.foxdevelop.com/2026/08/14/template-reuse/#respond</comments>
		
		<dc:creator><![CDATA[jack]]></dc:creator>
		<pubDate>Fri, 14 Aug 2026 02:41:33 +0000</pubDate>
				<category><![CDATA[NVelocity Tutorial]]></category>
		<guid isPermaLink="false">https://www.foxdevelop.com/?p=8444</guid>

					<description><![CDATA[1. Core Purpose The Velocity template engine achieves template reuse via #include / #parse.Common webpage sections such as page header navigation _head.htm and footer copyright _footer.htm can be extracted into ...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"></p>



<h3 class="wp-block-heading">1. Core Purpose</h3>



<p class="wp-block-paragraph">The Velocity template engine achieves <strong>template reuse</strong> via <code>#include</code> / <code>#parse</code>.<br>Common webpage sections such as page header navigation <code>_head.htm</code> and footer copyright <code>_footer.htm</code> can be extracted into separate sub‑templates and imported in pages, avoiding duplicate HTML code across multiple places.</p>



<h3 class="wp-block-heading">2. Key Differences Between the Two</h3>



<h4 class="wp-block-heading">1. <code>#include("file path")</code></h4>



<p class="wp-block-paragraph"><strong>Outputs the raw file content as‑is; Velocity variables (<code>$!{xxx}</code>) inside the sub‑template will NOT be parsed</strong></p>



<ul class="wp-block-list">
<li><code>$!{testinclude}</code> in sub‑templates will not be rendered; the literal string <code>$!{testinclude}</code> gets printed directly</li>



<li>Supports importing <strong>multiple files at once</strong>: <code>#include("a.htm","b.htm")</code></li>



<li>Best for: static‑only HTML and static snippets without any VTL variables</li>
</ul>



<h4 class="wp-block-heading">2. <code>#parse("file path")</code></h4>



<p class="wp-block-paragraph"><strong>Imports the sub‑template and fully interprets Velocity syntax</strong>. Variables passed from the parent template render normally inside sub‑templates</p>



<ul class="wp-block-list">
<li><code>$!{testinclude}</code> inside sub‑templates reads context variables and resolves to values supplied by backend code</li>



<li><strong>Accepts only one single file path</strong>; multiple simultaneous imports are not supported</li>



<li>Best for: common templates with <strong>variables</strong> such as headers and footers (page titles, dynamic copyright text, etc.)</li>
</ul>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">From your screenshot notes:<br><code>#include simply pastes sub‑template source code into the main template without parsing, while #parse performs full syntax parsing</code></p>
</blockquote>



<h3 class="wp-block-heading">3. Practical Example</h3>



<h4 class="wp-block-heading">1. Backend code (.cs)</h4>


<pre class="wp-block-code"><span><code class="hljs language-javascript">context.Put(<span class="hljs-string">"testinclude"</span>, <span class="hljs-string">"Text from sub‑template:"</span>);</code></span></pre>


<h4 class="wp-block-heading">2. Content of sub‑template <code>_footer.htm</code></h4>


<pre class="wp-block-code"><span><code class="hljs">Copyright info $!{testinclude}</code></span></pre>


<h5 class="wp-block-heading">Case ① Using <code>#include("./themes/default/_footer.htm")</code></h5>



<p class="wp-block-paragraph">Page output:</p>


<pre class="wp-block-code"><span><code class="hljs">Copyright info $!{testinclude}</code></span></pre>


<p class="wp-block-paragraph">The variable stays unparsed; the placeholder is printed literally.</p>



<h5 class="wp-block-heading">Case ② Using <code>#parse("./themes/default/_footer.htm")</code></h5>



<p class="wp-block-paragraph">Page output:</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript">Copyright info Text <span class="hljs-keyword">from</span> sub‑template:</code></span></pre>


<p class="wp-block-paragraph">Variables render correctly. This explains why <code>#parse</code> is preferred for shared header and footer templates in real‑world projects.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">4. Recommended Usage Scenarios</h3>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Directive</th><th>Use‑case</th></tr></thead><tbody><tr><td><code>#include</code></td><td>Static assets, plain text, static HTML fragments where variable rendering is unnecessary</td></tr><tr><td><code>#parse</code></td><td>Shared headers, footers, sidebars; sub‑templates containing <code>$variables</code>, <code>#if</code> and other VTL syntax</td></tr></tbody></table></figure>



<p class="wp-block-paragraph"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Development guidelines:<br>Use <code>#parse</code> consistently for shared components likely requiring dynamic variables, such as navigation bars and footer copyright blocks. Use <code>#include</code> for plain static text and style fragments.</p>



<p class="wp-block-paragraph">Template reuse</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.foxdevelop.com/2026/08/14/template-reuse/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Define variables inside templates</title>
		<link>https://www.foxdevelop.com/2026/08/14/define-variables-inside-templates/</link>
					<comments>https://www.foxdevelop.com/2026/08/14/define-variables-inside-templates/#respond</comments>
		
		<dc:creator><![CDATA[jack]]></dc:creator>
		<pubDate>Fri, 14 Aug 2026 02:31:26 +0000</pubDate>
				<category><![CDATA[NVelocity Tutorial]]></category>
		<guid isPermaLink="false">https://www.foxdevelop.com/?p=8427</guid>

					<description><![CDATA[The variables covered so far are defined in C# code‑behind files and then consumed in templates. In fact, you can define variables directly inside templates using the set directive. 1. ...]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">The variables covered so far are defined in C# code‑behind files and then consumed in templates. In fact, you can define variables directly inside templates using the set directive.</p>



<h3 class="wp-block-heading">1. Defining Variables with <code>#set()</code></h3>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">Two ways to declare variables:<br>Method 1: Define variables in advance via backend C#/Java code, read them in template pages;<br>Method 2: <strong>Define variables directly inside templates with <code>#set()</code></strong> (core content of this lesson)</p>



<p class="wp-block-paragraph"></p>
</blockquote>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#set($name = "hello foxdevelop.com")</span>
<span class="hljs-comment">#set($okay = "common baby")</span>
<span class="hljs-comment">#set($nameokay = "$name|$okay")</span></code></span></pre>


<ul class="wp-block-list">
<li><code>#set(variable = value)</code> supports strings, numbers, arrays, object properties and assignments from other variables</li>



<li>Weak‑typed, no need to declare variable types</li>
</ul>



<h3 class="wp-block-heading">2. Two Variable‑Rendering Syntax and Specifications</h3>



<h4 class="wp-block-heading">2.1 Basic Forms</h4>



<ul class="wp-block-list">
<li><code>$name</code>: If the variable does not exist, outputs literal text <code>$name</code></li>



<li><code>$!name</code>: <strong>Silent rendering</strong>; outputs blank content when variable is missing (recommended to avoid stray <code>$xxx</code> text on pages)</li>



<li>Recommended standard syntax: <strong><code>${name}</code></strong> (curly braces mark variable boundaries explicitly)</li>
</ul>



<h4 class="wp-block-heading">2.2 Boundary Issue Example</h4>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#set($name = "hello")</span>
$namehello     × Engine parses variable <span class="hljs-keyword">as</span> $namehello <span class="hljs-keyword">and</span> cannot resolve it
${name}hello   √ Correct: resolves $name then appends plain‑text hello</code></span></pre>


<h3 class="wp-block-heading">3. Single‑Line Comments <code>##</code></h3>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">## This is a comment and will never appear in rendered output</span>
<span class="hljs-comment">#set($name = "test") ## End‑of‑line comments are also supported</span></code></span></pre>


<ol class="wp-block-list">
<li>Comment content <strong>will not be printed into the final rendered page</strong>;</li>



<li>You cannot see comment text when viewing browser page source (comments get discarded during template‑engine processing).</li>
</ol>



<h3 class="wp-block-heading">4. Multiple Assignment Types Supported by #set</h3>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">## String assignment</span>
<span class="hljs-comment">#set($name = "hello world")</span>

<span class="hljs-comment">## Assign one variable to another</span>
<span class="hljs-comment">#set($obj= $name)</span>

<span class="hljs-comment">## Assign object properties</span>
<span class="hljs-comment">#set($obj.Friend = "monica")</span>
<span class="hljs-comment">#set($obj.Blame = $whitehouse.Leak)</span>

<span class="hljs-comment">## Call methods and assign return values</span>
<span class="hljs-comment">#set($obj.Plan = $spindoctor.weave($web))</span>

<span class="hljs-comment">## Numeric assignment</span>
<span class="hljs-comment">#set($obj.Number = 123)</span>

<span class="hljs-comment">## Array assignment</span>
<span class="hljs-comment">#set($obj.Say = &#91;"Not", $my, "fault"])</span></code></span></pre>


<h3 class="wp-block-heading">5. Special Rules for Non‑existent Variables</h3>


<pre class="wp-block-code"><span><code class="hljs language-php">$sadfsdfsd</code></span></pre>


<p class="wp-block-paragraph">When the template engine cannot resolve a variable:</p>



<ul class="wp-block-list">
<li>Ordinary syntax <code>$sadfsdfsd</code>: outputs literal string <code>$sadfsdfsd</code></li>



<li>Using <code>$!sadfsdfsd</code>: outputs empty string for missing variables</li>
</ul>



<p class="wp-block-paragraph">Define variables inside templates</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.foxdevelop.com/2026/08/14/define-variables-inside-templates/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Fundamental template syntax</title>
		<link>https://www.foxdevelop.com/2026/08/14/fundamental-template-syntax/</link>
					<comments>https://www.foxdevelop.com/2026/08/14/fundamental-template-syntax/#respond</comments>
		
		<dc:creator><![CDATA[jack]]></dc:creator>
		<pubDate>Fri, 14 Aug 2026 02:21:48 +0000</pubDate>
				<category><![CDATA[NVelocity Tutorial]]></category>
		<guid isPermaLink="false">https://www.foxdevelop.com/?p=8411</guid>

					<description><![CDATA[1. Pass Entity Objects to Templates 1.1 PageInfo Entity Class Previously we passed simple strings, which is inefficient and hard‑to‑maintain. Wrapping data into entity objects makes code much clearer. 1.2 ...]]></description>
										<content:encoded><![CDATA[
<h3 class="wp-block-heading">1. Pass Entity Objects to Templates</h3>



<h4 class="wp-block-heading">1.1 PageInfo Entity Class</h4>



<p class="wp-block-paragraph">Previously we passed simple strings, which is inefficient and hard‑to‑maintain. Wrapping data into entity objects makes code much clearer.</p>


<pre class="wp-block-code"><span><code class="hljs language-cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">PageInfo</span>
{
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">bool</span> IsShowImage;
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">bool</span> IsShowImage1
    {
        <span class="hljs-keyword">get</span> { <span class="hljs-keyword">return</span> IsShowImage; }
        <span class="hljs-keyword">set</span> { IsShowImage = <span class="hljs-keyword">value</span>; }
    }
}</code></span></pre>


<ul class="wp-block-list">
<li>Private field <code>IsShowImage</code>, exposed via public property <strong><code>IsShowImage1</code></strong></li>



<li>Template access syntax: <code>$info.IsShowImage1</code></li>
</ul>



<h4 class="wp-block-heading">1.2 Populate Context Data</h4>


<pre class="wp-block-code"><span><code class="hljs language-cs"><span class="hljs-comment">//Template context container</span>
IContext context = <span class="hljs-keyword">new</span> VelocityContext();

PageInfo pi = <span class="hljs-keyword">new</span> PageInfo();
pi.IsShowImage1 = <span class="hljs-literal">true</span>;

<span class="hljs-comment">//Store object pi under key name info into context</span>
context.Put(<span class="hljs-string">"info"</span>,pi);

<span class="hljs-comment">//String variable sample</span>
context.Put(<span class="hljs-string">"infotitle"</span>,<span class="hljs-string">"Hello"</span>);</code></span></pre>


<p class="wp-block-paragraph"><code>context.Put("keyName",data)</code>: pass data to template, template reads values by <code>$keyName</code>.</p>



<h3 class="wp-block-heading">2. Core Velocity Template Syntax</h3>



<h4 class="wp-block-heading">2.1 Directive Marker <code>#</code></h4>



<p class="wp-block-paragraph">Statements starting with <code>#</code> are directives, common ones:<br><code>#if</code>, <code>#else</code>, <code>#end</code>, <code>#foreach</code>, <code>#set</code>, <code>#include</code></p>



<p class="wp-block-paragraph">Conditional rendering example:</p>



<p class="wp-block-paragraph">Prepare two image files and place them under Contents/Images in website root directory. Toggle IsShowImage1 to render different images.</p>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-comment">#if($info.IsShowImage1)</span>
    &lt;img src=<span class="hljs-string">"./Contents/Images/a.gif"</span> /&gt;
<span class="hljs-comment">#else</span>
    &lt;img src=<span class="hljs-string">"./Contents/Images/b.gif"</span> /&gt;
<span class="hljs-comment">#end</span></code></span></pre>


<p class="wp-block-paragraph">Logic: render a.gif when <code>IsShowImage1=true</code>, otherwise render b.gif</p>



<h4 class="wp-block-heading">2.2 Variable Marker <code>$</code></h4>



<ul class="wp-block-list">
<li><code>$variableName</code>: output variable value</li>



<li><code>${variableName}</code>: <strong>variable boundary delimiter</strong> (critical!)</li>
</ul>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">Scenario: text directly follows variable, engine cannot tell where variable name ends.</p>
</blockquote>


<pre class="wp-block-code"><span><code class="hljs language-php">Incorrect: $infotitlefoxdevelop.com
Engine parses whole string <span class="hljs-keyword">as</span> variable infotitlefoxdevelop, which does not exist!

Correct: ${infotitle}foxdevelop.com
Engine resolves infotitle variable then concatenate foxdevelop.com string</code></span></pre>


<h4 class="wp-block-heading">2.3 Silent‑output Modifier <code>!</code> → <code>$!variableName</code></h4>



<p class="wp-block-paragraph">Default: if variable missing, page outputs raw text <code>$variableName</code><br><code>$!variableName</code>: output blank when variable missing or null, do not print dollar sign</p>



<p class="wp-block-paragraph">Comparison example:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Syntax</th><th>Variable exists(value=&#8221;Hello&#8221;)</th><th>Variable missing</th></tr></thead><tbody><tr><td><code>$infotitle</code></td><td>Hello</td><td><code>$infotitle</code></td></tr><tr><td><code>$!infotitle</code></td><td>Hello</td><td>(blank, nothing rendered)</td></tr><tr><td><code>${infotitle}</code></td><td>Hello</td><td><code>${infotitle}</code></td></tr><tr><td><code>$!{infotitle}</code></td><td>Hello</td><td>blank</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">Full test snippet:</p>


<pre class="wp-block-code"><span><code class="hljs language-xml">${infotitle}foxdevelop.com <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span>
$!infotitlefoxdevelop.com <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span>
$infotitle <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span>
$!infotitle <span class="hljs-tag">&lt;<span class="hljs-name">br</span>/&gt;</span></code></span></pre>


<h3 class="wp-block-heading">Summary</h3>



<ol class="wp-block-list">
<li>Always wrap variable with <code>${variable}</code> when concatenating text after variable</li>



<li>Prefer <code>$!{variableName}</code> in production environment, avoid raw <code>$xxx</code> breaking layout on null variables</li>



<li>For boolean property check with <code>#if</code>, write directly <code>#if($info.IsShowImage1)</code>, no need extra <code>==true</code></li>
</ol>



<p class="wp-block-paragraph">Fundamental template syntax</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.foxdevelop.com/2026/08/14/fundamental-template-syntax/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Introduction to NVelocity</title>
		<link>https://www.foxdevelop.com/2026/08/01/introduction-to-nvelocity/</link>
					<comments>https://www.foxdevelop.com/2026/08/01/introduction-to-nvelocity/#respond</comments>
		
		<dc:creator><![CDATA[jack]]></dc:creator>
		<pubDate>Sat, 01 Aug 2026 12:39:53 +0000</pubDate>
				<category><![CDATA[NVelocity Tutorial]]></category>
		<guid isPermaLink="false">https://www.foxdevelop.com/?p=7123</guid>

					<description><![CDATA[A template engine for the .NET platform, ported from Velocity on the Java platform. Introduction Widely adopted within ASP.NET projects;ASP.NET MVC ships with the Razor template engine; Many developers turn ...]]></description>
										<content:encoded><![CDATA[<p>A template engine for the .NET platform, ported from Velocity on the Java platform.</p>


<h4 class="wp-block-heading">Introduction</h4>



<p class="wp-block-paragraph">Widely adopted within ASP.NET projects;<br>ASP.NET MVC ships with the Razor template engine;</p>



<p class="wp-block-paragraph">Many developers turn to NVelocity when they want to introduce the MVC pattern into legacy <code>.aspx</code> web form projects.</p>



<p class="wp-block-paragraph">NVelocity is no longer actively maintained and should only be used for older .NET Framework legacy projects. New projects are recommended to adopt modern template engines such as Razor and Handlebars.NET.</p>



<p class="wp-block-paragraph">Version compatibility with .NET Framework</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>.NET Framework Version</th><th>NVelocity Version</th></tr></thead><tbody><tr><td>.NET 4.0</td><td>1.0 (latest official release)</td></tr><tr><td>.NET 2.0</td><td>0.5</td></tr><tr><td>.NET 1.0</td><td>0.48</td></tr></tbody></table></figure>



<p class="wp-block-paragraph">Velocity is a mature Java template engine known for its clean syntax that uses <code>#</code> and <code>$</code> as template markers. NVelocity fully implements this syntax specification.</p>



<h4 class="wp-block-heading">Download</h4>



<p class="wp-block-paragraph">The official repository received its last update in 2018, with version 1.2.0 being the newest release.</p>



<p class="wp-block-paragraph">You can grab the source code via the link below</p>



<p class="wp-block-paragraph">It is open-source and hosted on GitHub <a href="https://github.com/castleproject/NVelocity/tree/master">castleproject/NVelocity: Castle&#8217;s NVelocity</a></p>



<p class="wp-block-paragraph">Official downloads do not include precompiled DLL binaries. You may compile the source yourself after downloading, or directly import the project into your existing solution.</p>



<figure class="wp-block-image size-full"><img decoding="async" width="397" height="269" src="https://www.foxdevelop.com/wp-content/uploads/2026/08/image-13.png" alt="" class="wp-image-7081" srcset="https://www.foxdevelop.com/wp-content/uploads/2026/08/image-13.png 397w, https://www.foxdevelop.com/wp-content/uploads/2026/08/image-13-300x203.png 300w" sizes="(max-width: 397px) 100vw, 397px" /></figure>



<p class="wp-block-paragraph">Open the solution file with Visual Studio</p>



<p class="wp-block-paragraph">The final release targets .NET Core 2.1. Developers are advised to migrate it to .NET 4.8.</p>



<p class="wp-block-paragraph">After compilation, you can locate the compiled DLL inside the output directory</p>



<figure class="wp-block-image size-full"><img decoding="async" width="372" height="76" src="https://www.foxdevelop.com/wp-content/uploads/2026/08/image-14.png" alt="" class="wp-image-7082" srcset="https://www.foxdevelop.com/wp-content/uploads/2026/08/image-14.png 372w, https://www.foxdevelop.com/wp-content/uploads/2026/08/image-14-300x61.png 300w" sizes="(max-width: 372px) 100vw, 372px" /></figure>



<p class="wp-block-paragraph">You can then reference NVelocity.dll within your other ASP.NET Web projects.</p>



<h4 class="wp-block-heading">Project Integration</h4>



<p class="wp-block-paragraph">Reference the assembly: NVelocity.dll<br>Core benefit: This template engine greatly simplifies dynamic template switching (theme skinning) for websites.</p>



<p class="wp-block-paragraph">Create a folder named Themes at your website root as the root directory for all templates<br>Inside Themes, create separate folders for different themes, for example:</p>



<p class="wp-block-paragraph">Create the Themes folder at website root to store all theme resources</p>



<h4 class="wp-block-heading">Theme Directory Structure</h4>



<p class="wp-block-paragraph">Create a folder named default for your base template set.<br>Additional theme folders such as Themes/blue and Themes/dark can be added later.</p>


<pre class="wp-block-code"><span><code class="hljs language-php">Website Root
└── Themes
    ├── <span class="hljs-keyword">default</span>      <span class="hljs-comment"># Default theme template files</span>
    ├── dark         <span class="hljs-comment"># Dark theme template files</span>
    └── blue         <span class="hljs-comment"># Blue theme template files</span></code></span></pre>


<p class="wp-block-paragraph">On runtime, the engine reads the active theme name from configuration and loads templates from the corresponding theme folder.<br>Template switching simply updates configuration values to point to another theme directory without modifying business logic.<br>This approach works well for legacy ASP.NET WebForm (.aspx) projects implementing front-end skin switching.</p>



<h4 class="wp-block-heading">aspx</h4>



<p class="wp-block-paragraph">Create a new .aspx page called <code>Register.aspx</code> in the website root for demonstration purposes.</p>



<p class="wp-block-paragraph">Create a template file named <code>register.htm</code> under <code>Themes/default</code>. This file serves as the NVelocity template containing HTML markup and Velocity syntax.</p>



<p class="wp-block-paragraph">Open <code>Register.aspx</code>, <strong>keep only the page directive</strong> and remove all other HTML markup:</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">%@</span> <span class="hljs-attr">Page</span> <span class="hljs-attr">Language</span>=<span class="hljs-string">"C#"</span> <span class="hljs-attr">AutoEventWireup</span>=<span class="hljs-string">"true"</span> <span class="hljs-attr">CodeBehind</span>=<span class="hljs-string">"Register.aspx.cs"</span> <span class="hljs-attr">Inherits</span>=<span class="hljs-string">"NVelocityStudy.Web.Register"</span> %&gt;</span></code></span></pre>


<p class="wp-block-paragraph">The .aspx page will no longer handle rendering directly and acts merely as an entry point. Backend code invokes NVelocity to load the <code>register.htm</code> template and output rendered HTML content.</p>


<pre class="wp-block-code"><span><code class="hljs language-css"><span class="hljs-selector-tag">Website</span> <span class="hljs-selector-tag">Root</span>
├─ <span class="hljs-selector-tag">Register</span><span class="hljs-selector-class">.aspx</span>          # <span class="hljs-selector-tag">Access</span> <span class="hljs-selector-tag">entry</span> <span class="hljs-selector-tag">point</span>
├─ <span class="hljs-selector-tag">Register</span><span class="hljs-selector-class">.aspx</span><span class="hljs-selector-class">.cs</span>       # <span class="hljs-selector-tag">Backend</span> <span class="hljs-selector-tag">logic</span>
└─ <span class="hljs-selector-tag">Themes</span>
    └─ <span class="hljs-selector-tag">default</span>
        └─ <span class="hljs-selector-tag">register</span><span class="hljs-selector-class">.htm</span>   # <span class="hljs-selector-tag">NVelocity</span> <span class="hljs-selector-tag">template</span> <span class="hljs-selector-tag">file</span></code></span></pre>


<p class="wp-block-paragraph">You will implement logic within <code>Register.aspx.cs</code> to load the <code>register.htm</code> template from the active theme, inject model data and produce final HTML output.</p>



<p class="wp-block-paragraph">To switch themes, modify the directory path setting so the engine loads <code>register.htm</code> from another theme folder.</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="325" height="357" src="https://www.foxdevelop.com/wp-content/uploads/2026/08/image-15.png" alt="" class="wp-image-7083" srcset="https://www.foxdevelop.com/wp-content/uploads/2026/08/image-15.png 325w, https://www.foxdevelop.com/wp-content/uploads/2026/08/image-15-273x300.png 273w" sizes="auto, (max-width: 325px) 100vw, 325px" /></figure>



<h4 class="wp-block-heading">aspx.cs</h4>



<p class="wp-block-paragraph">Open the backend file <code>Register.aspx.cs</code>. Implement NVelocity rendering logic inside the <strong>Page_Load</strong> method to parse templates when the page loads.</p>


<pre class="wp-block-code"><span><code class="hljs language-cs"><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Collections.Generic;
<span class="hljs-keyword">using</span> System.Web;
<span class="hljs-keyword">using</span> System.Web.UI;
<span class="hljs-keyword">using</span> System.Web.UI.WebControls;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">NVelocityStudy.Web</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">partial</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Register</span> : <span class="hljs-title">System</span>.<span class="hljs-title">Web</span>.<span class="hljs-title">UI</span>.<span class="hljs-title">Page</span>
    {
        <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Page_Load</span>(<span class="hljs-params"><span class="hljs-keyword">object</span> sender, EventArgs e</span>)</span>
        {
            <span class="hljs-comment">// Implement template loading and rendering logic here</span>
        }
    }
}</code></span></pre>


<p class="wp-block-paragraph">Insert the following code inside Page_Load</p>


<pre class="wp-block-code"><span><code class="hljs language-cs"><span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Page_Load</span>(<span class="hljs-params"><span class="hljs-keyword">object</span> sender, EventArgs e</span>)</span>
{
    <span class="hljs-comment">// Step 1: Instantiate VelocityEngine</span>
    VelocityEngine ve = <span class="hljs-keyword">new</span> VelocityEngine();

    <span class="hljs-comment">// Step 2: Configure engine settings</span>
    ExtendedProperties pros = <span class="hljs-keyword">new</span> ExtendedProperties();
    pros.AddProperty(RuntimeConstants.RESOURCE_LOADER, <span class="hljs-string">"file"</span>); <span class="hljs-comment">// Load templates from local files</span>
    pros.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Server.MapPath(<span class="hljs-string">@""</span>)); <span class="hljs-comment">// Template root directory</span>
    ve.Init(pros); <span class="hljs-comment">// Initialize engine with configuration</span>

    <span class="hljs-comment">// Step 3: Load template file</span>
    Template template = ve.GetTemplate(<span class="hljs-string">"themes/default/register.htm"</span>);

    <span class="hljs-comment">// Step 4: Create context and pass variables to template</span>
    IContext context = <span class="hljs-keyword">new</span> VelocityContext();
    context.Put(<span class="hljs-string">"websiteName"</span>, <span class="hljs-string">"FoxDevelop"</span>);
    context.Put(<span class="hljs-string">"domainName"</span>, <span class="hljs-string">"foxdevelop.com"</span>);

    <span class="hljs-comment">// Step 5: Merge template with data and render HTML</span>
    StringWriter writer = <span class="hljs-keyword">new</span> StringWriter();
    template.Merge(context, writer); <span class="hljs-comment">// Store rendered output</span>

    <span class="hljs-comment">// Step 6: Send output to browser</span>
    Response.Write(writer.ToString().Replace(<span class="hljs-string">"\r\n"</span>, <span class="hljs-string">"&lt;br/&gt;"</span>));
}</code></span></pre>


<p class="wp-block-paragraph">This code reads the HTML template file, defines variables and replaces placeholder markers inside the template.</p>



<ol class="wp-block-list">
<li><strong>Instantiate VelocityEngine</strong> Creates an isolated engine instance, unlike the static global <code>Velocity.Init()</code>, allowing separate configurations.</li>



<li><strong>Engine initialization and configuration</strong></li>
</ol>



<ul class="wp-block-list">
<li><code>RESOURCE_LOADER=file</code> : Specifies local filesystem template loading;</li>



<li><code>FILE_RESOURCE_LOADER_PATH</code> : Sets root path for template lookup;</li>



<li><code>ve.Init(pros)</code> : Applies settings and completes initialization.</li>
</ul>



<ol start="3" class="wp-block-list">
<li><strong>Template loading via <code>GetTemplate()</code></strong> Reads <code>themes/default/register.htm</code> relative to the configured root path.</li>



<li><strong>VelocityContext data context</strong><code>context.Put(key, value)</code> passes C# variables into templates, accessible in htm files using <code>$websiteName</code>, <code>$domainName</code>.</li>



<li><strong>Template Merge rendering</strong><code>template.Merge(context, stream)</code> injects variables into templates to build full HTML strings. <code>StringWriter</code> captures rendered text output.</li>



<li><strong>Response output</strong> Sends final HTML to browsers; <code>Replace("\r\n","&lt;br/&gt;")</code> converts source line breaks into HTML break tags.</li>
</ol>



<h4 class="wp-block-heading">htm Template</h4>



<p class="wp-block-paragraph">Modify the <code>register.htm</code> file within your theme directory</p>


<pre class="wp-block-code"><span><code class="hljs language-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span> <span class="hljs-meta-keyword">PUBLIC</span> <span class="hljs-meta-string">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span class="hljs-meta-string">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.w3.org/1999/xhtml"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    Website：$websiteName <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
    Domain：$domainName
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></code></span></pre>


<p class="wp-block-paragraph">These two <code>$xxxxxxx</code> markers correspond to variables defined in Page_Load and will be substituted with actual values during rendering.</p>



<ol class="wp-block-list">
<li><strong>Variable syntax <code>$VariableName</code></strong> <code>$websiteName</code> and <code>$domainName</code> follow NVelocity (VTL) variable syntax. Values assigned with <code>context.Put("websiteName", "FoxDevelop")</code> get replaced automatically by the engine:</li>
</ol>



<ul class="wp-block-list">
<li><code>$websiteName</code> → <code>FoxDevelop</code></li>



<li><code>$domainName</code> → <code>foxdevelop.com</code></li>
</ul>



<p class="wp-block-paragraph">Runtime Output</p>


<pre class="wp-block-code"><span><code class="hljs language-css"><span class="hljs-selector-tag">Website</span>：<span class="hljs-selector-tag">FoxDevelop</span>
<span class="hljs-selector-tag">Domain</span>：<span class="hljs-selector-tag">foxdevelop</span><span class="hljs-selector-class">.com</span></code></span></pre>


<p class="wp-block-paragraph">Introduction to NVelocity</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.foxdevelop.com/2026/08/01/introduction-to-nvelocity/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
