fizzed/rocker

Centralized conditional RockerContent rendering ?

albert-kam opened this issue · 2 comments

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 ?

I deeply apologize for the confusion. Allow me to make it simpler this time.

The steps of executions:


(0) Backend has validation errors, 
    populating parameter map variables with null values and 
      a validationResult object containing the error messages. 
    Why null values ? Because of errornous inputs, we cannot return correct parameters.
(1) MyPage.rocker.html getting rendered, 
    accepting the null variables and validationResult as args.
(2) MyPage.rocker.html calls MyLayout.rocker.html: 
    (2.1) Renders the content
    (2.2) Passes the rendered content and a validationResult as args to MyLayout
(3) MyLayout renders: 
    (3.1) If validationResult has no errors, 
          then writes the rendered content from step 2
    (3.2) If validationResult has errors, then write down the error messages

The key issue is at (2) because it has to render the content before passing it to MyLayout.rocker.html. The rendering fails because the parameters needed to render the MyPage content are of null values as returned by the backend.

The expectation is that upon this validationError situation, the MyLayout displays the error messages, without even rendering the MyPage.html content.

How do i do this without having to do null or validationResult checking in MyPage.rocker.html and delegate the checking and displaying of error messages to MyLayout.rocker.html ?

Thank you ..