Haml: Control whitespace around text

前端 未结 13 2134
时光说笑
时光说笑 2020-11-30 16:38

In my Rails template, I\'d like to accomplish final HTML to this effect using HAML:

I will first link somewhere

        
相关标签:
13条回答
  • 2020-11-30 17:29

    Although not well documented, this is cleanly achieved using HAML whitespace preservation (>) combined with an ASCII space (& #32;), and not with helpers:

    %a{:href=>'/home'}> Home link
    ,  
    %a{:href=>'/page'} Next link
    

    This will produce what you want:

    <a href='/home'>Anchor text</a>,&#32;
    <a href='/page'>More text</a>
    

    But I agree, HAML needs to come up with a better way of doing this, as it does add unnecessary ASCII characters to the page (but it's still more efficient than using helpers).

    0 讨论(0)
  • 2020-11-30 17:30

    Yet another option that I've used in the past:

    - if @condition
      %span> , then some more text after the link.
    
    0 讨论(0)
  • 2020-11-30 17:31

    The preserve function worked for me

    .white-space-pre= preserve "TEXT"

    0 讨论(0)
  • 2020-11-30 17:33

    You can use the 'aligator syntax' of HAML

    Whitespace Removal: > and <

    and < give you more control over the whitespace near a tag. > will remove all whitespace surrounding a tag, while < will remove all whitespace immediately within a tag. You can think of them as alligators eating the whitespace: > faces out of the tag and eats the whitespace on the outside, and < faces into the tag and eats the whitespace on the inside. They’re placed at the end of a tag definition, after class, id, and attribute declarations but before / or =.

    http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_removal__and_

    0 讨论(0)
  • 2020-11-30 17:33

    The solution that I got working is:

    I will first
    = link_to 'link somewhere', 'http://example.com'
    - if @condition
      = ", then render this half of the sentence if a condition is met"
    

    You can use =, though = is used to output the result of Rails code, but here it will server the purpose.

    0 讨论(0)
  • 2020-11-30 17:37

    A better way to do this has been introduced via Haml's helpers:

    surround

    = surround '(', ')' do
      %a{:href => "food"} chicken
    
    Produces:
    (<a href='food'>chicken</a>)
    

    succeed:

    click
    = succeed '.' do
      %a{:href=>"thing"} here
    
    Produces:
    click
    <a href='thing'>here</a>.
    

    precede:

    = precede '*' do
      %span.small Not really
    
    Produces:
    *<span class='small'>Not really</span>
    

    To answer the original question:

    I will first
    = succeed ',' do
      = link_to 'link somewhere', 'http://example.com'
    - if @condition
      then render this half of the sentence if a condition is met
    
    Produces:
    I will first
    <a href="http://example.com">link somewhere</a>,
    then render this half of the sentence if a condition is met
    
    0 讨论(0)
提交回复
热议问题