Discussion:
[Mako] most MAKO-nic way to add indentation?
Stefaan Himpe
2011-12-15 22:36:53 UTC
Permalink
Hello all,

With a template like this:

if condition:
....${code_body}

where code_body will be replaced with a multiline string,
is there a way to have the complete code_body use the indentation as
defined in the template?

Example:

code_body = "statement1\nstatement2\nstatement3\n"

instead of this result

if condition:
....statement1
statement2
statement3

i would like to get:

if condition:
....statement1
....statement2
....statement3

Am I overlooking the obvious?

Best regards,
Stefaan.
--
You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
To post to this group, send email to mako-***@googlegroups.com.
To unsubscribe from this group, send email to mako-discuss+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.
Michael Bayer
2011-12-17 02:42:15 UTC
Permalink
Post by Stefaan Himpe
Hello all,
....${code_body}
where code_body will be replaced with a multiline string,
is there a way to have the complete code_body use the indentation as defined in the template?
code_body = "statement1\nstatement2\nstatement3\n"
instead of this result
....statement1
statement2
statement3
....statement1
....statement2
....statement3
Am I overlooking the obvious?
give yourself a filter:

<%!
def wrap(t):
return t.replace("\n", "\n ")
%>

${code_body | wrap}
--
You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
To post to this group, send email to mako-***@googlegroups.com.
To unsubscribe from this group, send email to mako-discuss+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.
Stefaan Himpe
2011-12-17 07:13:35 UTC
Permalink
Post by Michael Bayer
<%!
return t.replace("\n", "\n ")
%>
${code_body | wrap}
Hello Michael,

Thanks for answering.

Your proposed solution works well for a fixed indentation,
but it requires a different filter for each indentation level.

To handle different indentation levels, one could create a filter
generator (a function that takes a parameter indentation, and that
returns a function that adds that specific amount of indentation to a
given string), but that still requires that one counts the desired
indentation, which may not be very copy-paste/reindent/template
refactoring friendly.

For now I'm happy with the following solution that automatically
finds out the desired indentation from the template:

%for line in code_body.splitlines():
....${line}
%endfor

Best regards,
Stefaan.
--
You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
To post to this group, send email to mako-***@googlegroups.com.
To unsubscribe from this group, send email to mako-discuss+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.
Loading...