Add onBeforeRender and onAfterRender as array of callbacks

6 months ago

bckp
Member | 12
+
0
-

I suggest adding onBeforeRender and onAfterRender as callbacks for Presenter, as startup and shutdown have their callbacks. Another reasson for this is, if I use traits to add lets say Translator, or Ajaxifier for requests, i can`t add some stuff that is called only if render is called by trait, but need to modify presenter.

Right now, if i add trait, i need to modify presenter to know, that some code need to be called in beforeRender, so trait is not possible to use stand-alone without modification of code. Callbacks will solve this, so lets say translatorAware trait can simply have

<?php
injectTranslator(Translator $translator){
    $this->translator = $translator;
    $this->onBeforeRender[] = function()use($translator){
        $this->template->setTranslator($translator);
    }
}
?>

and can be used directly without modifications of configs, without modification of presenter itself… onAfterRender should be added to be consistent.

You can do this by custom presenter prototype, but i belive this should be part of presenter itself.

Last edited by bckp (2019-04-24 14:54)

6 months ago

David Grudl
Nette Core | 6877
+
0
-

onBeforeRender sounds good, onAfterRender seems useless to me.

Maybe it can be just onRender…

6 months ago

bckp
Member | 12
+
0
-

I was thinking about onRender, but there exists methods:
startup and onStartup[]
shutdown and onShutdown[]

that is reason for onBeforeRender and onAfterRender, but honesly, it can be merged into onRender[] and it can be called right after/before beforeRender(). It will solve problem i see for adding translator to templates only if i need to.

onAfterRender can be used to some specific cases, like modifying what snippets are send based on dependencies between them, but that is way to specific for one of my project.

Last edited by bckp (2019-04-24 17:23)

4 months ago

bckp
Member | 12
+
+1
-

OK, created PR
@dg

Last edited by bckp (2019-06-07 12:44)