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-3 of 3 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!

Block to Partial Rails Helper

From http://errtheblog.com/post/13.
Create a helper which takes a block and renders that block within the context of a partial.

Create this helper:
def block_to_partial(partial_name, options = {}, &block)
  options.merge!(:body => capture(&block))
  concat(render(:partial => partial_name, :locals => options), block.binding)
end


Create this helper, too:
def sidebar_module(title, options = {}, &block)
  block_to_partial('shared/sidebar_module', options.merge(:title => title), &block)
end


Create this partial (app/views/shared/_sidebar_module.rhtml):
<div <%= %[id="#{id}"] unless id.blank? %> class="sidebar_module">
  <h2><%= title %></h2>
  <%= body %>
</div>


Use it in your views:
<% sidebar_module 'Recent Stories' do %>
  <%= render :partial => 'recent_stories', :collection => @recent_stories %>
<% end %>

My default layout in Rails projects

Writing a new layout from scratch gets boring, so I have a generic / default layout to use and extend from.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title><%= @page_title %></title>
	<%= stylesheet_link_tag "main" %>
	<%= javascript_include_tag "prototype" %>
	<%= javascript_include_tag "scriptaculous" %>
	<%= javascript_include_tag "general" %>
	<!--[if lt IE 7.]>
	<script defer type="text/javascript" src="/javascripts/pngfix.js"></script>
	<![endif]-->
</head>

<body class="<%= @body_class %>">
	<div id="container">
			
	<%= @content_for_layout %>

	</div>
</body>
</html>


pngfix.js is available here.
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS