如何在 slim 变量中使用换行符?
How to use line breaks in a slim variable?
我在 slim 中有这个变量:
- foo = 'my \n desired multiline <br/> string'
#{foo}
当我使用 slimrb 命令行命令解析输出时,变量的内容被编码:
my \n desired multiline <br/> string
如何让 slimrb
输出原始内容以生成多行字符串?
请注意,.html_safe
和.raw
都不可用。
这里有两个问题。首先在 Ruby 字符串中使用单引号 – '
– 不要将 \n
转换为换行符,它们保留为文字 \
和 n
。您需要使用双引号。这也适用于 Slim。
其次,Slim HTML escapes the result of interpolation by default. To avoid this use double braces around the code. Slim also HTML escapes Ruby output by default (using =
). To avoid escaping in that case use double equals (==
).
结合这两者,您的代码将类似于:
- foo = "my \n desired multiline <br/> string"
td #{{foo}}
这会产生:
<td>my
desired multiline <br/> string</td>
更简单的方法是使用线指示器作为逐字文本 |
。文档 here 。例如;
p
| This line is on the left margin.
This line will have one space in front of it.
我在 slim 中有这个变量:
- foo = 'my \n desired multiline <br/> string'
#{foo}
当我使用 slimrb 命令行命令解析输出时,变量的内容被编码:
my \n desired multiline <br/> string
如何让 slimrb
输出原始内容以生成多行字符串?
请注意,.html_safe
和.raw
都不可用。
这里有两个问题。首先在 Ruby 字符串中使用单引号 – '
– 不要将 \n
转换为换行符,它们保留为文字 \
和 n
。您需要使用双引号。这也适用于 Slim。
其次,Slim HTML escapes the result of interpolation by default. To avoid this use double braces around the code. Slim also HTML escapes Ruby output by default (using =
). To avoid escaping in that case use double equals (==
).
结合这两者,您的代码将类似于:
- foo = "my \n desired multiline <br/> string"
td #{{foo}}
这会产生:
<td>my
desired multiline <br/> string</td>
更简单的方法是使用线指示器作为逐字文本 |
。文档 here 。例如;
p
| This line is on the left margin.
This line will have one space in front of it.