I'm unable to use `for blocks` in custom print formats

I’m trying to add the given custom HTML section in my print formats, but have something parsing my HTML, and breaking the template!

{% set sub_tables = frappe.get_list('DocField', fields=['fieldname', 'options', 'label'], filters={'parent': doc.name, 'fieldtype': 'Table'}) %} 

<table class="table table-bordered">
    <tbody>
{% for table in sub_tables %}
        <tr>
            <td>{{ table["fieldname"] }}</td>
            <td>{{ table["options"] }}</td>
            <td>{{ table["label"] }}</td>
        </tr>
{% endfor %}
    </tbody>
</table>

<!-- markdown -->

After the sanitization I’m getting it

{% set sub_tables = frappe.get_list('DocField', fields=['fieldname', 'options', 'label'], filters={'parent': doc.name, 'fieldtype': 'Table'}) %} 

<table class="table table-bordered">
    <tbody>
        <tr>
            <td>{{ table["fieldname"] }}</td>
            <td>{{ table["options"] }}</td>
            <td>{{ table["label"] }}</td>
        </tr>
    </tbody>
</table>
{% for table in sub_tables %}{% endfor %}

<!-- markdown -->

And this is chashing my templates!

1 Like

@max_morais_dmm what is the trace?

@rmehta

Traceback (innermost last):
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/app.py", line 60, in application
    response = frappe.api.handle()
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/api.py", line 50, in handle
    return frappe.handler.handle()
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/handler.py", line 19, in handle
    execute_cmd(cmd)
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/handler.py", line 36, in execute_cmd
    ret = frappe.call(method, **frappe.form_dict)
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/__init__.py", line 806, in call
    return fn(*args, **newargs)
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/templates/pages/print.py", line 148, in download_pdf
    html = frappe.get_print(doctype, name, format)
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/__init__.py", line 1022, in get_print
    html = build_page("print")
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/website/render.py", line 127, in build_page
    context = get_context(path)
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/website/context.py", line 16, in get_context
    context = build_context(context)
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/website/context.py", line 54, in build_context
    ret = module.get_context(context)
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/templates/pages/print.py", line 37, in get_context
    no_letterhead=frappe.form_dict.no_letterhead),
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/templates/pages/print.py", line 121, in get_html
    html = template.render(args, filters={"len": len})
  File "/usr/frappe5/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/frappe5/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/./templates/print_formats/standard.html", line 28, in top-level template code
    {{ render_field(df, doc) }}
  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/./templates/print_formats/standard_macros.html", line 5, in template
    
{{ frappe.render_template(df.options, {"doc": doc}) or "" }}

  File "/usr/frappe5/frappe-bench/apps/frappe/frappe/utils/jinja.py", line 41, in render_template
    return get_jenv().from_string(template).render(context)
  File "/usr/frappe5/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/frappe5/frappe-bench/env/local/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "

See the trace in your bench console… we need to fix this since its an html trace

@rmehta

22:59:25 web.1  | Error on request:
22:59:25 web.1  | Traceback (most recent call last):
22:59:25 web.1  |   File "/vagrant/frappe-bench/env/lib/python2.7/site-packages/werkzeug/serving.py", line 194, in run_wsgi
22:59:25 web.1  |     execute(self.server.app)
22:59:25 web.1  |   File "/vagrant/frappe-bench/env/lib/python2.7/site-packages/werkzeug/serving.py", line 185, in execute
22:59:25 web.1  |     write(data)
22:59:25 web.1  |   File "/vagrant/frappe-bench/env/lib/python2.7/site-packages/werkzeug/serving.py", line 153, in write
22:59:25 web.1  |     self.send_header(key, value)
22:59:25 web.1  |   File "/usr/lib/python2.7/BaseHTTPServer.py", line 401, in send_header
22:59:25 web.1  |     self.wfile.write("%s: %s\r\n" % (keyword, value))

But the issue, is the HTML is being sanitized by the HTML editor

This was fixed by changing the fieldtype as “Code” for custom code Print Format.

@anand, I’ll check! You mean the field format_data? I’m using the Print Format Builder!

I presume this is not fixed. right? @max_morais_dmm were you able to find a work around for this issue? I’m trying to get tax break up to be printed in print format…

@kirthi, it is not fixed! Is a issue with print formats in Print Format Builder

I create standards print formats, using jinja templates! Choosing the format of the print format for Server

Thanks @max_morais_dmm