Allow control statements (if, else, endif, etc.) to be closed so they can be inlined
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 459
- Forks
- 90
- PR merge metrics
- No merged PRs in 30d
Description
Migrated issue, originally created by Anonymous
In line with the discussion here:
http://groups.google.com/group/mako-discuss/browse_thread/thread/835ab6db0bb0cdb4
The pertinent parts quoted:
However, if the case is that Jinja2's (% if <expr> %}, because it has a
closing %} allows it to be inlined like {% if <expr> %} <html> {% else %}
<html> {% endif %}, then I'd want to think a bit bigger here. I'd want to
go into match_control_line() as we did and just find some way to make the
newline optional:
% if <expr>:\n
<html>\n
% else:\n
<html>\n
% endif\n
we could perhaps just co-opt the { } verbosity:
{% if <expr>:} <html> {% else:} <html> {% endif}
or just rip them off totally:
{% if <expr> %} <html> {% else %} <html> {% endif %}
either of the above would require a rewritten (either in-place or additional
version of) match_control_line(), as we'd be doing the same kind of matching
which we do in match_expression(), which is that we need to make use of
parse_until_text() so that we properly skip over anything that might be
Python code. I'd want the two formats to be completely interchangeable:
% if expr:
<html> {% else %} <html>
% endif
{% if <expr> %} <html>
% elif <expr>:
<html>
% else:
<html> {% endif %}
just some ideas but I'd need someone to be motivated to help patch out
lexer.py and add new tests as well.
I've frequently used Python's native 1 if 1 else 0 syntax, but it doesn't always seem ideal.
This is the only thing I can think of that other template languages can do that I wish Mako had.
Thank you for making Mako.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lexer.py, especially match_control_line() and the parsing around parse_until_text(), as identified in the issue. Review the existing control-line tests, then define tests for interchangeable multiline and inline if/else/elif/endif forms; done means inline control statements parse and render correctly without breaking the existing syntax.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100