【问题标题】:How to declare variable with a lot of new lines in Rail's haml file?如何在 Rail 的 haml 文件中用大量新行声明变量?
【发布时间】:2021-09-04 07:46:43
【问题描述】:

版本

Rails 6.0.3
红宝石 2.7.2

我想做什么

haml文件中有text_area_tag

text_area_tag("context[]",nil,:wrap=>"virtual",:rows=>"8")

现在我想添加这样的句子作为占位符

placeholder test

Hi I'm Mike, placeholder
new line

new line
new line

Hi I'm Michelle
new line
new line

Hi I'm Goku
new line new line

(and more... and more...)
・
・
・

问题

我可以像这样在 view haml 文件中声明变量

- var = "placeholder test 
 Hi I'm Mike, placeholder 
 new line 
 
 new line .........."

但它太长了,超级意大利面条代码。 如果我可以像javascript↓那样使用,那就有用了

const var = `
placeholder test

Hi I'm Mike, placeholder
new line

......
`

如果有人知道在 haml 文件中使用大量新行声明变量的更好方法,请告诉我。

【问题讨论】:

    标签: ruby-on-rails newline haml


    【解决方案1】:

    要创建新行,你可以直接在haml文件上使用“\n”,或者像我的方式使用times4这样的变量,注意不要将名称设置为4times,它将不起作用!

    %h4 Content
    - var = "placeholder test 
 Hi I'm Mike, placeholder 
 new line 
 
 new line .........."
    - iso1 = "placeholder test \nHi I'm Mike, placeholder \nnew line \n \nnew line .........."
    - times4 = "\n" * 4
    - iso2 = "placeholder test \nHi I'm Mike, placeholder \nnew line #{times4}new line .........."
    = text_area_tag("context[]",nil,:wrap=>"virtual",:rows=>"8", :placeholder=>"#{iso2}")
    

    编码愉快!

    多行更新,可以使用“|”在变量声明中,例如:

    - iso3 = "placeholder test \n" + |
    "Hi I'm Mike, placeholder \n" + |
    "new line #{times4}" + |
    "new line .........." |
    

    注意以“|”结尾,否则会报错!

    做得更多,开门! : D

    【讨论】:

    • 感谢您给了我“4times”的方式:) 但不幸的是,它至少需要在变量中使用长的一行句子,对吗?也许我会这样做
    • 更新了多行 var 声明!
    猜你喜欢
    • 2011-05-05
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2017-07-12
    • 2012-11-14
    • 2020-09-17
    相关资源
    最近更新 更多