Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 17 total  RSS 

Write your own XSLT functions

Source: XML.com: Writing Your Own Functions in XSLT 2.0 [xml.com]

   1  
   2  <xsl:stylesheet version="2.0" 
   3    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   4    xmlns:foo="http://whatever">
   5  
   6    <!-- Compare two strings ignoring case, returning same
   7         values as compare(). -->
   8    <xsl:function name="foo:compareCI">
   9      <xsl:param name="string1"/>
  10      <xsl:param name="string2"/>
  11      <xsl:value-of select="compare(upper-case($string1),upper-case($string2))"/>
  12    </xsl:function>
  13  
  14    <xsl:template match="/">
  15  compareCI red,blue: <xsl:value-of select="foo:compareCI('red','blue')"/>
  16  compareCI red,red: <xsl:value-of select="foo:compareCI('red','red')"/>
  17  compareCI red,Red: <xsl:value-of select="foo:compareCI('red','Red')"/>
  18  compareCI red,Yellow: <xsl:value-of select="foo:compareCI('red','Yellow')"/>
  19    </xsl:template>
  20  
  21  </xsl:stylesheet>

Replace text template for parameter hash table

/*

Replace text template for parameter hash table

Ex:
$mytext = 'Hi, my name is {%name%}, and my address is {%address%}';
rep_templates($mytext, array('name'=>'Steven', 'address'=>'Rua Beira Mar, 12'));
print $text; //Output: Hi, my name is Steven, and my address is Rua Beira Mar, 12

Other ex.

$row = mysql_fetch_assoc($my_result_from_query);

$mytext = file_get_contents('./text_for_email_template.txt');

tranf_dados($mytext, $row);

sendmail($row['email', 'Subject:Hi mane!', $mytext);


*/
   1  
   2  function rep_templates(&$t, $d){
   3      preg_match_all ( '/{\%(\w*)\%\}/' , $t , $matches );
   4      foreach($matches[1] as $m){
   5          if($d[$m]!=null){
   6              $pattern = "/{\%".$m."\%\}/";
   7              $t = preg_replace( $pattern, $d[$m], $t);
   8          }
   9      }
  10  }

Java Cons class

Very simple Cons class for Java

   1  
   2  public class Cons<T,U>
   3  {
   4      private T car;
   5      private U cdr;
   6  
   7      public Cons( T carArg, U cdrArg )
   8      {
   9          car = carArg;
  10          cdr = cdrArg;
  11      }
  12  
  13      public T getCar(){ return car; }
  14      public U getCdr(){ return cdr; }
  15  }

Passing an XSL param from one template to another

   1  
   2  <?xml version="1.0" encoding="ISO-8859-1"?>
   3  <xsl:stylesheet version="1.0"
   4  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   5  
   6  <xsl:variable name="xx">
   7    <html>
   8    <body>
   9    <xsl:call-template name="show_title">
  10      <xsl:with-param name="title" />
  11    </xsl:call-template>
  12    </body>
  13    </html>
  14  </xsl:variable>
  15  
  16  <xsl:template name="show_title" match="/">
  17    <xsl:param name="title" />
  18    <xsl:for-each select="catalog/cd">
  19      <p>Title: <xsl:value-of select="$title" /></p>
  20    </xsl:for-each>
  21  </xsl:template>
  22  
  23  </xsl:stylesheet>

source: XSLT <xsl:param> Element [w3schools.com]

Trim Template for XSLT

Common Trim function for XSLT (as a template)

   1  
   2  <xsl:template name="left-trim">
   3    <xsl:param name="s" />
   4    <xsl:choose>
   5      <xsl:when test="substring($s, 1, 1) = ''">
   6        <xsl:value-of select="$s"/>
   7      </xsl:when>
   8      <xsl:when test="normalize-space(substring($s, 1, 1)) = ''">
   9        <xsl:call-template name="left-trim">
  10          <xsl:with-param name="s" select="substring($s, 2)" />
  11        </xsl:call-template>
  12      </xsl:when>
  13      <xsl:otherwise>
  14        <xsl:value-of select="$s" />
  15      </xsl:otherwise>
  16    </xsl:choose>
  17  </xsl:template>
  18  
  19  <xsl:template name="right-trim">
  20    <xsl:param name="s" />
  21    <xsl:choose>
  22      <xsl:when test="substring($s, 1, 1) = ''">
  23        <xsl:value-of select="$s"/>
  24      </xsl:when>
  25      <xsl:when test="normalize-space(substring($s, string-length($s))) = ''">
  26        <xsl:call-template name="right-trim">
  27          <xsl:with-param name="s" select="substring($s, 1, string-length($s) - 1)" />
  28        </xsl:call-template>
  29      </xsl:when>
  30      <xsl:otherwise>
  31        <xsl:value-of select="$s" />
  32      </xsl:otherwise>
  33    </xsl:choose>
  34  </xsl:template>
  35  
  36  <xsl:template name="trim">
  37    <xsl:param name="s" />
  38    <xsl:call-template name="right-trim">
  39      <xsl:with-param name="s">
  40        <xsl:call-template name="left-trim">
  41          <xsl:with-param name="s" select="$s" />
  42        </xsl:call-template>
  43      </xsl:with-param>
  44    </xsl:call-template>
  45  </xsl:template>

Exception Assertion Template For Eclipse

Eclipse template for exception assertion (Create template in Eclipse > Preferences > Java > Editor > Templates > New ... OK)

How to use:
1. Select code throwing exception
2. Alt + Shift + Z -> select corresponding template.

   1  
   2          try
   3          {
   4              ${line_selection}${cursor}
   5              fail("${Exception} expected.");
   6          }
   7          catch (${Exception} e)
   8          {
   9              assertEquals(${message}, e.getMessage());
  10          }

Java Singleton Template for Eclipse

This is a template for easily creating an implementation of the Singleton Pattern on Eclipse. Open Window->Preferences->Java->Editor->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.

   1  
   2  private static ${enclosing_type} instance;
   3  
   4  private ${enclosing_type}(){}
   5  
   6  public static ${enclosing_type} getInstance(){
   7  	if(null == instance){
   8  		instance = new ${enclosing_type}();
   9  	}
  10  	return instance;
  11  }

Ruby style string injection via Prototype Templates

Requires Prototype 1.5

I'm honestly not sure why they didn't do this out of the box.

   1  
   2  Object.extend(String.prototype, {
   3    mixin: function(obj) {
   4      return new Template(this).evaluate(obj);
   5    }
   6  });


Oh, yes.

   1  
   2  "#{company} forever!".mixin({company: 'Unspace'}) // Unspace forever!

Simplest possible PHP templating engine

// This code takes a template name and an array of variables
// and parses them with a file
//
// Example:
// print template('hello', array('who'=>'world'));
//
// Template (/templates/hello.html)
// Hello <?=$who?>!
//
// Outputs:
// Hello world!

   1  
   2  define('DIR_TEMPLATES', dirname(__FILE__).'/templates');
   3  
   4  function template($__name__, $__data__=array()) {
   5  	extract($__data__);
   6  	ob_start();
   7  	require(DIR_TEMPLATES.'/'.$__name__.'.html');
   8  	return ob_get_clean();
   9  }

Running erb templates from the command line

The following script takes the name of a template as it's argument. Within the
template the command erb 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.

   1  
   2  #!/usr/bin/ruby
   3  # Quick and dirty template processing script. It takes
   4  # as an argument the name of the first template script
   5  # and then executes it to standard output.
   6  
   7  require "erb"
   8  
   9  
  10  class QuickTemplate
  11     attr_reader :args, :text
  12     def initialize(file)
  13        @text = File.read(file)
  14     end
  15     def exec(args={})
  16        b = binding
  17        template = ERB.new(@text, 0, "%<>")
  18        result = template.result(b)
  19        # Chomp the trailing newline
  20        result.gsub(/\n$/,'')
  21     end
  22  end
  23  
  24  def erb(file, args={})
  25     QuickTemplate.new(file).exec(args)
  26  end
  27  
  28  puts erb(ARGV[0])


The erb command itself takes an optional argument of a hash which is passed to the template as the
variable name args. Thus you can parameterize your sub templates.

Call the command as

   1  
   2  ruby erb_run.rb main.thtml


The main template

   1  
   2  <html>
   3     <head>
   4     </head>
   5     <div>
   6      <%= erb("title.thtml") %>
   7     </div>
   8  </html>


and the sub template title.thtml

   1  
   2  <title> This is Alex's cool restraunt</title>

« Newer Snippets
Older Snippets »
Showing 1-10 of 17 total  RSS