Ruby on Rails上的Slim模板,最佳实践是什么

B.I*_*.I. 6 templates ruby-on-rails slim-lang

我无法理解基本的Slim语法.

第一个问题,如何进入换行(换行符)?

第二个请求,请你重写下面的片段,我怀疑我没有这么简单的方法吗?

- provide(:title, @course.title)                                                                                                          

.row
  aside.span4
    section
      h1 = @course.title.capitalize

      => link_to t('ui.edit'), edit_course_path(@course)
      '|
      => link_to t('ui.back'), courses_path

      p
        b #{t('activerecord.attributes.subject.title')}:
        | #{@course.subject.title}

      p
        b #{t('activerecord.attributes.student_level.title')}:
        | #{@course.student_level.title}

      h4 #{t('activerecord.attributes.course.objectives')}
      = @course.objectives
Run Code Online (Sandbox Code Playgroud)

这是它的输出:

标题a

tahrirlash(编辑)| orqaga

Predmet nomi:英语5-7岁

O'quvchi darajasi:初学者

Kurs haqida ma'lumot

目标b

Ama*_*arg 9

对于新行,您应该只使用br:

h1 Line1 content
br
h1 Line2 content
Run Code Online (Sandbox Code Playgroud)

关于上面提到的代码,它可以像这样重写:

-provide(:title,@course.title)                                                    
.row
  aside.span4
    section
      h1 = @course.title.capitalize

      = link_to t('ui.edit'), edit_course_path(@course)
      '|
      = link_to t('ui.back'), courses_path

      p
        b = t('activerecord.attributes.subject.title')
        |:     
        = @course.subject.title

      p
        b = t('activerecord.attributes.student_level.title')
        |: 
        = @course.student_level.title

      h4 = t('activerecord.attributes.course.objectives')
      = @course.objectives
Run Code Online (Sandbox Code Playgroud)

  • Slim似乎不支持单行<br>标签而不会破坏新元素.Aman Garg上面的例子强制一个新的<h1>标签.Alt:如果你想要它在同一条线上,那就是做````h1 content <br> content```. (3认同)

yes*_*nik 5

要将br标签插入 slim 中的某个标签中:

示例 1. Slim 模板:

h1
  | Hello
  br
  | world
Run Code Online (Sandbox Code Playgroud)

它将生成 html:

<h1>Hello<br>world</h1>
Run Code Online (Sandbox Code Playgroud)

示例 2.用于显示表单的 slim 模板片段:

p
  = f.label :title
  br
  = f.text_field :title
Run Code Online (Sandbox Code Playgroud)

它将生成 html:

<p>
  <label for="question_title">Title</label><br>
  <input name="question[title]" id="question_title" type="text">
</p>
Run Code Online (Sandbox Code Playgroud)