Zope PostgreSQL variable with HTML and DTML

While this isn't Plone related, it was suggested that I post this Zope question here and hopefully someone will be able to show me a good solution.

The details are posted on StackOverflow at https://stackoverflow.com/questions/61622357/zope-postgresql-variable-with-html-and-dtml

However, as Matthew Wilkes replied, this might not be possible using DTML directly (though I hope it is).

The issue at hand is that when a blog article is written, images need to be posted within the article's content in the correct places. In order for Zope to display those images, DTML is used with variables to gather the various folders in the path, as well as the image's filename. Additionally, I designed it so that the dtml-var tag would call a dtml-document which contains both html and dtml code, allowing a site redesign in the future without having to edit every blog post individually.

I am open to creative suggestions and a potential redesign of the workflow, but at this point I do not see how I would ever be able to allow someone to post a blog article with images in the article's content, without storing the dtml code within the article and thus within the db column, which is why I am trying to figure out a way to make Zope render the dtml-var code that the ZSQL Method returns.

Thanks in advance for any and all assistance.

Matthew and I worked together in the #zope channel on Freenode and he came up with a beautiful solution, that is also nice and secure. Hopefully this will be useful to many other in the community, and a huge thank you to Matthew for the help.

Here's his solution:

If you have a fixed number of delegations to DTML methods, writing a Python script, such as:

## Script (Python) "parse_variables"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=post, _
##title=
##
post = post.replace("##POST_IMAGE##", context.postimg(None, _))
return post

And then calling that with your variable that contains the user-supplied data, like <dtml-var expr="parse_variables(data, _)">