Git Product home page Git Product logo

Comments (9)

jeremyevans avatar jeremyevans commented on June 21, 2024

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.

jrgns avatar jrgns commented on June 21, 2024

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.

jeremyevans avatar jeremyevans commented on June 21, 2024

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.

janko avatar janko commented on June 21, 2024

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.

jeremyevans avatar jeremyevans commented on June 21, 2024

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.

jrgns avatar jrgns commented on June 21, 2024

Sounds great.

from roda.

jrgns avatar jrgns commented on June 21, 2024

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.

jeremyevans avatar jeremyevans commented on June 21, 2024

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.

jrgns avatar jrgns commented on June 21, 2024

Cool, thanx, looks useful.

from roda.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.