O'Reilly logo

Metaprogramming Elixir by Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required