Enhance Your API with HTML Attribute Support

If we want our HTML library to be truly useful to the world, we need to add support for tag attributes such as class and id. Let’s extend our DSL to support an optional keyword list that gets translated into tag attributes for each macro. For example, our goal is to support the following API:

 
div id: ​"main"​ ​do
 
h1 class: ​"title"​, ​do​: text(​"Welcome!"​)
 
div class: ​"row"​ ​do
 
div class: ​"column"​ ​do
 
p ​"Hello!"
 
end
 
end
 
button onclick: ​"javascript: history.go(-1);"​ ​do
 
text ​"Back"
 
end
 
end

Let’s revisit our Html module and add support for tag attributes. Replace your tag/2 macro and for tag <- @tags comprehension with the following code. Save the updated listing ...

Get Metaprogramming Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.