Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate plain-old XML using Haml?

I've been working on a piece of software where I need to generate a custom XML file to send back to a client application. The current solutions on Ruby/Rails world for generating XML files are slow, at best. Using builder or event Nokogiri, while have a nice syntax and are maintainable solutions, they consume too much time and processing.

I definetly could go to ERB, which provides a good speed at the expense of building the whole XML by hand.

HAML is a great tool, have a nice and straight-forward syntax and is fairly fast. But I'm struggling to build pure XML files using it. Which makes me wonder, is it possible at all?

Does any one have some pointers to some code or docs showing how to do this, build a full, valid XML from HAML?

like image 216
lsdr Avatar asked May 10 '10 18:05

lsdr


People also ask

What is HAML file?

Haml (HTML Abstraction Markup Language) Haml is a markup language that's used to cleanly and simply describe the HTML of any web document, without the use of inline code. Haml functions as a replacement for inline page templating systems such as PHP, ERB, and ASP.

What is HAML in CSS?

What is it? Haml (HTML abstraction markup language) is based on one primary principle: markup should be beautiful. It's not just beauty for beauty's sake either; Haml accelerates and simplifies template creation down to veritable haiku.


3 Answers

This demonstrates some things that could use useful for xml documents:

!!! XML
%root{'xmlns:foo' => 'http://myns'}
  -# Note: :dashed-attr is invalid syntax
  %dashed-tag{'dashed-attr' => 'value'} Text
  %underscore_tag Text
  - ['apple', 'orange', 'pear'].each do |fruit|
    - haml_tag(fruit, "Yummy #{fruit.capitalize}!", 'fruit-code' => fruit.upcase)
  %foo:nstag{'foo:nsattr' => 'value'}

Output:

<?xml version='1.0' encoding='utf-8' ?>
<root xmlns:foo='http://myns'>
  <dashed-tag dashed-attr='value'>Text</dashed-tag>
  <underscore_tag>Text</underscore_tag>
  <apple fruit-code='APPLE'>Yummy Apple!</apple>
  <orange fruit-code='ORANGE'>Yummy Orange!</orange>
  <pear fruit-code='PEAR'>Yummy Pear!</pear>
  <foo:nstag foo:nsattr='value'></foo:nstag>
</root>

Look at the Haml::Helpers link on the haml reference for more methods like haml_tag.

If you want to use double-quotes for attributes,

See: https://stackoverflow.com/a/967065/498594

Or outside of rails use:

>> Haml::Engine.new("%tag{:name => 'value'}", :attr_wrapper => '"').to_html
=> "<tag name=\"value\"></tag>\n"
like image 107
Kelvin Avatar answered Oct 25 '22 18:10

Kelvin


Doing XML in HAML is easy, just start off your template with:

!!! XML

which produces

<?xml version='1.0' encoding='utf-8' ?>

Then as @beanish said earlier, you "make up your own tags":

%test
  %test2 hello
  %item{:name => "blah"}

to get

<test>
  <test2>hello</test2>
  <item name='blah'></item>
</test>

More: http://haml.info/docs/yardoc/file.REFERENCE.html#doctype_

like image 26
philoye Avatar answered Oct 25 '22 16:10

philoye


%test
  %test2 hello
  %item{:name => "blah"}

run it through haml

haml hamltest.haml test.xml

open the file in a browser

<test>
  <test2>hello</test2>
  <item name='blah'></item>
</test>

The HAML reference talks about html tags and gives some examples. HAML reference

like image 38
Beanish Avatar answered Oct 25 '22 18:10

Beanish