Mike M.
Mike M.

Reputation: 56

Prevent Slim from parsing code inside <script>-tags

I use slim for templating in a sinatra application and ractive.js with handlebars-like syntax on clientside.

I keep template code inside script tags but slim tries to parse them like the rest of the file. My simplified code looks like this:

section.header-section
  h1 Items
section.main-section
script#items type="text/html"
  <ul class="items">
    {{#each items}}
      <li>{{title}}</li>
    {{/each}}
  </ul>

For this code slim parser returns an error: Unknown line indicator ... Line 29, Column 14 {{#each items}} ^.

I tried the suggested workaround:

set :slim, {:pretty => true, :attr_list_delims => {'(' => ')', '[' => ']'}, :code_attr_delims => {'(' => ')', '[' => ']'}}

It didn't help. So I need slim to ignore everything inside script preserving the indentation for readability. The only possible solution is to create my own embedded engine and literaly do nothing inside it. Are there any other ways to fix it?

Upvotes: 3

Views: 2877

Answers (1)

matt
matt

Reputation: 79783

You could use | for verbatim text:

script#items type="text/html"
  |
    <ul class="items">
      {{#each items}}
        <li>{{title}}</li>
      {{/each}}
    </ul>

which produces:

<script id="items" type="text/html"><ul class="items">
  {{#each items}}
    <li>{{title}}</li>
  {{/each}}
</ul></script>

Upvotes: 5

Related Questions