Sorry, we don't support your browser.  Install a modern browser
This post is closed.

Before/after render hooks#342

I think it’d be useful to have a hook before a page is selected to be rendered and after its final HTML is generated. Somewhere around here in the core.

The render:before hook use case

You can render another page as a variant. For example, we want to make A/B tests of a page. We have /my-page and /my-page-v2, but we don’t want the variant to actually have a different URL because once we finish the test - the link will be gone. We want to trigger the variant with /my-page?v=2. That’s possible with a custom route, but is a bit weird to do. Also, the /my-page-v2 would still be accessible, unless it’s a draft, which can also complicate things.

With a render:before hook, you can check if the page that the router selected has a variant (we have a custom field for that in our code) and if it has - return another page to render instead. In that same hook, you can check if the selected page by the router is a direct link to the variant (/my-page-v2) and return a 404. It solves both problems!

The render:after hook use case

Once the page’s HTML is generated, it can be useful to make global substitutions. For example, you can use the replace() field method, buy you have to make sure you call it everywhere. Also, there might be places where you can’t call it (e.g. HTML attributes generated by the editor). You can’t know where the user would want to put {​{​ page.title }}, so it’d be great if you have a single catch-all solution.

With a render:after hook, you can have a simple regex match {​{​ ... }} and make the substitution. It’ll work everywhere in the document, regardless of where it comes from, because it operates on the final HTML.

3 years ago
Merged into More hooks to more pluggable#419
3 years ago