Repository navigation
v3.1.0.rc1
Pre-releaseAdded
-
Add
expose!anddecorate!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 neweager: trueexposure option, which you can also give toexpose,decorateandprivate_expose. (@timriley in #289) -
Add
Hanami::View::Locals, which holds a view's locals and resolves them lazily. This is available viaRendered#localsas well aslocalsor_localsinside 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::CyclicExposureErrorwhen 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 anArgumentErrorfor 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 totruealso 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#_localsand thelocalsshortcut in templates now return aHanami::View::Localsinstead of a Hash, for a view's template and layout. Calllocals.to_hif 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 aTypeErrorrather 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#localsnow returns the sameHanami::View::Localsthe template received, instead of a Hash. Reading a local from it resolves only that exposure, as doesRendered#[]. Callrendered.locals.to_hif you need a Hash, which resolves every exposure. Code that treatsrendered.localsas a Hash (such as RSpec'sincludematcher) needs.to_hadded. (@timriley in #289)Rendered#==now compares rendered string output only. It no longer compares locals. This is so that inspecting or comparing aRendereddoes not resolve exposures that rendering skipped. To compare locals, comparerendered.localsdirectly, 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, setconfig.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 meanspart.render("partial")makes the part available in the partial under its inferred name, rather than raising aNameError. (@parndt in #292) token_list(and itsclass_namesalias) now removes duplicate tokens and excludesfalsevalues, as its documentation describes. Previouslytoken_list("foo", "foo bar")returned"foo foo bar", andfalsewas included as the token"false". (@parndt in #292)- Building a scope by name with no
scope_namespaceconfigured now raisesHanami::View::UndefinedConfigError, naming the scope and how to fix it. Previously this failed withNoMethodError: undefined method 'const_defined?' for nil. (@parndt in #293)