Class: Wytch::SitemapView

Inherits:
Object
  • Object
show all
Defined in:
lib/wytch/sitemap_view.rb

Overview

Generates an XML sitemap for the site.

This view renders a sitemap.xml file listing all pages that have include_in_sitemap? returning true.

Examples:

Using in a content file (content/sitemap.rb)

view Wytch::SitemapView
add Wytch::SitemapHelper

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ SitemapView

Creates a new sitemap view.

Parameters:

  • page (Page)

    the page object (not used but required for interface)



16
17
18
# File 'lib/wytch/sitemap_view.rb', line 16

def initialize(page)
  @page = page
end

Instance Method Details

#callString

Renders the sitemap XML.

Returns:

  • (String)

    XML sitemap content



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wytch/sitemap_view.rb', line 23

def call
  require "builder"

  xml = ::Builder::XmlMarkup.new(indent: 2)
  xml.instruct! :xml, version: "1.0", encoding: "UTF-8"
  xml.urlset xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" do
    Wytch.site.pages.values.each do |page|
      next unless page.include_in_sitemap?
      xml.url do
        xml.loc "#{Wytch.site.base_url}#{page.path}"
        xml.lastmod page.last_modified if page.last_modified
        xml.changefreq page.change_frequency if page.change_frequency
        xml.priority page.priority if page.priority
      end
    end
  end
end