![](/img/trans.png)
[英]What are the Rails best practices for javascript templates in restful/resourceful controllers?
[英]Slim templates on Ruby on Rails, what are best practices
我無法理解基本的 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
這是它的輸出:
tahrirlash (編輯) | 奧爾加加
Predmet nomi:英語 5-7 歲
O'quvchi darajasi:初學者
目標 b
對於新行,你應該只使用 br 像:
h1 Line1 content
br
h1 Line2 content
關於上面提到的代碼,可以這樣重寫:
-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
將br標簽插入到 slim 中的某個標簽中:
示例 1. Slim 模板:
h1
| Hello
br
| world
它將產生 html:
<h1>Hello<br>world</h1>
示例 2.顯示表單的細長模板片段:
p
= f.label :title
br
= f.text_field :title
它將產生 html:
<p>
<label for="question_title">Title</label><br>
<input name="question[title]" id="question_title" type="text">
</p>
我必須不同意這一點。 我一直使用$/
代替,原因如下:
br
標簽已經過時,並且由於可能出現導致問題的邊緣情況而變得不那么優先。 相比之下,使用$/
沒有任何影響。$/
總體上更加慣用,因為我們使用的是 ruby 庫。 它是完全不可知的,並且還可以適應其他模式,而無需打印額外標簽的開銷。這是我在幾個實時項目中使用的“真實世界”示例: conditional-html-tags.rb
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.