Partial Sinatra Variable Access

I am new to Sinatra and I am trying to access data from a partial database.

Here is a partial example that I want on the page:

<% @articles.each do |article| %> <ul> <li> <%= article.articleName %> </li> </ul> <% end %> 

It works fine if I just set up a route, for example

 get '/articles' do @article = Articles.all erb :articles end 

and the / articles page with something like

 <% @articles.each do |article| %> <article> <p> <%= article.articleName %> </p> <p> <%= article.articleBody %> </p> </article> <% end %> 

However, it looks like this code works if I put it in partial.

Any help would be greatly appreciated. I am sure that I am missing something simple.

+4
source share
2 answers

Sinatra does not have built-in scores such as Rails, but you can use regular templates as partial, as indicated in: http://www.sinatrarb.com/faq.html#partials

Example:

article template:

 <% @articles.each do |article| %> <%= erb :'partials/_article', :layout => false, :locals => { :article => article } %> <% end %> 

partials / _article template:

 Title <%= article.title %> ... 

PS: set the path to the partial from the root of the dir template. This strange syntax :'partials/_article' is a Sinatra trick, it allows you to access the template in subdir, it will not work (I think) :partials/_article or 'partials/_article' .

+4
source

Sinatra does not have partial functionality. So you have two options:

  • create your own partial handler like here or
  • use partials.rb from here
+2
source

All Articles