How do I include inline JavaScript in Haml?

Active3 hr before
Viewed126 times

7 Answers


External: By using an external JavaScript file.,Internal/Inline: By using “script” element in the “head” or “body” section.,Consider the example below for an HTML file where we use JavaScript to insert a new “p” element into the “div” using inline JavaScript in the “body” section.,We will be extending the below example for HTML and see how it could be implemented similarly for HAML. To include inline JavaScript in HTML, the “script” element is used in the “head” or “body” section and the element contains the JavaScript code in-between the opening and closing tags.

load more v

Docs:,Science Fiction & Fantasy,So i tried the above :javascript which works :) However HAML wraps the generated code in CDATA like so:

: javascript
$(document).ready(function() {
load more v

To include JavaScript directly in a Haml view, use the nifty :javascript filter. Haml sees the contents as JavaScript and wraps them in a script tag when rendered as HTML.,Translate JavaScript Content,This helper is used primarily for internal use, as a macro at the top of several views to declaratively include JavaScript entered through the Admin UI.,Add JavaScript through a Manifest

To include JavaScript directly in a Haml view, use the nifty :javascript filter. Haml sees the contents as JavaScript and wraps them in a script tag when rendered as HTML.

: javascript
load more v

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. However, Haml avoids the need for explicitly coding HTML into the template, because it is actually an abstract description of the HTML, with some code to generate dynamic content.,You can use as many such attribute methods as you want by separating them with commas, like a Ruby argument list. All the hashes will be merged together, from left to right. For example, if you defined,When describing HTML documents with Haml, you can have a document type or XML prolog generated automatically by including the characters !!!. For example:,However in some cases you may want to make Haml explicitly use a specific gem to be used by a filter. In these cases you can do this via Tilt, the library Haml uses to implement many of its filters:

The first step for all of these is to install the Haml gem:

gem install haml
load more v

How can I pass tag attributes with :javascript so that they will be rendered to the <script>-tag?,I'm currently looking into enabling CSP ([1] via [2]) and I've some places where I cannot remove <script> directly. In CSP you can use a nonce as an attribute for <script> tags to allow inline evaluation of JavaScript.,I was able to add nonce attribute to my <script> and <style> tags by doing these two changes.,The good thing about this approach is that there's no need of creating new filters, :javascript and :css add nonce tag now.

   : javascript noince: content_security_policy_script_nonce
// my JavaScript
load more v

Haml is a markup language predominantly used with Ruby that cleanly and simply describes the HTML of any web document without the use of inline code. It is a popular alternative to using Rails templating language (.erb) and allows you to embed Ruby code into your markup.,Whilst this article has focused on how you can write HTML using Haml, we couldn’t finish it without a brief bit on how to get some Ruby code into your template.,Haml comments are comments that included in the template file but not rendered in the final output. You specify them with -# and the same rules that applied to HTML comments apply here too:,You can also use Haml on a project independent of Ruby, by installing the Haml gem on your machine and using the command line to convert it to html.

To use Haml you’ll first need to install it. This shouldn’t be any harder than opening up your command line and typing:

gem install haml
load more v

Dokumente:,Also habe ich das Obige ausprobiert: Javascript, das funktioniert :) HAML verpackt den generierten Code jedoch wie folgt in CDATA:, Account Anmeldung Registrieren ,Der :plainFilter in diesem Szenario entspricht also meinen Anforderungen.

Wie kann ich so etwas schreiben, um es in eine Vorlage aufzunehmen, aber in Haml?

$(document).ready( function() {
  $('body').addClass( 'test' );
} );
load more v

Other "include-inline" queries related to "How do I include inline JavaScript in Haml?"