CodeIgniter Forums
pre_render() event - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: pre_render() event (/showthread.php?tid=69455)



pre_render() event - dannywebdev - 11-26-2017

It would be great if there was a pre_render () event, so you could inject data into the view automatically. As for example, data referring to the session.


RE: pre_render() event - PaulD - 11-27-2017

EDIT: Oops, sorry, did not realise this was for CI4, my answer is for CI3, my bad :-(

You can refer to session data directly in the view, so in a header for instance you can check if a session username is set and show it or show a log in button, for instance.

You can also fetch a view, with data sent to it like normal, and have the fully rendered HTML passed back as a string. Then you can 'inject' whatever you want, whatever 'inject' means.

PHP Code:
$html $this->load->view('home'$dataTRUE); // third option returns view as string. 

Is this what you meant by 'pre-render'?

Paul.


RE: pre_render() event - kilishan - 11-27-2017

That's an interesting suggestion. I have a feeling this might have been prompted by watching my failed attempt to do something similar in the repo for validation errors. Smile I was having a brain-fart at that moment, though, and simply using session('key') worked out pretty well, though might not so well in the parser.

I can also see this being very handy for other site-wide things you might want to make available, like things for the HTML meta tags, etc.

The biggest problem I can see is the event would be ran for every nested view, also, and quickly become a performance hit. I'll have to think on a good solution for this, but it's definitely a good idea.


RE: pre_render() event - InsiteFX - 11-28-2017

The events class should be handling this using event triggers in the view.

But this would need to be checked in the core class methods and for firing the triggers.


RE: pre_render() event - dannywebdev - 12-01-2017

I was doing an authentication system and I found that in each controller I have to pass user data, roles, etc. That's why I get the idea of an event that does it automatically in each render. Try to make the event, but run up to five times per render, it is not efficient.