<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: template code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 17:22:07 GMT</pubDate>
    <description>DZone Snippets: template code</description>
    <item>
      <title>Write your own XSLT functions</title>
      <link>http://snippets.dzone.com/posts/show/5863</link>
      <description>Source: &lt;a href="http://www.xml.com/pub/a/2003/09/03/trxml.html"&gt;XML.com: Writing Your Own Functions in XSLT 2.0&lt;/a&gt; [xml.com]&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;xsl:stylesheet version="2.0" &lt;br /&gt;  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&lt;br /&gt;  xmlns:foo="http://whatever"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;!-- Compare two strings ignoring case, returning same&lt;br /&gt;       values as compare(). --&gt;&lt;br /&gt;  &lt;xsl:function name="foo:compareCI"&gt;&lt;br /&gt;    &lt;xsl:param name="string1"/&gt;&lt;br /&gt;    &lt;xsl:param name="string2"/&gt;&lt;br /&gt;    &lt;xsl:value-of select="compare(upper-case($string1),upper-case($string2))"/&gt;&lt;br /&gt;  &lt;/xsl:function&gt;&lt;br /&gt;&lt;br /&gt;  &lt;xsl:template match="/"&gt;&lt;br /&gt;compareCI red,blue: &lt;xsl:value-of select="foo:compareCI('red','blue')"/&gt;&lt;br /&gt;compareCI red,red: &lt;xsl:value-of select="foo:compareCI('red','red')"/&gt;&lt;br /&gt;compareCI red,Red: &lt;xsl:value-of select="foo:compareCI('red','Red')"/&gt;&lt;br /&gt;compareCI red,Yellow: &lt;xsl:value-of select="foo:compareCI('red','Yellow')"/&gt;&lt;br /&gt;  &lt;/xsl:template&gt;&lt;br /&gt;&lt;br /&gt;&lt;/xsl:stylesheet&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 31 Jul 2008 21:58:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5863</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Replace text template for parameter hash table</title>
      <link>http://snippets.dzone.com/posts/show/5518</link>
      <description>/*&lt;br /&gt;&lt;br /&gt;Replace text template for parameter hash table&lt;br /&gt;&lt;br /&gt;Ex:&lt;br /&gt;$mytext = 'Hi, my name is {%name%}, and my address is {%address%}';&lt;br /&gt;rep_templates($mytext, array('name'=&gt;'Steven', 'address'=&gt;'Rua Beira Mar, 12'));&lt;br /&gt;print $text; //Output: Hi, my name is Steven, and my address is Rua Beira Mar, 12&lt;br /&gt;&lt;br /&gt;Other ex.&lt;br /&gt;&lt;br /&gt;$row = mysql_fetch_assoc($my_result_from_query);&lt;br /&gt;&lt;br /&gt;$mytext = file_get_contents('./text_for_email_template.txt');&lt;br /&gt;&lt;br /&gt;tranf_dados($mytext, $row);&lt;br /&gt;&lt;br /&gt;sendmail($row['email', 'Subject:Hi mane!', $mytext);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;*/&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function rep_templates(&amp;$t, $d){&lt;br /&gt;    preg_match_all ( '/{\%(\w*)\%\}/' , $t , $matches );&lt;br /&gt;    foreach($matches[1] as $m){&lt;br /&gt;        if($d[$m]!=null){&lt;br /&gt;            $pattern = "/{\%".$m."\%\}/";&lt;br /&gt;            $t = preg_replace( $pattern, $d[$m], $t);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 19 May 2008 12:43:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5518</guid>
      <author>stvkoch (Steven Koch)</author>
    </item>
    <item>
      <title>Java Cons class</title>
      <link>http://snippets.dzone.com/posts/show/5419</link>
      <description>Very simple Cons class for Java&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public class Cons&lt;T,U&gt;&lt;br /&gt;{&lt;br /&gt;    private T car;&lt;br /&gt;    private U cdr;&lt;br /&gt;&lt;br /&gt;    public Cons( T carArg, U cdrArg )&lt;br /&gt;    {&lt;br /&gt;        car = carArg;&lt;br /&gt;        cdr = cdrArg;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public T getCar(){ return car; }&lt;br /&gt;    public U getCdr(){ return cdr; }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 21 Apr 2008 23:43:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5419</guid>
      <author>frost137 (Douglas Wyatt)</author>
    </item>
    <item>
      <title>Passing an XSL param from one template to another</title>
      <link>http://snippets.dzone.com/posts/show/5387</link>
      <description>&lt;code&gt;&lt;br /&gt;&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;&lt;br /&gt;&lt;xsl:stylesheet version="1.0"&lt;br /&gt;xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;&lt;br /&gt;&lt;br /&gt;&lt;xsl:variable name="xx"&gt;&lt;br /&gt;  &lt;html&gt;&lt;br /&gt;  &lt;body&gt;&lt;br /&gt;  &lt;xsl:call-template name="show_title"&gt;&lt;br /&gt;    &lt;xsl:with-param name="title" /&gt;&lt;br /&gt;  &lt;/xsl:call-template&gt;&lt;br /&gt;  &lt;/body&gt;&lt;br /&gt;  &lt;/html&gt;&lt;br /&gt;&lt;/xsl:variable&gt;&lt;br /&gt;&lt;br /&gt;&lt;xsl:template name="show_title" match="/"&gt;&lt;br /&gt;  &lt;xsl:param name="title" /&gt;&lt;br /&gt;  &lt;xsl:for-each select="catalog/cd"&gt;&lt;br /&gt;    &lt;p&gt;Title: &lt;xsl:value-of select="$title" /&gt;&lt;/p&gt;&lt;br /&gt;  &lt;/xsl:for-each&gt;&lt;br /&gt;&lt;/xsl:template&gt;&lt;br /&gt;&lt;br /&gt;&lt;/xsl:stylesheet&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;source: &lt;a href="http://www.w3schools.com/XSL/el_param.asp"&gt;XSLT &lt;xsl:param&gt; Element&lt;/a&gt; [w3schools.com]</description>
      <pubDate>Fri, 18 Apr 2008 15:00:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5387</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Trim Template for XSLT</title>
      <link>http://snippets.dzone.com/posts/show/4032</link>
      <description>Common Trim function for XSLT (as a template)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;xsl:template name="left-trim"&gt;&lt;br /&gt;  &lt;xsl:param name="s" /&gt;&lt;br /&gt;  &lt;xsl:choose&gt;&lt;br /&gt;    &lt;xsl:when test="substring($s, 1, 1) = ''"&gt;&lt;br /&gt;      &lt;xsl:value-of select="$s"/&gt;&lt;br /&gt;    &lt;/xsl:when&gt;&lt;br /&gt;    &lt;xsl:when test="normalize-space(substring($s, 1, 1)) = ''"&gt;&lt;br /&gt;      &lt;xsl:call-template name="left-trim"&gt;&lt;br /&gt;        &lt;xsl:with-param name="s" select="substring($s, 2)" /&gt;&lt;br /&gt;      &lt;/xsl:call-template&gt;&lt;br /&gt;    &lt;/xsl:when&gt;&lt;br /&gt;    &lt;xsl:otherwise&gt;&lt;br /&gt;      &lt;xsl:value-of select="$s" /&gt;&lt;br /&gt;    &lt;/xsl:otherwise&gt;&lt;br /&gt;  &lt;/xsl:choose&gt;&lt;br /&gt;&lt;/xsl:template&gt;&lt;br /&gt;&lt;br /&gt;&lt;xsl:template name="right-trim"&gt;&lt;br /&gt;  &lt;xsl:param name="s" /&gt;&lt;br /&gt;  &lt;xsl:choose&gt;&lt;br /&gt;    &lt;xsl:when test="substring($s, 1, 1) = ''"&gt;&lt;br /&gt;      &lt;xsl:value-of select="$s"/&gt;&lt;br /&gt;    &lt;/xsl:when&gt;&lt;br /&gt;    &lt;xsl:when test="normalize-space(substring($s, string-length($s))) = ''"&gt;&lt;br /&gt;      &lt;xsl:call-template name="right-trim"&gt;&lt;br /&gt;        &lt;xsl:with-param name="s" select="substring($s, 1, string-length($s) - 1)" /&gt;&lt;br /&gt;      &lt;/xsl:call-template&gt;&lt;br /&gt;    &lt;/xsl:when&gt;&lt;br /&gt;    &lt;xsl:otherwise&gt;&lt;br /&gt;      &lt;xsl:value-of select="$s" /&gt;&lt;br /&gt;    &lt;/xsl:otherwise&gt;&lt;br /&gt;  &lt;/xsl:choose&gt;&lt;br /&gt;&lt;/xsl:template&gt;&lt;br /&gt;&lt;br /&gt;&lt;xsl:template name="trim"&gt;&lt;br /&gt;  &lt;xsl:param name="s" /&gt;&lt;br /&gt;  &lt;xsl:call-template name="right-trim"&gt;&lt;br /&gt;    &lt;xsl:with-param name="s"&gt;&lt;br /&gt;      &lt;xsl:call-template name="left-trim"&gt;&lt;br /&gt;        &lt;xsl:with-param name="s" select="$s" /&gt;&lt;br /&gt;      &lt;/xsl:call-template&gt;&lt;br /&gt;    &lt;/xsl:with-param&gt;&lt;br /&gt;  &lt;/xsl:call-template&gt;&lt;br /&gt;&lt;/xsl:template&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 May 2007 00:35:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4032</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Exception Assertion Template For Eclipse</title>
      <link>http://snippets.dzone.com/posts/show/3673</link>
      <description>Eclipse template for exception assertion (Create template in Eclipse &gt; Preferences &gt; Java &gt; Editor &gt; Templates &gt; New ... OK)&lt;br /&gt;&lt;br /&gt;How to use: &lt;br /&gt;1. Select code throwing exception&lt;br /&gt;2. Alt + Shift + Z -&gt; select corresponding template.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;        try&lt;br /&gt;        {&lt;br /&gt;            ${line_selection}${cursor}&lt;br /&gt;            fail("${Exception} expected.");&lt;br /&gt;        }&lt;br /&gt;        catch (${Exception} e)&lt;br /&gt;        {&lt;br /&gt;            assertEquals(${message}, e.getMessage());&lt;br /&gt;        }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 15 Mar 2007 12:00:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3673</guid>
      <author>guanzo (guanzo)</author>
    </item>
    <item>
      <title>Java Singleton Template for Eclipse</title>
      <link>http://snippets.dzone.com/posts/show/3618</link>
      <description>This is a template for easily creating an implementation of the Singleton Pattern on Eclipse. Open Window-&gt;Preferences-&gt;Java-&gt;Editor-&gt;Templates click on New and insert the code below on the pattern text area; add a name (I suggest "singleton") - whenever you type this name and press Ctrl+Space the code will be inserted in your class - and you're good to go.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;private static ${enclosing_type} instance;&lt;br /&gt;&lt;br /&gt;private ${enclosing_type}(){}&lt;br /&gt;&lt;br /&gt;public static ${enclosing_type} getInstance(){&lt;br /&gt;	if(null == instance){&lt;br /&gt;		instance = new ${enclosing_type}();&lt;br /&gt;	}&lt;br /&gt;	return instance;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Mar 2007 19:42:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3618</guid>
      <author>marcelotmelo (Marcelo Melo)</author>
    </item>
    <item>
      <title>Ruby style string injection via Prototype Templates</title>
      <link>http://snippets.dzone.com/posts/show/3386</link>
      <description>Requires Prototype 1.5&lt;br /&gt;&lt;br /&gt;I'm honestly not sure why they didn't do this out of the box.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Object.extend(String.prototype, {&lt;br /&gt;  mixin: function(obj) {&lt;br /&gt;    return new Template(this).evaluate(obj);&lt;br /&gt;  }&lt;br /&gt;});&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Oh, yes.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;"#{company} forever!".mixin({company: 'Unspace'}) // Unspace forever!&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 30 Jan 2007 03:07:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3386</guid>
      <author>leftist (Pete Forde)</author>
    </item>
    <item>
      <title>Simplest possible PHP templating engine</title>
      <link>http://snippets.dzone.com/posts/show/3052</link>
      <description>// This code takes a template name and an array of variables&lt;br /&gt;// and parses them with a file&lt;br /&gt;// &lt;br /&gt;// Example:&lt;br /&gt;//   print template('hello', array('who'=&gt;'world'));&lt;br /&gt;//&lt;br /&gt;// Template (/templates/hello.html)&lt;br /&gt;//   Hello &lt;?=$who?&gt;!&lt;br /&gt;// &lt;br /&gt;// Outputs:&lt;br /&gt;//   Hello world!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;define('DIR_TEMPLATES', dirname(__FILE__).'/templates');&lt;br /&gt;&lt;br /&gt;function template($__name__, $__data__=array()) {&lt;br /&gt;	extract($__data__);&lt;br /&gt;	ob_start();&lt;br /&gt;	require(DIR_TEMPLATES.'/'.$__name__.'.html');&lt;br /&gt;	return ob_get_clean();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 28 Nov 2006 18:55:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3052</guid>
      <author>tatarynowicz (Michal Tatarynowicz)</author>
    </item>
    <item>
      <title>Running erb templates from the command line</title>
      <link>http://snippets.dzone.com/posts/show/1723</link>
      <description>The following script takes the name of a template as it's argument. Within the&lt;br /&gt;template the command &lt;b&gt;erb&lt;/b&gt; is in scope to call more templates. I wrote this for a friend who was having hell with NVU templates and I suggested to write the code by hand and use this script to build his static pages.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;# Quick and dirty template processing script. It takes&lt;br /&gt;# as an argument the name of the first template script&lt;br /&gt;# and then executes it to standard output.&lt;br /&gt;&lt;br /&gt;require "erb"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class QuickTemplate&lt;br /&gt;   attr_reader :args, :text&lt;br /&gt;   def initialize(file)&lt;br /&gt;      @text = File.read(file)&lt;br /&gt;   end&lt;br /&gt;   def exec(args={})&lt;br /&gt;      b = binding&lt;br /&gt;      template = ERB.new(@text, 0, "%&lt;&gt;")&lt;br /&gt;      result = template.result(b)&lt;br /&gt;      # Chomp the trailing newline&lt;br /&gt;      result.gsub(/\n$/,'')&lt;br /&gt;   end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;def erb(file, args={})&lt;br /&gt;   QuickTemplate.new(file).exec(args)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;puts erb(ARGV[0])&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The erb command itself takes an optional argument of a hash which is passed to the template as the&lt;br /&gt;variable name &lt;b&gt;args&lt;/b&gt;. Thus you can parameterize your sub templates. &lt;br /&gt;&lt;br /&gt;Call the command as&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;ruby erb_run.rb main.thtml&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The main template&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt;   &lt;head&gt;&lt;br /&gt;   &lt;/head&gt;&lt;br /&gt;   &lt;div&gt;&lt;br /&gt;    &lt;%= erb("title.thtml") %&gt;&lt;br /&gt;   &lt;/div&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;and the sub template &lt;b&gt;title.thtml&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;title&gt; This is Alex's cool restraunt&lt;/title&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 21 Mar 2006 00:30:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1723</guid>
      <author>bradphelan (Brad Phelan)</author>
    </item>
  </channel>
</rss>
