Haml: Kontroluj białe znaki wokół tekstu

W moim szablonie Rails chciałbym wykonać końcowy HTML w tym celu używając HAML:

I will first <a href="http://example.com">link somewhere</a>, then render this half of the sentence if a condition is met

Szablon zbliżony:

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

Można jednak zauważyć, że tworzy to spację między łączem a przecinkiem. Czy jest jakiś praktyczny sposób, aby uniknąć tej białej spacji? Wiem, że istnieje składnia do usuwania białych znaków wokół tagów, ale czy ta sama składnia może być stosowana tylko do tekstu? Naprawdę nie podoba mi się rozwiązanie dodatkowych znaczników, aby to osiągnąć.

Author: Arslan Ali, 2009-08-21

13 answers

[[8]}lepszy sposób na to został wprowadzony przez pomocników Hamla:

Surround

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

:

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

Poprzedzać :

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

Aby odpowiedzieć na pierwotne pytanie:

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
Produkuje:
I will first
<a href="http://example.com">link somewhere</a>,
then render this half of the sentence if a condition is met
 211
Author: Ryan Crispin Heneise,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-24 13:26:02

Można to również zrobić za pomocą modyfikatora Haml "trim whitespace". Wstawianie > po deklaracji Haml zapobiega dodawaniu białych znaków wokół niej:

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

Produkuje:

I will first<a href='http://example.com'>link somewhere</a>, then render this half of the sentence if a condition is met

Jednak, jak widać, modyfikator > również usuwa białe spacje przed linkiem, usuwając żądaną spację między słowami a linkiem. Nie wymyśliłem jeszcze ładnego sposobu na obejście tego, poza dodaniem &nbsp; do końca "będę pierwszy", tak:

I will first&nbsp;
%a{:href => 'http://example.com'}> link somewhere
- if @condition
  , then render this half of the sentence if a condition is met

Które w końcu generuje żądane wyjście bez dużej ilości trudno odczytanej interpolacji:

I will first&nbsp;<span><a href="http://example.com">link somewhere</a></span>, then render this half of the sentence if a condition is met
 40
Author: Ryan Crispin Heneise,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-07 11:30:39

Dobra, oto rozwiązanie, które ustalam:

Helper

def one_line(&block)
  haml_concat capture_haml(&block).gsub("\n", '').gsub('\\n', "\n")
end

Widok

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

W ten sposób, białe znaki są domyślnie wykluczone, ale nadal mogę jawnie dołączyć je do linii "\n". (Potrzebuje podwójnego ukośnika, ponieważ w przeciwnym razie HAML interpretuje go jako rzeczywistą nową linię.) Daj mi znać, jeśli jest lepsza opcja tam!

 12
Author: Matchu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-21 15:26:55

Możesz użyć 'składni aligatora' HAML

Usuwanie spacji: > i

I usunie wszystkie białe znaki otaczające znacznik, podczas gdy twarze poza znacznikiem i je białe znaki na zewnątrz, a

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

 6
Author: Yo Ludke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-24 12:40:50

Gdy podejdę do tego typu rzeczy, użyję interpolacji łańcuchowej:

I will first #{link_to 'Link somewhere'}#{', then render this half of the sentence if a condition is met' if condition}

Nie podoba mi się wygląd literalnego ciągu w interpolacji, ale używałem go wcześniej z zadeklarowanymi łańcuchami lub dynamicznie generowanymi łańcuchami.

 5
Author: Chuck,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-21 16:49:41

Możesz to zrobić, aby zachować wiodącą przestrzeń:

%a{:href => 'http://example.com'}>= ' link somewhere'

Spacja jest w cudzysłowach.

 5
Author: thethinman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-24 09:58:51

Chociaż nie jest to dobrze udokumentowane, jest to czysto osiągane przy użyciu zachowania białych przestrzeni HAML (>) w połączeniu z przestrzenią ASCII (& #32;), a nie przy pomocy helperów:

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

To będzie produkować to, co chcesz:

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

Ale Zgadzam się, HAML musi wymyślić lepszy sposób na zrobienie tego, ponieważ dodaje niepotrzebne znaki ASCII do strony (ale i tak jest bardziej wydajny niż używanie helperów).

 3
Author: ojak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-16 08:34:47

Jest składnia Angle bracket "whitespace munching", w przeciwnym razie napisz dla niej metodę pomocniczą.

 1
Author: Andrew Vit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-08-21 11:49:30

Natknąłem się na podobny problem i znalazłem to, więc pomyślałem, że opublikuję inne rozwiązanie, które nie wymaga metody pomocniczej. Użyj interpolacji Ruby #{} do zawinięcia linku i instrukcji if:

I will first 
#{link_to 'link somewhere', 'http://example.com'}#{if true : ", then render this half of the sentence if a condition is met" end}

To działa w 3.0.18, może również działać we wcześniejszych wydaniach.

 1
Author: biscuits,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-19 04:56:30

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

- if @condition
  %span> , then some more text after the link.
 1
Author: colllin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-13 07:37:50

Można też zawsze:

= link_to url_path do 
  = ["part_1", "part_2"].join(", ")
 0
Author: bcackerman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-10 04:13:18

Rozwiązanie, które mam działa jest:

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

Możesz użyć =, chociaż = jest używany do wypisania wyniku kodu Rails, ale tutaj będzie serwerem celu.

 0
Author: Arslan Ali,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-24 10:08:21

Funkcja preserve zadziałała dla mnie

.white-space-pre= preserve "TEXT"

 0
Author: gtournie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-20 02:04:13