Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Javascript Type in Ruby Slim File

I have a Ruby on Rails application with mailers that use slim for the views.

I want to mark up an email with the Google markup format. It calls for a script tag with the type of JSON-LD, like this: <script type="application/ld+json">

My email view is written in slim. I know I can create a script tag that doesn't have a type set in slim by prefacing it with javascript:, but the type will not be set that way.

How can I set the type to JSON-LD in a slim template?

I want to put the Javascript in the script block rather than load it from a src, unlike the person who asked this question: Custom Javascript type with Rails and Slim

Thanks!

like image 935
Will Avatar asked Apr 13 '16 02:04

Will


1 Answers

You could just use a regular script tag and escape the javascript:

script type="application/ld+json"
  | alert('hi’)

Or you could create a shortcut to accomplish this:

From https://github.com/slim-template/slim:

You can also set additional fixed value attributes to a shortcut.

Slim::Engine.set_options shortcut: {'^' => {tag: 'script', attr: 'data-binding',
  additional_attrs: { type: "text/javascript" }}}

Then

^products
  == @products.to_json

which renders to

<script data-binding="products" type="text/javascript">
    [{"name": "product1", "price": "$100"},
     {"name": "prodcut2", "price": "$200"}]
</script>
like image 187
Jeremy Kallman Avatar answered Oct 18 '22 10:10

Jeremy Kallman