maki
maki

Reputation: 535

slim template iniline ruby code

I''m trying to include some ruby code in slim template, this is the code I want to put in slim:

      - nav_links_group = nil
      - if if_page_var('visible', :main_menu, :solutions)
      -   nav_links_group = @nav_links["solutions"]
      - elsif if_page_var('visible', :main_menu, :resources)
      -   nav_links_group = @nav_links["resources"]
      - elsif if_page_var('visible', :main_menu, :people)
      -   nav_links_group = @nav_links["people"]
      -end

      - if nav_links_group
      - current_index = nav_links_group.map{|e| e[0]}.index(request.original_url)
        - if current_index == 0
          span = sub_menu_link "#{nav_links_group[current_index + 1][0]}", "#{nav_links_group[current_index + 1][1]}"
        - elsif current_index + 1 == nav_links_group.count
          span = sub_menu_link "#{nav_links_group[current_index - 1][0]}", "#{nav_links_group[current_index - 1][1]}"
        - elsif current_index > 0 && current_index + 1 != nav_links_group.count
          span = sub_menu_link "#{nav_links_group[current_index - 1][0]}", "#{nav_links_group[current_index - 1][1]}"
          span = sub_menu_link "#{nav_links_group[current_index + 1][0]}", "#{nav_links_group[current_index + 1][1]}"
        - end
      - end

I got the error: syntax error, unexpected keyword_elsif

any suggestions?

Upvotes: 2

Views: 4615

Answers (2)

blackbiron
blackbiron

Reputation: 827

reviewing your code, i can say that your issue was :

  • slim doesn't use - end
  • your indentations are messed
  • don't use space between tag and '=' (use span= sub_menu_link, not span = sub_menu_link)

here some example

- nav_links_group = nil
- if if_page_var('visible', :main_menu, :solutions)
  - nav_links_group = @nav_links["solutions"]
- elsif if_page_var('visible', :main_menu, :resources)
  - nav_links_group = @nav_links["resources"]
- elsif if_page_var('visible', :main_menu, :people)
  - nav_links_group = @nav_links["people"]

- if nav_links_group
  - current_index = nav_links_group.map{|e| e[0]}.index(request.original_url)
  - if current_index == 0
    span= sub_menu_link "#{nav_links_group[current_index + 1][0]}", "#{nav_links_group[current_index + 1][1]}"
  - elsif current_index + 1 == nav_links_group.count
    span= sub_menu_link "#{nav_links_group[current_index - 1][0]}", "#{nav_links_group[current_index - 1][1]}"
  - elsif current_index > 0 && current_index + 1 != nav_links_group.count
    span= sub_menu_link "#{nav_links_group[current_index - 1][0]}", "#{nav_links_group[current_index - 1][1]}"
    span= sub_menu_link "#{nav_links_group[current_index + 1][0]}", "#{nav_links_group[current_index + 1][1]}"

Upvotes: 1

dyeje
dyeje

Reputation: 305

If you need to do multiline ruby in slim and you aren't adding elements, you can use ruby: and then put the code indented under it.

For your other code, simply remove the end keywords

ruby:
  nav_links_group = nil
  if if_page_var('visible', :main_menu, :solutions)
    nav_links_group = @nav_links["solutions"]
  elsif if_page_var('visible', :main_menu, :resources)
    nav_links_group = @nav_links["resources"]
  elsif if_page_var('visible', :main_menu, :people)
    nav_links_group = @nav_links["people"]
  end

- if nav_links_group
  - current_index = nav_links_group.map{|e| e[0]}.index(request.original_url)
  - if current_index == 0
    span= sub_menu_link "#{nav_links_group[current_index + 1][0]}", "#{nav_links_group[current_index + 1][1]}"
  - elsif current_index + 1 == nav_links_group.count
    span= sub_menu_link "#{nav_links_group[current_index - 1][0]}", "#{nav_links_group[current_index - 1][1]}"
  - elsif current_index > 0 && current_index + 1 != nav_links_group.count
    span= sub_menu_link "#{nav_links_group[current_index - 1][0]}", "#{nav_links_group[current_index - 1][1]}"
    span= sub_menu_link "#{nav_links_group[current_index + 1][0]}", "#{nav_links_group[current_index + 1][1]}"

Upvotes: 8

Related Questions