Skip to content
This repository has been archived by the owner on Jan 7, 2019. It is now read-only.

Use Jinja2's line statement features in our templates #6

Closed
ekiwi opened this issue May 19, 2013 · 9 comments
Closed

Use Jinja2's line statement features in our templates #6

ekiwi opened this issue May 19, 2013 · 9 comments

Comments

@ekiwi
Copy link
Member

ekiwi commented May 19, 2013

One thing that has always bugged me about the code produced by our templates is, that:

  • either there are indentations missing (i.e. if -%} was used as a closing tag)
  • or there are some seemingly random newlines (i.e. if only %} was used)

The result that would imho best fit our style of template coding (i.e. putting jinja2 statements in a separate line) is if these lines would just be left out in the file resulting from our template.
One way I found that makes exactly this behavior possible is to use the Jinja2 Line Statements. To use this we only need to add something like this:

loader.line_statement_prefix = 'some characters that never appear at the beginning of a line in cpp'
loader.line_comment_prefix = 'some other characters with the same properties'

to the scons/site_tools/template.py file around line 80.
This change does not affect any of our existing templates it only adds the possibility to use line statements which result imho in nicer output files in our template files.

The reason I didn't just commit this to our develop branch is that I would like to hear other people's opinion on what to use as a line_statement_prefix. Personally I like '{%' which resembles the regular block statements but would also appreciate only one character if we can find one that must not be used in a new line in Cpp.

@georgi-g
Copy link
Member

i don't think it is a good idea introducing additional syntax. The
templates are hard to read. The one you can be sure now is, that
executing parts of the file are within those braces { }. I would not
like to miss this feature.

Using the minuses '-' you can adjust removing white characters before or
after statements and formatting the output. Unfortunately you have to
experiment a while for achieve purposed output format.

Am 19.05.2013 14:01, schrieb Electronic Kiwi:

One thing that has always bugged me about the code produced by our
templates is, that:

  • either there are indentations missing (i.e. if -%} was used as a
    closing tag)
  • or there are some seemingly random newlines (i.e. if only /%}/ was
    used)

The result that would imho best fit our style of template coding (i.e.
putting jinja2 statements in a separate line) is if these lines would
just be left out in the file resulting from our template.
One way I found that makes exactly this behavior possible is to use
the Jinja2 Line Statements
http://jinja.pocoo.org/docs/templates/#line-statements. To use this
we only need to add something like this:

|loader.line_statement_prefix = 'some characters that never appear at the beginning of a line in cpp'
loader.line_comment_prefix = 'some other characters with the same properties'
|

to the scons/site_tools/template.py file around line 80.
This change does not affect any of our existing templates it only
/adds/ the possibility to use line statements which result imho in
nicer output files in our template files.

The reason I didn't just commit this to our develop branch is that I
would like to hear other people's opinion on what to use as a
line_statement_prefix. Personally I like '{%' which resembles the
regular block statements but would also appreciate only one character
if we can find one that must not be used in a new line in Cpp.


Reply to this email directly or view it on GitHub
#6.

@ekiwi
Copy link
Member Author

ekiwi commented May 19, 2013

Using the minuses '-' you can adjust removing white characters before or after statements and formatting the output.

This (while not being very intuitive) works most of the time.
But for example with my test file I haven't found any way using the '-' to prevent jinja2 from either indenting the last closing '}' bracket or putting the bracket behind the last statement (if you use a '-' at the beginning of the last jinja2 instruction).
If you want to try it yourself you can just clone the repository and run scons.

@dergraaf
Copy link
Member

i don't think it is a good idea introducing additional syntax.

I would agree with this but i think if we could simplify the creation of the templates it is worth it. As you already pointed out it is hard to get the whitespaces right the way it's currently done. When this gets easier with line statements i would vote for using them. A lot of the templates are already in a matching style with one statement per line.

@georgi-g
Copy link
Member

Am 19.05.2013 23:49, schrieb Electronic Kiwi:

Using the minuses '-' you can adjust removing white characters
before or after statements and formatting the output.

This (while not being very intuitive) works most of the time.
But for example with my test file
https://github.com/ekiwi/jinja2-playground/blob/master/test.cpp.in I
haven't found any way using the '-' to prevent jinja2 from either
indenting the last closing '}' bracket or putting the bracket behind
the last statement (if you use a '-' at the beginning of the last
jinja2 instruction).
If you want to try it yourself you can just clone the repository and
run scons.

I have also no solution for that.
Beside this I see a chance that readability of templates could increase
with one line statements.
Do you already know if jinja is able to cope with both one-line
statements and the usual? Your proposal is to use the same characters as
the usual begin with. Does this introduce a conflict?

@ekiwi
Copy link
Member Author

ekiwi commented May 20, 2013

Do you already know if jinja is able to cope with both one-line statements and the usual? Your proposal is to use the same characters as the usual begin with. Does this introduce a conflict?

I tried it with '{%' as the line statement character in a mixed file (line statements and regular statements) and it did not work:

 {% if 42 is prime -%}
 TemplateSyntaxError: unexpected '%'

But if you change the line statement character to something else, even like '{%%' it works for me on a mixed template file.

@dergraaf
Copy link
Member

Electronic Kiwi wrote:

I tried it with '{%' as the line statement character in a mixed file
(line statements and regular statements) and it did not work:

{% if 42 is prime -%}
TemplateSyntaxError: unexpected '%'

That's expected as now the "-%}" is trash at the end of the line.

But if you change the line statement character to something else,
even like '{%%' it works for me on a mixed template file.

I would drop the "{" as there is no matching closing bracket.
Perhaps we should use something like "%%" or "##".

@georgi-g
Copy link
Member

Am 20.05.2013 18:13, schrieb Fabian Greif:

Electronic Kiwi wrote:

I tried it with '{%' as the line statement character in a mixed file
(line statements and regular statements) and it did not work:

{% if 42 is prime -%}
TemplateSyntaxError: unexpected '%'

That's expected as now the "-%}" is trash at the end of the line.

But if you change the line statement character to something else,
even like '{%%' it works for me on a mixed template file.

I would drop the "{" as there is no matching closing bracket.
Perhaps we should use something like "%%" or "##".


Reply to this email directly or view it on GitHub
#6 (comment).

we should take a token which is never happen to be a start of any text
document to be generated. % and # are not good since they are often used
as comment characters.

maybe something like ">" or "?"

@ekiwi
Copy link
Member Author

ekiwi commented May 21, 2013

we should take a token which is never happen to be a start of any text document to be generated. % and # are not good since they are often used as comment characters. maybe something like ">" or "?"

'>' and '?' both do not work because something like this is legal in C++:

int a = (b
> c)
? 1 : 0

(Not that this is nice code, but you never know, maybe someone will need to have a line break in front of a '?' or a '>' one day)

I like '%%' because I already associate the '%' character with jinja2 statements. '##' would work as well, but to me it looks too much like a preprocessor macro (and isn't that also used to concatenate strings in the preprocessor?).

@ekiwi ekiwi closed this as completed Jun 1, 2013
@ekiwi ekiwi reopened this Jun 1, 2013
@ekiwi
Copy link
Member Author

ekiwi commented Jun 1, 2013

Closed with commit 22cd192

@ekiwi ekiwi closed this as completed Jun 1, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants