Search code examples
javascriptrubyhamlmiddleman

Specify async attribute to javascript with HAML


In order to include google analytics in a web page generate

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-135345611-1">

I couldn't find that much in the docs. Then I found this, but it converted my HTML tags to

%script(:async => "", src="https://www.googletagmanager.com/gtag/js?id=UA-135345611-1")

which failed the middleman v4.2.1 build with:

Invalid attribute list: "(:async => \"\", src=\"https://www.googletagmanager.com/gtag/js?id=UA-135345611-1\")".
partials/_header.html.haml:4

Anyone know how to write this line in HAML so it parses correctly?


Solution

  • Did you try this?

    %script{:async => "", :src => "https://www.googletagmanager.com/gtag/js?id=UA-135345611-1"}
    

    When I ran your code through that site it wrapped it with brackets rather than parens. Maybe that's why it's not parsing correctly?

    On a side note, I personally use https://html2haml.herokuapp.com/ since it's using the same version of haml that middleman uses.