Comments (9)
This is the expected and specified behavior. You need to pass layout options via :layout_opts
(e.g. layout_opts:{ locals: { local: 'Local Var' }}
). This allows you to specify different locals to the layout than to the view itself.
I suppose we could consider an option (off by default) to the render plugin that would automatically merge the view template :locals into the layout template :locals. I can probably work on that before the next release.
from roda.
Cool. It is somewhat confusing if you're coming from sinatra or something similiar where the passed variables are available in both the layout and the template.
My specific use case is to have the same value in the title
tag and somewhere on the page.
I saw the layout_opts
option, but didn't realize that locals
can be passed to the layout specifically. Maybe just make it clearer in the docs?
from roda.
I can definitely see where it can cause issues. It's not a problem if you pass data to the view implicitly via instance variables (like sinatra, view scope is the same as route scope), but some people prefer the more explicit approach by specifying locals.
I'll improve the docs as well, but I do think it would be good to support using :locals for both the view template and layout template via an option.
from roda.
How about :locals
being automatically passed to the layout template, and any locals specified in :layout_opts
would be merged with :locals
, overriding variables with the same name? I think this would satisfy both requirements, and wouldn't bring any backwards incompatibility.
from roda.
That's basically what the option would do. It can't be the default behavior (at least not till Roda 3), as it would affect backwards compatibility.
With this option, here is how I'm planning to populate locals, in terms of priority (least to greatest):
- Plugin :locals
- Plugin :layout_opts => :locals
- method :locals
- method :layout_opts => :locals
from roda.
Sounds great.
from roda.
Thanx!
Further to this, I see that the locals passed to a template / layout aren't inherited by partials either. Is this intentional, or an omission?
The workaround is to obviously pass the locals to the partial call:
<%= partial('navbar', locals: { title: title }) %>
from roda.
That is intentional. Note that templates are cached per combination of locals, so just blinding merging locals into partials could lead to increased memory usage (possibly combinatoric memory usage in edge cases with nested partials).
You could probably use the view_options plugin, which allows you to set locals in one place and use them for all template renders.
from roda.
Cool, thanx, looks useful.
from roda.
Related Issues (20)
- Envrionments plugin: provide instance methods too? HOT 1
- How can i load coffeescript in roda HOT 2
- How to use common_logger plugin to log information in routes implementation or in specs or other place in the application? HOT 6
- [feature request] Make the view method (from the render plugin) accept a block like the render method does HOT 1
- route_csrf incompatible with rack-protection authenticity token checks HOT 5
- Unfork HOT 3
- env['rack.errors'] parsed as a link in common_logger plugin documentation HOT 2
- String matcher in hash_routes combined with unescape_path does not match non-ascii strings HOT 1
- [Documentation / Question] Request for a FAQ for experienced sinatra users HOT 1
- Raises "Unsupported block result" when using Unreloader and json plugin HOT 2
- On app startup. Getting message > Rack::Handler is deprecated and replaced by Rackup::Handler HOT 2
- Not compatible with Haml >= 6 HOT 2
- URI Unicode handling HOT 6
- Duplicating Content-Type header when header key comes in lowercase HOT 3
- json plugin does not support FalseClass and String anymore HOT 1
- Can gRPC services and clients be integrated?
- Unicode escapes not decoded in path HOT 3
- Sanitize params when using plugin `error_mail` HOT 2
- Missing rackup on clean install HOT 4
- Bundler, Zeitwerk & Roda Plugins HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roda.