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-5 of 5 total  RSS 

Converting all ERb views to Haml

A little script to convert all your .erb views to .haml using html2haml, which is included with Haml installation.
Just drop this in your rails root folder and run it.

class ToHaml
  def initialize(path)
    @path = path
  end
  
  def convert!
    Dir["#{@path}/**/*.erb"].each do |file|
      `html2haml -rx #{file} #{file.gsub(/\.erb$/, '.haml')}`
    end
  end
end

path = File.join(File.dirname(__FILE__), 'app', 'views')
ToHaml.new(path).convert!

Convert .rhtml templates to .html.erb

// 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.

for f in $(find . -name '*.rhtml') ; do c=$(dirname $f)/$(basename $f .rhtml).html.erb ; hg mv $f $c ; done

Migrate rhtml views into erb views within subversion repository

From Brandon Keepers:

To migrate rhtml views into erb views within subversion repository:

Dir.glob('app/views/**/*.rhtml').each do |file|
  puts `svn mv #{file} #{file.gsub(/\.rhtml$/, '.erb')}`
end

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.

#!/usr/bin/ruby
# Quick and dirty template processing script. It takes
# as an argument the name of the first template script
# and then executes it to standard output.

require "erb"


class QuickTemplate
   attr_reader :args, :text
   def initialize(file)
      @text = File.read(file)
   end
   def exec(args={})
      b = binding
      template = ERB.new(@text, 0, "%<>")
      result = template.result(b)
      # Chomp the trailing newline
      result.gsub(/\n$/,'')
   end
end

def erb(file, args={})
   QuickTemplate.new(file).exec(args)
end

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

ruby erb_run.rb main.thtml


The main template

<html>
   <head>
   </head>
   <div>
    <%= erb("title.thtml") %>
   </div>
</html>


and the sub template title.thtml

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

Iterating over a list

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.

Arry#cycle is a method to enable that.

class Array
  def cycle(values)
    self.each_with_index do |o, i| 
      yield(o, values[i % values.length])
    end
  end
end


You can use it like this:

<% @something.cycle(["oddRow", "evenRow"]) do |obj, cssClass| %>
  <tr class="<%= cssClass %>">
    <td><%= obj.something %></td>
    <td><%= obj.something_else %></td>
  </tr>
<% end %>
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS