Skip to content

Centralized conditional RockerContent rendering ? #134

@albert-kam

Description

@albert-kam

I am having a common situation where a page template is using a common layout template for generating a html page.
The page template example:

@args(MyObj obj, MyError error)
  @Rocker.template(MY_LAYOUT, error) {
    <p>@obj.getSomething()</p> --- nullpointer if obj is null
  }

My requirement is that if there are error objects in the parameters, i do not want to render the page template body as the required parametes could be null, and let the layout template display the error messages.

The layout template example:

@args(MyError err, RockerBody content)
  @if (err.hasErrors()) {
    <ul>@renderErrors(obj)</ul>
  } else {
    @content
  }

The problem here is, the RockerBody of the page must be rendered first, before sending the rendered content to the layout template.

I could use @if in the page template to check for the existence of the error objects, but this means lots of duplicated @if in all pages that reuse this template layout.
Furthermore all extrajs and extracss RockerContent-s would need to have this conditioning too.

Workaround on the page template example:

@args(MyObj obj, MyError error)
  @Rocker.template(MY_LAYOUT, error) {
    @if (!err.hasErrors()) { // works fine, but code duplications in many other page templates
      <p>@obj.getSomething()</p> --- nullpointer if obj is null
    }
  }

Is there any way i could render a section (RockerBody or RockerContent) conditionally from the layout template without having to specify @if manually in the page templates ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions