You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now what I want to do is invoke Sinatra helpers in the body of the FooWidget. For example, I might have some CSS file in my FooWidget, being linked to via some path /css/app.css. But this might fail if my app was actually hosted at example.com/myapp. I want to use the to helper instead. So somehow, I need to get the to helper into the widget.
The heart of the problem (at least it looks that way to me) is that the to helper has to be evaluated in the context of the sinatra app instance, so my current solution is to wrap the helper in a Proc and pass it to the widget like this:
# in the Sinatra appget'/'doFooWidget.new(:to=>Proc.new{ |url| tourl}).to_htmlend# in FooWidgetdefcontentlink:rel=>stylesheet,:href=>@to.call('/css/app.css')end
This seems to work. I'd definitely call it an ugly hack though. Is there a better or clever-er way? I'm willing to restructure the widget and where it fits into my application, if that enables a better solution.
The text was updated successfully, but these errors were encountered:
Update: I've found a slightly more elegant way around this problem by passing the Sinatra application instance as context to the view:
# in Sinatra appget'/'doFooWidget.new(:ctx=>self).to_htmlend# in widgetdefcontentlink:rel=>'stylesheet',:href=>@ctx.to('/css/app.css')end
Would be better if I could just inject the entire calling context into the view, but I doubt that's feasible since an Erector view is a class itself and has its own context. Other suggestions appreciated.
Background: Sinatra 1.4.5, Erector 0.10.0, Ruby 2.1.2
I'm using Erector in a Sinatra app. Rather than creating a Tilt registration for Erector, I decided to just declare some Widgets in other ruby files:
Then I require the widget in my main sinatra application, and when I want to render a FooWidget, I make an instance of it and
to_html
it:Now what I want to do is invoke Sinatra helpers in the body of the FooWidget. For example, I might have some CSS file in my FooWidget, being linked to via some path
/css/app.css
. But this might fail if my app was actually hosted atexample.com/myapp
. I want to use theto
helper instead. So somehow, I need to get theto
helper into the widget.The heart of the problem (at least it looks that way to me) is that the
to
helper has to be evaluated in the context of the sinatra app instance, so my current solution is to wrap the helper in a Proc and pass it to the widget like this:This seems to work. I'd definitely call it an ugly hack though. Is there a better or clever-er way? I'm willing to restructure the widget and where it fits into my application, if that enables a better solution.
The text was updated successfully, but these errors were encountered: