Making it better
Posted by Jeremy Voorhis Fri, 02 Dec 2005 01:12:00 GMT
It’s even easier like this:
in app/views/layouts/application.rhtml:<%= xhtml_doctype :strict %>
<%= html_tag :lang => Globalize::Locale.active.language.iso_639_1 %>
<%= @content_for_layout %>
<%= end_html_tag %>
and in app/helpers/application_helper.rb:
module ApplicationHelper
# Renders an xhtml doctype declaration for the document's prolog. Defaults to xhtml transitional.
# <tt>xhtml_doctype :strict</tt>
def xhtml_doctype( doctype=:transitional )
doctype = :transitional unless [:transitional, :strict, :frameset].include? doctype
case doctype
when :transitional
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
when :strict
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
when :frameset
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
end
end
# Displays an html tag, complete with xhtml namespace and language. Accepts language as an option, but defaults to English.
# <tt>html_tag :lang => 'de'</tt>
def html_tag( options={} )
options[:lang] ||= 'en'
lang = options.delete( :lang )
"<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"#{lang}\" xml:lang=\"#{lang}\" #{options.map { |k,v| "#{k}=\"#{v}\"" }.join( ' ' )}>"
end
# End html tag.
def end_html_tag; "</html>" end
end
I plan to release this and some other small http/html-related gems as a plugin soon, so keep an eye out.

This looks like a convenient plugin if there ever was one. Maybe an (optional) built-in link to the validator in the footer would discourage miss-use of the doc type declarations.
I don’t like the validator buttons much myself, and wouldn’t want to impose that upon anyone. It also seems beyond the scope of what the plugin would encompass – utilities for correctly using and getting more out of html and http, but not necessarily bragging about it :)
Not sure here – but shouldn’t one of lang or xml:lang be picked? (depending on how it’s served; application/xml or text/html)