Cómo insertar símbolos HTML especiales con HAML


Cuando digo:

%p= item.price + " dollars"

Estoy recibiendo

50 & nbsp ;dólares

En lugar de tener un símbolo de espacio no rompible.

¿Cómo insertar este y otro símbolo especial usando HAML?

Author: AntonAL, 2011-06-10

7 answers

¿Qué tal

%p= item.price + " dollars".html_safe
 68
Author: HakonB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-10 11:47:23

Use != en lugar de =

Ver "Unescaping HTML" en la referencia haml: http://haml.info/docs/yardoc/file.REFERENCE.html#unescaping_html

 13
Author: Adam Fraser,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-16 12:39:26

Intenté usar html_safe de diferentes maneras, pero ninguna funcionó. Usar \xa0 como sugirió ngn tampoco funcionó, pero me llevó a probar el escape Unicode del espacio sin ruptura, que funcionó:

"FOO\u00a0BAR"

Y .html_safe ni siquiera es necesario (a menos que algo más en la cadena lo necesite, por supuesto).

El Lenguaje de programación Ruby , primera edición, dice: "En Ruby 1.9, las cadenas entre comillas dobles pueden incluir caracteres de escape Unicode arbitrarios con escapes \u. En su la forma más simple, \u es seguida exactamente por cuatro dígitos hexadecimales ..."

 12
Author: Teemu Leisti,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-29 13:08:42

La opción de interpolación:

%p= "#{item.price} dollars".html_safe
 11
Author: Dave Simpson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-11 14:41:31

Esta respuesta es para una pregunta ligeramente diferente, pero encontré esta pregunta buscándola...
Si tienes una etiqueta submit %input{ :type => "submit", :value => " dollars", :name => "very_contrived" } incluso si lanzas un html_safe en el :value no evaluará el html.
La solución es usar el helper de rails... duh

= submit_tag " dollars".html_safe

Esto es bastante obvio, pero me hizo tropezar. Código heredado + actualización de rails = este tipo de cosas: P

 3
Author: mraaroncruz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-01 13:50:27

Puedes usar \xa0 en la cadena en lugar de  . 0xa0 es el código ASCII del espacio que no se rompe.

 0
Author: ngn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-10 11:10:39

Prefiero usar el carácter en sí con el método HTML escapado con la mayoría de los símbolos que no sean los caracteres de espacio en blanco. De esa manera no tengo que recordar todos los códigos html. En cuanto a los espacios en blanco, prefiero usar CSS, esta es una forma mucho más limpia.

%p&= "#{item.price} $%&#*@"
 0
Author: Danny Oosthoek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-10 07:10:47