Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim templates - removing whitespaces around the block tag

I'm trying Slim templates in a new project (after using Haml for quite a while). While overall experience is great, I've run into a problem with spaces being always inserted around tags which normally use display: block

Say,

ul.ampm
  li.am am
  li.pm pm

generates

<ul class="ampm">
  <li class="am">
    am
  </li>
  <li class="pm">
    pm
  </li>
</ul>

while

.ampm
  span.am am
  span.pm pm

generates

<div class="ampm">
  <span class="am">am</span></span class="pm">pm</span>
</div>

Normally it's not a big issue, but we use responsive layouts which applies display: inline-block to li tags; obviously, having whitespace between them breaks the layout.

I'm aware of

Slim::Engine.options[:pretty] = true

option (and turning it on does remove the offending whitespace), but it makes all generated source hard to read, not just the part I'd like to fix.

< and > in Slim seem to do the opposite to what I need - they're intended to be used for adding whitespace around inline tags.

So...

  • is it possible to remove whitespace around a single block tag in Slim similar to HAML whitespace eaters? (Without the impact of turning off the pretty option, that is)
  • if not, is it a fundamental Slim restriction ("by design") or something which is on the roadmap and would be potentially implemented in the future?

Much appreciated.

like image 754
Konstantin Burnaev Avatar asked Oct 31 '22 12:10

Konstantin Burnaev


1 Answers

I worked around this by reverting back to inline HTML in the critical places. For me, it was adding a collection of options to a select:

select
  - my_collection.each do |item|
  <option value="#{item.id}">#{item.name}</option>

I can put up with this in the few cases where it really matters (as it did for me in the option elements). But if you need better whitespacing throughout your code, I think you're out of luck.

is it a fundamental Slim restriction ("by design") or something which is on the roadmap and would be potentially implemented in the future?

I'm going to go with "no" for both of those. It looks like the Slim team just implemented a naive algorithm and didn't look back. Case in point, this quote from the GitHub issue tracker:

The pretty renderer is not working well under some circumstances since Slim's focus lies on performance. If you have time please provide patches for the pretty renderer of the temple project https://github.com/judofyr/temple and also provide test cases.

like image 91
Craig Walker Avatar answered Nov 12 '22 17:11

Craig Walker