Skip to content

v3.1.0.rc1

Pre-release
Pre-release

Choose a tag to compare

@timriley timriley released this 27 Nov 13:11
v3.1.0.rc1
dd02c63

Added

  • Add expose! and decorate! to define eager exposures. Eager exposures run before any template renders, along with the exposures they depend on. Use them when an exposure's errors must raise before rendering starts (such as a not-found error), or when an exposure must run even if no template reads it. expose! is a shorthand for the new eager: true exposure option, which you can also give to expose, decorate and private_expose. (@timriley in #289)

  • Add Hanami::View::Locals, which holds a view's locals and resolves them lazily. This is available via Rendered#locals as well as locals or _locals inside templates. It replaces what was previously a plain hash. To access members, it provides #[] and #fetch (resolve one local), #key? (resolves nothing), and #to_h (resolves every local). (@timriley in #289)

  • Raise Hanami::View::CyclicExposureError when exposures depend on each other in a cycle. The error names the cycle. Views check for cycles when they are initialized. (@timriley in #289)

  • Templates and partials can declare the locals they take with a magic comment, such as <%# locals: (title:, subtitle: nil) %> in ERB, -# locals: (title:) in Haml, or /# locals: (title:) in Slim. Rendering raises an ArgumentError for a missing or unknown local and fills in defaults. A template declaring its locals resolves only the exposures it declares, unless it takes the rest with **. The declaration must be a comment in the template's own language, and ends at its matching parenthesis, skipping any inside quoted defaults. (@parndt in #290)

    To opt out, set config.renderer_options = {extract_fixed_locals: false}. Setting it to true also turns on Tilt's own extraction for other template engines.

Changed

  • Exposures are now lazy. An exposure runs only when a template (or another exposure) first reads it, and its value is kept for the rest of the render. Exposures not read by the template are never run. If an exposure has side effects, or other code depends on the order exposures run in, define it with expose! to keep the previous behavior. (@timriley in #289)
  • Scope#_locals and the locals shortcut in templates now return a Hanami::View::Locals instead of a Hash, for a view's template and layout. Call locals.to_h if you need a Hash. Partials rendered with locals still receive a Hash. (@timriley in #289)
  • Splatting a template's locals (e.g. render("item", **locals)) now raises a TypeError rather than implicitly converting locals into a Hash, which would result in every local being resolved at once. Instead, pass only the specific locals you need (e.g. render("item", item: book, title:)), which resolves only those exposures. To keep the previous behavior, splat after explicitly converting the locals to a hash, e.g. render("item", **locals.to_h). (@timriley in #289)
  • Rendered#locals now returns the same Hanami::View::Locals the template received, instead of a Hash. Reading a local from it resolves only that exposure, as does Rendered#[]. Call rendered.locals.to_h if you need a Hash, which resolves every exposure. Code that treats rendered.locals as a Hash (such as RSpec's include matcher) needs .to_h added. (@timriley in #289)
  • Rendered#== now compares rendered string output only. It no longer compares locals. This is so that inspecting or comparing a Rendered does not resolve exposures that rendering skipped. To compare locals, compare rendered.locals directly, e.g. rendered.locals == {title: "Hi"}, which resolves every exposure. (@timriley in #289)
  • Existing locals: magic comments, such as those kept from a Rails app, are now enforced. To keep the previous behavior, set config.renderer_options = {extract_fixed_locals: false}. (@parndt in #290)
  • Require Tilt 2.6 or later, the first version supporting fixed locals. (@parndt)

Fixed

  • Support the leading <%- trim marker in ERB tags, treating it the same as <% (as Erubi does). Previously the - was left in the generated Ruby code, causing a syntax error. A <%-# locals: (...) %> magic comment is also now recognised. (@hmaddocks in #288)
  • Parts initialized without a name: now default to a Symbol name rather than a String. This means part.render("partial") makes the part available in the partial under its inferred name, rather than raising a NameError. (@parndt in #292)
  • token_list (and its class_names alias) now removes duplicate tokens and excludes false values, as its documentation describes. Previously token_list("foo", "foo bar") returned "foo foo bar", and false was included as the token "false". (@parndt in #292)
  • Building a scope by name with no scope_namespace configured now raises Hanami::View::UndefinedConfigError, naming the scope and how to fix it. Previously this failed with NoMethodError: undefined method 'const_defined?' for nil. (@parndt in #293)

Compare v3.0.3 ... v3.1.0.rc1