February 25, 2008

Posted by John

Tagged html, xml, and yaml

Older: Handy iTerm Shortcut

Newer: KYTCR Part IV: Resource Reporting

How to represent HTML/XML in YAML

So I nearly threw my laptop across the room in a violent rage trying to figure this out. Thankfully I didn’t and rather scoured the googles for a solution. Upon finding it, beautiful music rang from the heavens and a solitary ray of sunshine beamed down on me.

I don’t know if it’s something people rarely try to do or if it’s just hard to find but I thought I would post it here in hopes that the next poor sap who attempts to do so has a bit easier going.

The Tricki-Tikki-Tavi

The trick (if it can be qualified as that) is to use !xml | and some proper spacing like so:

  theme_id: 1
  filename: news.category.liquid
  data: !xml | 
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        	<title>{{ site.name }} // University of Notre Dame</title>
        <div id="news">
        	<h1><a href="/news/">News</a> &raquo; <span class="category_name">{{ category.name }}</span></h1>
        	{% for item in news %}
        		<div class="news">
        			<h2>{{ item | link_to_news }}</h2>
        			<h3>{{ item.published_at | strftime: '%B %d, %Y'}}</h3>
        			<div class="excerpt">
        				{{ item.excerpt }} 
        	{% endfor %}
        	{{ news | will_paginate: path }}
  creator_id: 3
  updater_id: 3

If you line up the html with the !xml, you will be good as gold. Phew. Now back to my regularly scheduled programming.


Sorry, comments are closed for this article to ease the burden of pruning spam.


Authored by John Nunemaker (Noo-neh-maker), a web developer and programmer who has fallen deeply in love with Ruby. More about John.


Feed IconRailsTips Articles - An assortment of howto's and thoughts on Ruby and Rails.

Feed IconRails Quick Tips - Ruby and Rails related links.