Thoughts on HTML / Python template delimiters

Working on creating a small templating type engine to allow mixing of HTML and Python code and considering what to use as the delimiters between each. Beginning suggestion is:

HTML: ← begin HTML block

<% Python expression %>    <- inline Python code

Python: ← begin Python code block

<# HTML text #>     <- output HTML with newline
<#= HTML text #>    <- output HTML inline (no newline)
? "HTML text"       <- output HTML whole line
?? "HTML text"      <- output HTML whole line (on same line)

Are there more suitable ‘Pythonic’ delimiters which I should be using?

Have you come across Jinja 2 and Pyscript ?

If you are targeting HTML documents only, my personal preference is for a syntax like Chameleon’s. The advantage, for me, is that the template still looks like a HTML document and HTML syntax highlighters handle it quite well. It was a while ago, but I recall that Chameleon simply felt better compared to something like Jinja. But, of course, it seems like Jinja’s syntax won the popularity contest a long time ago already, and many tools have adopted Jinja itself or have built template engines using a syntax similar to Jinja’s.

Mako is another template engine with its own syntax.

f"""
<title>{title}</title>
"""

For simple stuff, this is one option!

I was shying away from {} because they are used in Javascript so could potentially cause confusion when Javascript code exists in the HTML string.

Edit:
However, guess I could use {{ Python expression }} to output HTML from a Python expression which would be similar to Django.

Unfortunately I don’t seem to be able to edit the original post so repeating here with edit
to include {{ }} tags and separate, more Pythonic (same as Django syntax), Python code tag {% %}

HTML: ← begin HTML block

{% Python code %}    <- Python code
{{ Python expression }}    <- inline Python expression

Python: ← begin Python code block

<# HTML text #>     <- output HTML with newline
<#= HTML text #>    <- output HTML inline (no newline)
? "HTML text"       <- output HTML whole line
?? "HTML text"      <- output HTML whole line (on same line)

However, from what I can see, there don’t seem to be any standard delimiters to adopt for outputting to HTML stream from within Python code block for which I am currently using <# HTML text #> & <#= HTML text #>.

I do not understand what that means.

From a line in a Python program adding some HTML, say ‘<td></td>’, to the outgoing HTML stream.

This is just the jinja2 syntax. If you’re going down this road, I’d suggest reading their codebase and doing some parallel development, then proposing upstream changes with them since they’re the defacto standard (Django).

I’m not particular to jinja2 btw, I just think trying to maintain and advance existing patterns is good unless you have a revolutionary idea that requires a whole new system is all.

Yep, not wanting to go against the defacto standards so trying to adopt similar delimiters. However jinja2 doesn’t seem to have any delimiters in use for outputting code directly from within a block of Python code hence my suggestion to use <#= #>, <# #>, ?, ??, unless anyone can suggest more appropriate delimiters for this..

Ahhh I see, in that case have you checked out pyjsx? It’s another one that tries to bring the template and the code closer together.

Again not trying to say don’t do what you’re doing, but definitely take a look at prior art.

You may want to see also cog from Ned Batchelder, which addresses a PHP use case using Python: Cog — cog 3.6.0 documentation