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?
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.
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 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 #>.
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..