Add to Technorati Favorites

adbits - tell the world

Google Sitemap with Ditto

Votes: 2731
Welcome one and everyone, in this tutorial we'll be going through the basic steps of generating  (on the fly) a Google Sitemap using nothing but Ditto in the ModX CMS, it's a thing of beauty.

The spec

First lets go over the google map spec, you can read up on it here: https://www.google.com/webmasters/tools/docs/en/protocol.html Below is the main "jist" of it all.

<?xml version="1.0" encoding="UTF-8"?>
  < urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
   < url>
    < loc>http://www.example.com/</loc>
    < lastmod>2005-01-01</lastmod>
    < changefreq>monthly</changefreq>
    < priority>0.8</priority>
   </url> 
  </urlset>

In Previous tutorials I've covered using Ditto to solve many XML "list" problems, this really no different.

Build the chunk

The "chunk" is an internal term ModX uses for little pieces of utility code, they can often be included in snippets like Ditto.  Lets name the chunk "google_sitemap".

<url>      
<loc>[(site_url)]/[~[+id+]~]</loc>      
<lastmod>[+date+]</lastmod>      
<priority>[+tvpriority+]</priority>
<changefreq>[+tvchangefreq+]</changefreq>

</url>
In Ditto, the chunk is the part that repeats, so we'll have to put some placeholders in to tell Ditto where to stick our data.
<loc>   - opening location tag
 [(site_url)] - put the site base URL here
/ - add in the slash between the site url and the page url
[~[+id+]~] - the internal page URL
</loc>  - Closing location tag.
Lets tackle the lastmod tag next.  This tag is optional in the spec, but with Ditto it's so easy.

 

<lastmod>  // opening modified date tag
[+date+]  // date placeholder for Ditto , we format this later in the Ditto call
</lastmod> // closing tag

The priority seemed tricky at first, but I then thought about it more while I was in a meeting, and I found a solution.

<priority> //opening Tag
[+tvpriority+] // Template Variable for holding a priority level from 0.1 to 1.0, it defaults to 0.5
</priority> //closing tag

<changefreq> //opening Tag
[+tvchangefreq+] // Template Variable for holding a changefreq from always to never, it defaults to yearly
</changefreq> //closing tag


And then wrap it all in the <url></url> tags.

 

The Template Variables

This will allow us to set a priority level for each page. Create a new template variable and name it "priority", give it an appropriate caption, I used "Google sitemap priority".  Choose DropDown List Menu for the input type, I found it to be the most efficient.  I then used a double pipes delimited list, for the options like so: .1||.2|| .3|| .5|| .6|| .7|| .8|| .9 1 I then set the Default value to be .5, and applied it to any pages I wish to use the sitemap (all of them)

Now do the same for a second TV called "changefreq", using these values always||hourly||daily||weekly||monthly||yearly||never I set the defualt to "yearly".

The Page

Lets make a page in the root of our site and call it "sitemap.xml" and give it the correct alias (we all use friendly urls right? See me after class if you don't).
Make it uses these settings

 Paste this into the page content
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
[[Ditto? &dateFormat=`%Y-%m-%d`&startID=`0` &tpl=`google_sitemap` &displayArchive=`0`&dateSource=`editon`&summarize=`500` &total=`500` sortby=`createon`&descendentDepth=`3`&showInMenuOnly=`1`&filter=`type,document,1`]] </urlset>

Lets go through the Ditto Call

[[Ditto? //openeing call
&dateFormat=`%Y-%m-%d` //Format the date for the XML spec
&startID=`0` // start at the site root
&tpl=`google_sitemap` // use the chunk we setup earlier
&displayArchive=`0` // don't use the archive
&dateSource=`editedon` // use the eaditon date for the placeholder
&summarize=`500` //put up to 500 entries in the list
&total=`500` //put up to 500 entries in the list
&sortby=`createon` // sort it by the creation date for fun, Google says physical placement has no meaning.
&descendentDepth=`3` // Go 3 levels deep
&showInMenuOnly=`1` //Hide docuemtns marked "do not show in menu"
&filter=`type,document,1` // Filter out weblinks, because they don't have TVs
]] // close

Make sure you set this page to not show in menu. 

Happy Sitemapping! 

Check our sitemap out , croll to the bottom to see the variations in priority. 

Update

This tutorial is written using Ditto 1.2 with Ditto 2 you don't need the tv prefix for the placeholder in the chunk for the Templace Variable (TV) and descendentDepth is simply depth.

Update 2

I submitted this page's Sitemap to google, and wouldn't you know I got an error, at line 6.  I had to add in &showInMenuOnly=`1`, Then I had to set my sitemap.xml to not show in menu, because since it's template is blank it has to template variable for priority, and the priority value is blank in the xml.  Complicated I know, I made the changes in the example code. 

Update 3

Added Changefreq option