<?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>dev.enekoalonso.com &#187; freemarker</title>
	<atom:link href="http://dev.enekoalonso.com/tag/freemarker/feed/" rel="self" type="application/rss+xml" />
	<link>http://dev.enekoalonso.com</link>
	<description>having fun with code</description>
	<lastBuildDate>Wed, 12 Oct 2011 21:40:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Equivalent of parseInt() in Freemarker</title>
		<link>http://dev.enekoalonso.com/2010/07/02/equivalent-of-parseint-in-freemarker/</link>
		<comments>http://dev.enekoalonso.com/2010/07/02/equivalent-of-parseint-in-freemarker/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 19:05:40 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[cast]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[freemarker]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[parseInt]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=604</guid>
		<description><![CDATA[Programming languages have methods to cast integers to strings and strings to integers, but usually is had to find equivalents to parseInt(), a Javascript function that basically removes all non-numeric characters from a string and returns the resulting integer. Today, while working on a Freemarker template, I had to convert strings like &#8220;40 ms.&#8221; and [...]]]></description>
			<content:encoded><![CDATA[<p>Programming languages have methods to cast integers to strings and strings to integers, but usually is had to find equivalents to parseInt(), a Javascript function that basically removes all non-numeric characters from a string and returns the resulting integer.</p>
<p>Today, while working on a Freemarker template, I had to convert strings like &#8220;40 ms.&#8221; and &#8220;128 px&#8221; to their respective integers 40 and 128. The solution I found is to use regular expressions, which are kind of tricky in Freemarker but can save you lots of time.</p>
<p>Example in Javascript:</p>
<div class="geshi no javascript">
<ol>
<li class="li1">
<div class="de1">console.<span class="me1">log</span><span class="br0">&#40;</span>parseInt<span class="br0">&#40;</span><span class="st0">&quot;40 ms&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="co1">// outputs 40</span></div>
</li>
<li class="li1">
<div class="de1">console.<span class="me1">log</span><span class="br0">&#40;</span><span class="st0">&quot;40 ms&quot;</span>.<span class="me1">match</span><span class="br0">&#40;</span><span class="re0">/^\d+/</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="co1">// outputs 40</span></div>
</li>
</ol>
</div>
<p>Same in Freemarker:</p>
<div class="geshi no text">
<ol>
<li class="li1">
<div class="de1">&lt;#assign myInt = &quot;40 ms.&quot;?matches(&quot;^(\\d+)(.*)&quot;)[0]?groups[1]&gt;</div>
</li>
</ol>
</div>
<p>Looks like even using regular expressions, in Freemarker we need to <a href="http://freemarker.org/docs/ref_builtins_string.html#ref_builtin_matches">match the whole string</a> with groups to separate the number from the rest. Thus, this will work only if the number is at the beginning of the string, which was the initial requirement in my case.</p>
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>July 20, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/20/little-tricks-string-padding-in-javascript/" title="Little tricks: string padding in Javascript">Little tricks: string padding in Javascript</a> (3)</li><li>September 21, 2010 -- <a href="http://dev.enekoalonso.com/2010/09/21/date-from-iso-8601-string/" title="Date from ISO 8601 string">Date from ISO 8601 string</a> (1)</li><li>August 18, 2010 -- <a href="http://dev.enekoalonso.com/2010/08/18/little-tricks-repeating-strings-in-javascript-python/" title="Little tricks: repeating strings in Javascript &#038; Python">Little tricks: repeating strings in Javascript &#038; Python</a> (1)</li><li>July 1, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/01/little-tricks-editing-strings-by-index-in-javascript-and-python/" title="Little tricks: editing strings by index in Javascript and Python">Little tricks: editing strings by index in Javascript and Python</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2010/07/02/equivalent-of-parseint-in-freemarker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Regular expressions in Freemarker</title>
		<link>http://dev.enekoalonso.com/2009/04/03/regular-expressions-in-freemarker/</link>
		<comments>http://dev.enekoalonso.com/2009/04/03/regular-expressions-in-freemarker/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 21:49:00 +0000</pubDate>
		<dc:creator>Eneko Alonso</dc:creator>
				<category><![CDATA[uncategorized]]></category>
		<category><![CDATA[expressions]]></category>
		<category><![CDATA[freemarker]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[regular]]></category>
		<category><![CDATA[switch]]></category>

		<guid isPermaLink="false">http://dev.enekoalonso.com/?p=293</guid>
		<description><![CDATA[At work we have our custom CMS (SWITCH) which uses Freemarker to create template-based pages. So far I had never needed this, but today I had to figure out how to strip all html tags from a text block. Well, nothing like a good RegEx for that :) Trimming an HTML block to 100 characters [...]]]></description>
			<content:encoded><![CDATA[<p>At work we have our custom CMS (SWITCH) which uses Freemarker to create template-based pages. So far I had never needed this, but today I had to figure out how to strip all html tags from a text block. Well, nothing like a good RegEx for that :)</p>
<p>Trimming an HTML block to 100 characters and stripping all the html tags:</p>
<div class="geshi no freemarker">
<ol>
<li class="li1">
<div class="de1">&lt;p class=&quot;bio&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp; ${Text?replace(&quot;&lt;/?[^&gt;]+(&gt;|$)&quot;, &quot;&quot;, &quot;r&quot;)?substring(0,100)}&#8230;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/p&gt;</div>
</li>
</ol>
</div>
<p><a href="http://freemarker.sourceforge.net/docs/ref_builtins_string.html#ref_builtin_string_flags">RegEx in Freemarker</a> differ a little bit from RegEx in Javascript, so be aware of that :) Freemarker uses <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html">Java 1.4 RegEx syntax</a>.</p>
<p>Enjoy!<br />
<h3  class="related_post_title">Related Posts:</h3><ul class="related_post"><li>July 2, 2010 -- <a href="http://dev.enekoalonso.com/2010/07/02/equivalent-of-parseint-in-freemarker/" title="Equivalent of parseInt() in Freemarker">Equivalent of parseInt() in Freemarker</a> (0)</li><li>March 15, 2010 -- <a href="http://dev.enekoalonso.com/2010/03/15/more-updates-to-mooml-coming-soon/" title="More updates to Mooml coming soon">More updates to Mooml coming soon</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://dev.enekoalonso.com/2009/04/03/regular-expressions-in-freemarker/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

