rocker
rocker copied to clipboard
Centralized conditional RockerContent rendering ?
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 don't think some of your examples aren't valid rocker templates, I'm guessing you missed some syntax? I'm also actually having a tough time following which is what in your examples and why you'd need an @if in everything. If you simply consolidate your logic into a single template you call from the others, not sure why you think you need conditional logic everywhere. If you can consolidate your examples into something a little easier to follow, with exact syntax, happy to help.
On Thu, Feb 27, 2020 at 1:59 AM albert-kam [email protected] wrote:
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) {
@obj.getSomething()
--- 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 content as the required parametes could be null, and let the layout template display the error messages.
The layout template example:
@args(MyError err, RockerContent content) if (err.hasErrors()) {
@renderErrors(obj)
} else { @content }The problem here is, the RockerContent of the page must be rendered first, before sending the content to the layout template.
I could use @if https://github.com/if in the page template to check for the existence of the error objects, but this means lots of duplicated @if https://github.com/if in all pages that reuse this template layout. Furthermore all extrajs and extracss RockerBody 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
@obj.getSomething()
--- nullpointer if obj is null } }Is there any way i could render a section (RockerBody-s and RockerContent-s) conditionally from the layout template without having to specify @if https://github.com/if manually in the page templates ?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/fizzed/rocker/issues/134?email_source=notifications&email_token=AAEPAAVIQYI73D4IMTBDM3TRE5QE5A5CNFSM4K4U6VG2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IQV6YFA, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEPAAR5MPR2U7RUEUZ7HM3RE5QE5ANCNFSM4K4U6VGQ .
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 ..