Override the content of the Page before sending to browser

I want to be able to override, make some changes to the Page context/text before it is shown in the browser to the user.

Example: When editing Page content I want to be able to do the following:

<p>Etiam ac faucibus augue, a imperdiet quam. Curabitur vitae ex auctor, pellentesque risus quis, venenatis nisl. Ut fringilla dui at aliquet lobortis.</p>
{{custom_tag}}
<p>Praesent feugiat mi quis odio lobortis, eu luctus lorem tristique. Nam eu molestie orci. Aenean venenatis turpis at sapien congue dictum. Phasellus maximus vel sapien ut vestibulum.</p>

I want to filter out {{custom_tag}}, and add my custom HTML to replace it.

Two potential options:

If you have the view template under your own control then you can move the replacement logic into the browser view for doing the replacement on the fly.

1 Like

No idea if it is a good idea, but you could have two fields and use an event subsciber to save 'changed content from field ' in field 2

The reason for doing that t way would (only) be for keeping the original markup also.

I assume it would also be possible to use a custom widget to render. Again: no idea if it is a stupid idea (but it is probably easier to make than a portal transform ( you could do simple search / replace in the widgets 'def render' (?)

Thank you guys for your feedback. For the time-being I ended up creating a behaviour and a viewlet to display my content. Was a bit tight on a schedule.
But the other idea I am playing around in my head is to create a pattern and load additional content using the rest api.

I solved a similar issue not long ago by creating a base class which contained a mapping of variables to methods in the class.

Every time the responsible view was called, I would import and check the text content with the class. This would run through and where there was a matching variable, it would call the mapped method and replace the content with whatever is in the return statement.

Of course, the whole class and sub methods were context-less so it could only be run in the response.

Plone Foundation Code of Conduct