Multiple Haml Elements on Same Line

落爺英雄遲暮 提交于 2019-11-29 03:55:40
edtsech

Unfortunately, you cannot put two haml elements on the same line.

You can achieve something similar by using inline html elements:

%h1 <a href='/'>Lorem ipsum</a>

Why don't you like this format?

%h1 
  %a{:href => '/'} Professio.

Another option is to write special 'helper' method (that generate an html link). For example, the link_to in Rails:

%h1= link_to 'Professio', root_url
Jim Garvin

Late answer, but I was just tackling the same problem myself and knew HAML had a way to do this as part of the language. See the White Space Removal section of the HAML reference.

You could do it this way:

%h1<
  %a{ :href => '/' } Professio.

Or this way:

%h1
  %a{ :href => '/' }> Professio.

Haml cannot do this. Slim can:

h1: a( href='/' ) Professio.

is the same as:

h1
  a( href="/ ) Professio

You can write as deeper tree as you need:

ul.nav
  li.active: a( href="/home" ):    strong Home
  li:        a( href="/contact" ): span   Contact

Jade also has similar syntax and support this feature, but it's been designed for Node.js environment.

SundayEdition

If you're looking to preserve the HTML on the same line you could try something like this:

irb> print Haml::Engine.new("%h1<\n  %a{:href => '/'} Profession.").render()
<h1><b href='/'>Profession.</a></h1>

Found here: HAML whitespace removal

[Edit: I know that's says b href above...]

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