What exactly are Sinatra’s locals, and is there any difference between using them in templates as opposed to instance variables? Here’s what I mean by locals:
erb :template, :locals => {:variable => 'value'}
Edit:
In terms of implementation, what are the differences between instance and local variables, and are there any benefits to using one over the other?
From Sinatra's intro:
Templates are evaluated within 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 typically used when rendering templates as partials from within other templates.
And for some templates there is (for Radius in this case):
Since you cannot call Ruby methods directly from a Radius template, you almost always want to pass locals to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With