Calling user defined function, get unexpected newline ?

42 views
Skip to first unread message

zhengx...@163.com

unread,
Sep 3, 2018, 9:15:26 AM9/3/18
to Mako Templates for Python
I have following template, I defined a function "get_config_param" and call it at following lines, after rendering the template, there is a unexpected newline in the output. What i want is:
`define MAX_LEN_WIDTH 12
but what I got is:
`define MAX_LEN_WIDTH
12

>>>>>>>>>> template begin >>>>>>>>>>>>>>>>>>>>>>
<%def name="get_config_param(param_name)">
<%
    import re
    find_flag = 0
    line = ""
    pattern = "^\s*`define\s+%s\s+(\d+)" % param_name
    f = open("ring_define.tv", "r")
    for line in f.readlines():
        m = re.search(pattern, line)
        if m:
            return (int(m.group(1)))
            find_flag = 1
    if(find_flag == 0):
        print("Error: not find param %s in ring_define.tv" % param_name)
        return(0)
%>
</%def>

<% import math %>
//** golobal

`define MAX_LEN_WIDTH ${(int(math.log(get_config_param("AXI_IN_ID_WIDTH"), 2)))}

<<<<<<<<<<<<<< template end <<<<<<<<<<<<<<<<<<<<<<<<<<<


Mike Bayer

unread,
Sep 22, 2018, 6:53:03 PM9/22/18
to mako-d...@googlegroups.com
you want to put backslashes in your def, something like:

<%def ...>\
<%
...
%>\
</%def>

because otherwise you have newlines in there
> --
> You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mako-discuss...@googlegroups.com.
> To post to this group, send email to mako-d...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mako-discuss.
> For more options, visit https://groups.google.com/d/optout.

George DeLaJungla

unread,
Mar 30, 2020, 2:55:41 PM3/30/20
to Mako Templates for Python

Is there any alternative to include python functions (not rendering anything, just calculating and returning something) from another file without doing the following?

<%def ...>\
<%
   ...
%>\
</%def>

<%
def...
%>

or

<%!
def

%>

do not work.

El diumenge, 23 setembre de 2018 0:53:03 UTC+2, Mike Bayer va escriure:
> To unsubscribe from this group and stop receiving emails from it, send an email to mako-d...@googlegroups.com.

Mike Bayer

unread,
Mar 30, 2020, 3:16:19 PM3/30/20
to mako-d...@googlegroups.com
you can omit the newlines explicitly:

<%def><%



%></%def>
To unsubscribe from this group and stop receiving emails from it, send an email to mako-discuss...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages