Custom Tags with XMLSubsMatch

[Index |RD format]

Before XML, there was the need to make HTML markup smarter. Ruby/ASP gives you the ability to have a Ruby subroutine handle the execution of any predefined tag, taking the tag descriptors, and the text contained between, as block of the subroutine. This custom tag technology can be used to extend a web developer's abilities to add dynamic pieces without having to visibly use <% %> style code entries.

So, lets say that you have a table that you want to insert for an employee with contact info and the like, you could set up a tag like:

<my:new-employee name="Jane" last="Doe" phone="555-2222">
  Jane Doe has been here since 1998.
</my:new-employee>

Any colons, ':', in the XML custom tag will turn into '::', a Ruby mosule separator, so the my:employee tag would translate to the my::employee subroutine, or the employee subroutine in the my module. Any dash "-" will also be translated to an underscore "_", as dash is not valid in the names of Ruby subroutines.

Then you would create the my::employee subroutine in the my ruby module or whereever like so:

module My
  def My::test( h )
    print "<B>In My::Test</B><BR>\n"
    yield if block_given?
    h.each_pair { |k,v|
      print "#{k} =&gt; #{v}<BR>\n"
    }
  end
end

Though XML purists would not like this custom tag technology to be related to XML, the reality is that a careful site engineer could render full XML documents with this technology, applying all the correct styles that one might otherwise do with XSLT.

Custom tags defined in this way can be used as XML tags are defined with both a body and without as it

<my:new-employee>...</my:new-employee>

and just

<my:new-employee />

These tags are very powerful in that they can also enclose normal ASP logic, like:

<my:new-employee>
  <!-- normal ASP logic -->
  <% birthday = Time::new %>

  <!-- ASP inserts -->
  This employee has been online for <%= int(rand()*600)+1 %>
  seconds, and was born near <%= birthday %>.
</my:new-employee>   

For an example of this custom XML tagging in action, please check out the tests/subsmatch.asp script.