<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: erb code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 06:06:12 GMT</pubDate>
    <description>DZone Snippets: erb code</description>
    <item>
      <title>Converting all ERb views to Haml</title>
      <link>http://snippets.dzone.com/posts/show/5449</link>
      <description>A little script to convert all your .erb views to .haml using html2haml, which is included with Haml installation.&lt;br /&gt;Just drop this in your rails root folder and run it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class ToHaml&lt;br /&gt;  def initialize(path)&lt;br /&gt;    @path = path&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def convert!&lt;br /&gt;    Dir["#{@path}/**/*.erb"].each do |file|&lt;br /&gt;      `html2haml -rx #{file} #{file.gsub(/\.erb$/, '.haml')}`&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;path = File.join(File.dirname(__FILE__), 'app', 'views')&lt;br /&gt;ToHaml.new(path).convert!&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 01 May 2008 23:27:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5449</guid>
      <author>divoxx (Rodrigo Kochenburger)</author>
    </item>
    <item>
      <title>Convert .rhtml templates to .html.erb</title>
      <link>http://snippets.dzone.com/posts/show/4640</link>
      <description>// run this shell command from within the dir you want to change. You will need to change the hg line to use which ever version control you are using, or if you're not use version control (naughty!) then leave out the hg letters altogether.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;for f in $(find . -name '*.rhtml') ; do c=$(dirname $f)/$(basename $f .rhtml).html.erb ; hg mv $f $c ; done&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 12 Oct 2007 08:58:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4640</guid>
      <author>whomwah (Duncan Robertson)</author>
    </item>
    <item>
      <title>Migrate rhtml views into erb views within subversion repository</title>
      <link>http://snippets.dzone.com/posts/show/3624</link>
      <description>From &lt;a href="http://opensoul.org/2007/3/4/renaming-rhtml-to-erb-using-ruby"&gt;Brandon Keepers&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;To migrate rhtml views into erb views within subversion repository:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Dir.glob('app/views/**/*.rhtml').each do |file|&lt;br /&gt;  puts `svn mv #{file} #{file.gsub(/\.rhtml$/, '.erb')}`&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 04 Mar 2007 07:57:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3624</guid>
      <author>nicwilliams (Dr Nic Williams)</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>
    <item>
      <title>Iterating over a list</title>
      <link>http://snippets.dzone.com/posts/show/355</link>
      <description>One of the common things you want to do with a table in a web application is use different classes so the rows have different styles.  i.e alternate between light and dark backgrounds.&lt;br /&gt;&lt;br /&gt;Arry#cycle is a method to enable that.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  def cycle(values)&lt;br /&gt;    self.each_with_index do |o, i| &lt;br /&gt;      yield(o, values[i % values.length])&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You can use it like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% @something.cycle(["oddRow", "evenRow"]) do |obj, cssClass| %&gt;&lt;br /&gt;  &lt;tr class="&lt;%= cssClass %&gt;"&gt;&lt;br /&gt;    &lt;td&gt;&lt;%= obj.something %&gt;&lt;/td&gt;&lt;br /&gt;    &lt;td&gt;&lt;%= obj.something_else %&gt;&lt;/td&gt;&lt;br /&gt;  &lt;/tr&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 31 May 2005 18:31:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/355</guid>
      <author>koz (Michael Koziarski)</author>
    </item>
  </channel>
</rss>
