Slim templates - removing whitespaces around the block tag

眉间皱痕 提交于 2019-12-23 13:59:41

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/27737779/slim-templates-removing-whitespaces-around-the-block-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!