Kyle Sletten
Kyle Sletten

Reputation: 5413

Creating XML with Haml

I'm trying to make a dynamic sitemap for my CMS-style rails app, but I am having trouble creating a sitemap in XML using Haml. I looked at the docs and they say that I should be able to use !!! XML to insert the <?xml version="1.0" encoding="UTF-8"?> tag at the beginning of the document. When I try to do this, it does not render anything at all and I am forced to use a literal meta-xml tag. What am I doing wrong?

content_controller.rb
=====================
class ContentController < ApplicationController
  # other methods

  def sitemap
    @sections = Section.all :include => :pages
    respond_to do |format|
      format.xml
    end
  end
end

sitemap.xml.haml
================
<?xml version="1.0" encoding="UTF-8"?>
-# !!! XML
-# the above tag does not work
%urlset{:xmlns => 'http://www.sitemaps.org/schemas/sitemap/0.9'}
  %url
    %loc= root_url
  - @sections.each do |section|
    - section.pages.each do |page|
      %url
        %loc= "#{root_url}#{section.url}/#{page.url}"
        %lastmod= page.updated_at

Upvotes: 1

Views: 1601

Answers (1)

Vika
Vika

Reputation: 3285

You'll need to set :format => :xhtml in order for this to work.
In your environment.rb

Haml::Template.options[:format] = :xhtml

More info here http://www.mail-archive.com/[email protected]/msg06984.html

Upvotes: 4

Related Questions