Sinatra locals vs. Instance Variables

What are Sinatras locators, and is there a difference between using them in templates as opposed to instance variables? Here is what I mean by locals :

erb :template, :locals => {:variable => 'value'} 

Edit:

In terms of implementation, what are the differences between instances and local variables, and are there any advantages to using one over the other?

+4
source share
1 answer

From Sinatra intro :

Templates are evaluated in the same context as route handlers. Instance variables set in route handlers are directly accessible by templates:

 get '/:id' do @foo = Foo.find(params[:id]) haml '%h1= @foo.name' end 

Or specify an explicit hash of local variables:

 get '/:id' do foo = Foo.find(params[:id]) haml '%h1= bar.name', :locals => { :bar => foo } end 

This is usually used when rendering templates as partial from other templates.

And for some templates there is (for Radius in this case):

Since you cannot invoke Ruby methods directly from the Radius template, you almost always want to pass locals to it.

+5
source

Source: https://habr.com/ru/post/1485376/


All Articles