<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dot.Using &#187; apache</title>
	<atom:link href="http://blog.kesor.net/category/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kesor.net</link>
	<description>Making technology about computers, and computers about usability.</description>
	<lastBuildDate>Mon, 29 Jun 2009 22:46:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>HAML Caching CGI</title>
		<link>http://blog.kesor.net/2007/07/30/haml-caching-cgi/</link>
		<comments>http://blog.kesor.net/2007/07/30/haml-caching-cgi/#comments</comments>
		<pubDate>Mon, 30 Jul 2007 08:38:23 +0000</pubDate>
		<dc:creator>kesor</dc:creator>
				<category><![CDATA[apache]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[haml]]></category>
		<category><![CDATA[ideas]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.kesor.net/2007/07/30/haml-caching-cgi/</guid>
		<description><![CDATA[Mike Zillion asked about how to make HAML a processor (of haml files) for Apache on the HAML Group on Google. That inspired me to write a proper wrapper with caching that will Hamlize templates into HTML and cache those for speedy access on subsequent requests.
This is what I came up with:


#!/bin/env ruby

exit if ARGV[0].nil?
exit [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mikezillion.com">Mike Zillion</a> asked about how to make HAML a processor (of haml files) for Apache on the <a href="http://groups.google.com/group/haml/t/b2fd4dbc5f76cd61">HAML Group</a> on Google. That inspired me to write a proper wrapper with caching that will Hamlize templates into HTML and cache those for speedy access on subsequent requests.</p>
<p>This is what I came up with:<br />
<span id="more-9"></span></p>
<pre lang="ruby">
#!/bin/env ruby

exit if ARGV[0].nil?
exit unless File.exists?(ARGV[0])

CACHE_DIR_NAME='cache'

haml_file = ARGV[0]
haml_time = File.stat(haml_file).mtime

html_file = CACHE_DIR_NAME + '/' + haml_file.sub(/aml$/,'tml')
if File.exists?(html_file)
  html_time = File.stat(html_file).mtime

  if html_time > haml_time
    output = File.read(html_file)
  end
end

if output.nil?
  require 'rubygems'
  require 'haml'
  template = File.read(haml_file)
  haml_engine = Haml::Engine.new(template)
  output = haml_engine.to_html()

  # cache the output
  Dir.mkdir(CACHE_DIR_NAME) unless File.directory?(CACHE_DIR_NAME)
  html_file_io = File.open(html_file,"w")
  html_file_io.print(output)
  File.utime(Time.now, haml_time, html_file)
end

# unbuffer output
$stdout.sync = true

require 'cgi'
ENV['SERVER_SOFTWARE'] ||= 'not set'
cgi = CGI.new('html3')
print cgi.header(
        'type'     => 'text/html',
        'charset'  => 'UTF-8',
        'length'   => output.length,
        'server'   => ENV['SERVER_SOFTWARE'],
        'expires'  => Time.now + 10*3600*24, # 10 days
        'Pragma'   => 'no-cache',
        'Last-Modified' => haml_time,
        'Cache-Control' => 'no-cache'
)
print output
</pre>
<p>And as Mike suggested, adding a couple lines to your Apache configuration makes all the difference:</p>
<pre line="1">
  AddType text/haml .haml
  AddHandler haml-file .haml
  Action haml-file /dev/bin/haml_cache_cgi.rb
  Action text/haml /dev/bin/haml_cache_cgi.rb
</pre>
<p></p>

<div class="sociable">

<ul>
	<li class="sociablefirst"><a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fblog.kesor.net%2F2007%2F07%2F30%2Fhaml-caching-cgi%2F&amp;title=HAML%20Caching%20CGI&amp;bodytext=Mike%20Zillion%20asked%20about%20how%20to%20make%20HAML%20a%20processor%20%28of%20haml%20files%29%20for%20Apache%20on%20the%20HAML%20Group%20on%20Google.%20That%20inspired%20me%20to%20write%20a%20proper%20wrapper%20with%20caching%20that%20will%20Hamlize%20templates%20into%20HTML%20and%20cache%20those%20for%20speedy%20access%20on%20subsequen" title="Digg"><img src="http://blog.kesor.net/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fblog.kesor.net%2F2007%2F07%2F30%2Fhaml-caching-cgi%2F&amp;title=HAML%20Caching%20CGI&amp;notes=Mike%20Zillion%20asked%20about%20how%20to%20make%20HAML%20a%20processor%20%28of%20haml%20files%29%20for%20Apache%20on%20the%20HAML%20Group%20on%20Google.%20That%20inspired%20me%20to%20write%20a%20proper%20wrapper%20with%20caching%20that%20will%20Hamlize%20templates%20into%20HTML%20and%20cache%20those%20for%20speedy%20access%20on%20subsequen" title="del.icio.us"><img src="http://blog.kesor.net/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fblog.kesor.net%2F2007%2F07%2F30%2Fhaml-caching-cgi%2F&amp;title=HAML%20Caching%20CGI" title="Reddit"><img src="http://blog.kesor.net/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fblog.kesor.net%2F2007%2F07%2F30%2Fhaml-caching-cgi%2F" title="Technorati"><img src="http://blog.kesor.net/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="mailto:?subject=HAML%20Caching%20CGI&amp;body=http%3A%2F%2Fblog.kesor.net%2F2007%2F07%2F30%2Fhaml-caching-cgi%2F" title="email"><img src="http://blog.kesor.net/wp-content/plugins/sociable/images/email_link.png" title="email" alt="email" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fblog.kesor.net%2F2007%2F07%2F30%2Fhaml-caching-cgi%2F&amp;t=HAML%20Caching%20CGI" title="Facebook"><img src="http://blog.kesor.net/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fblog.kesor.net%2F2007%2F07%2F30%2Fhaml-caching-cgi%2F&amp;title=HAML%20Caching%20CGI&amp;annotation=Mike%20Zillion%20asked%20about%20how%20to%20make%20HAML%20a%20processor%20%28of%20haml%20files%29%20for%20Apache%20on%20the%20HAML%20Group%20on%20Google.%20That%20inspired%20me%20to%20write%20a%20proper%20wrapper%20with%20caching%20that%20will%20Hamlize%20templates%20into%20HTML%20and%20cache%20those%20for%20speedy%20access%20on%20subsequen" title="Google Bookmarks"><img src="http://blog.kesor.net/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a></li>
	<li><a rel="nofollow"  href="" title="Pownce"><img src="http://blog.kesor.net/wp-content/plugins/sociable/images/" title="Pownce" alt="Pownce" class="sociable-hovers" /></a></li>
	<li class="sociablelast"><a rel="nofollow"  href="" title="TwitThis"><img src="http://blog.kesor.net/wp-content/plugins/sociable/images/" title="TwitThis" alt="TwitThis" class="sociable-hovers" /></a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.kesor.net/2007/07/30/haml-caching-cgi/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.194 seconds -->
