Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim templates on Ruby on Rails, what are best practices

I have trouble understanding basic Slim syntax.

First question, how to do you enter new line (line break)?

Second request, could you please rewrite the following snippet, I suspect I didn't do it easy way?

- 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

This is its output:

Title a

tahrirlash (edit) | orqaga

Predmet nomi: english 5-7 year olds

O'quvchi darajasi: beginner

Kurs haqida ma'lumot

objectives b

like image 373
B.I. Avatar asked Sep 18 '13 16:09

B.I.


People also ask

What is Slim in Rails?

What is Slim? Slim is a fast, lightweight templating engine with support for Rails 3 and later. It has been heavily tested on all major ruby implementations. We use continuous integration (travis-ci).

What is a slim file?

Definition of slim file : a file very narrow in proportion to its length.

What is Slim css?

Slim is a page-templating language that minimizes markup and syntax. It removes most of the extra "programming-like" symbols from HTML so that your code looks cleaner. Slim also adds if-else statements, loops, includes, and more. These let you build complex pages without resorting to server-side languages like PHP.


2 Answers

For new line, you should just use br like:

h1 Line1 content
br
h1 Line2 content

And about the above mentioned code, it can be rewrite like this:

-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
like image 176
Aman Garg Avatar answered Sep 28 '22 08:09

Aman Garg


To insert br tag into some tag in slim:

Example 1. Slim template:

h1
  | Hello
  br
  | world

It will produce html:

<h1>Hello<br>world</h1>

Example 2. Fragment of slim template for displaying form:

p
  = f.label :title
  br
  = f.text_field :title

It will produce html:

<p>
  <label for="question_title">Title</label><br>
  <input name="question[title]" id="question_title" type="text">
</p>
like image 35
yesnik Avatar answered Sep 28 '22 08:09

yesnik