Beforerender event for View class

70 views
Skip to first unread message

Summer White

unread,
Jul 4, 2017, 11:35:44 PM7/4/17
to Fat-Free Framework
I need to do a search and replace on template files before I render them (This is a very special use case that I've ran into).

I noticed the afterrender but no beforerender. So I wrote some code outside the framework just before I render the template.

        if ($f3->MIME == "text/html")
       
{
           
if (!cache::instance()->exists(base::instance()->hash($f3->FILE))) {

                $contents
= file_get_contents(getcwd()."/".$f3->FILE);

                $contents
= preg_replace_callback("/<ckeditor>/", function ($match) {
                   
                    $id
= substr("cid-".md5(uniqid(rand(), true)), 0, 12);

                   
return '<ckeditor id="'.$id.'">';
               
}, $contents);

                file_put_contents
(getcwd()."/".$f3->FILE, $contents);
           
}
       
}

Ignore the two globals MIME and FILE. Those are my own.

The biggest problem with this is that it doesn't apply for inner templates.

So my next step is to add code into view class to accomplish this.

Before I go hurderling in I'd just like to double check that it would be best to add beforerender. I'll do a PR but up to Bong or Ikkez to accept it if they feel its a proper addition to the framework.

ikkez

unread,
Jul 5, 2017, 3:21:01 AM7/5/17
to Fat-Free Framework
why  don't you register <ckeditor> as custom tag with template->extend and put your logic into that directive?

Summer White

unread,
Jul 5, 2017, 4:54:04 AM7/5/17
to Fat-Free Framework
I do extend ckeditor tag. I'd rather not post my whole codebase but please trust me there is a solid reason why I'm searching the template file before it is rendered.

In short though, my <ckeditor> tag needs an id attribute. Much like a primary key. <ckeditor id="maincontent">To be editted</ckeditor>. I've got a plugin for sublime that generates random alphanumeric strings so I can be lazy. But I get sick of hitting the shortcuts so I decided to have php do it for me. And trust me that I've thought long and hard about a way around having a id attribute but its a nessecary evil in my project.

ved

unread,
Jul 5, 2017, 5:09:17 AM7/5/17
to Fat-Free Framework
Hi,

If the html id you need is just a random string and not something that needs an actual value (from db or something) then why don't you just use it directly in the template?

<ckeditor id="{{ substr("cid-".md5(uniqid(rand(), true)), 0, 12) }}">

Or as hinted by @ikkez, generating it in your class that extends/creates the custom tag.
I'm not really getting the point of parsing a compiled template to add an id after the fact instead of just placing it directly in the template or generating class.
I think I'm probably missing something.

ikkez

unread,
Jul 5, 2017, 5:13:59 AM7/5/17
to Fat-Free Framework
I still don't get it why you don't generate the random numbers in the <ckeditor> tag handler and write it from there into the template.

Summer White

unread,
Jul 5, 2017, 8:39:41 PM7/5/17
to Fat-Free Framework
Hey Ikkez and Ved, here is a video explaining the situation. https://youtu.be/fmbLiAD9kIw

Hope that helps :)

ikkez

unread,
Jul 6, 2017, 4:00:15 AM7/6/17
to Fat-Free Framework
thanks for the enlightning video :) well I see the problem.. the id's have to be exactly the same for every instance otherwise it's not possible to match the stored content with the slots in the page.
Well I wrote quite the same system some time ago, but I used to put named IDs into each editor tag - I guess you don't want to care about that and want a collision-free automatic system? Well in that case some sort of "beforerender" would make sense, indeed. And there doesn't seem to be another option to solve this.
Reply all
Reply to author
Forward
0 new messages