Custom Macro in standard_macros.html

Hi all,

I have got custom Macros in standard_macros.html since the beginning of v4.0. The transition to v7 was absolutely no problem. Thanks to the developers :slight_smile:

But now one question arises:

we have the add_header function with max_pages and page_num. I want the same function in a custom macro called “add_header2”.

Actually it looks like the following:

{%- macro add_header2(doc, letter_head, no_letterhead, page_num, max_pages) -%}
        {% if letter_head and not no_letterhead %}
        <div class="letter-head">{{ letter_head }}</div>
        {% endif %}


       <table width="100%">
<tr>
	<td width="60%">
		 {% if doc.doctype == "Purchase Order" %}
		 <p style="line-height:145%;">{{ doc.supplier_name }}<br>{{ doc.address_display }}<br>
		<div id="test" style="display:none;"></div>
		</p>
		 {% else %}
		 <p style="line-height:145%;">{{ doc.customer_name }}<br>{{ doc.address_display }}<br>
		<div id="test" style="display:none;"></div>
		</p>
		{% endif %}
	</td>
	<td width="40%">
		<!-- Rechnungsnummer und so -->

		<table style="line-height:105%;" width="100%">
	<tr>
		<td width="50%">Belegnummer</td>
		<td width="50%" style="text-align:right;">{{ doc.name }}</td>
	</tr>



		</table>
	</td>
</tr>
</table>

<br><br>
<table width="100%">
	<tr>
		<td width="50%">
	<div id="Rechnung">
	</div>
  {% if page_num != max_pages %}

		{% if doc.doctype == "Sales Invoice" %}
			<big><big><big><big>Rechnung </big></big></big></big>
		{% elif doc.doctype == "Purchase Order" %}
			<big><big><big><big>Bestellung </big></big></big></big>
		{% elif doc.doctype == "Delivery Note" %}
		<big><big><big><big>Lieferschein </big></big></big></big>
		{% elif doc.doctype == "Sales Order" %}
		<big><big><big><big>Auftragsbestätigung </big></big></big></big>
		{% elif doc.doctype == "Quotation" %}
		<big><big><big><big>Angebot </big></big></big></big>
		{% elif doc.doctype == "Customer Issue" %}
		<big><big><big><big>Kundenreklamation </big></big></big></big>
		{% else %}

		<big><big><big><big>{{ doc.doctype }} </big></big></big></big>
		{% endif %}
    	{% endif %}


		</td>
		<td width="50%" style="text-align:right; valign:top;">
		 {% if doc.doctype == "Purchase Order" %}

		 {% elif doc.doctype == "Customer Issue" %}

		 {% elif doc.doctype == "Quotation" %}
		 <b>Datum:</b> {{ doc.get_formatted("transaction_date") }}<br>
		 {% elif doc.doctype == "Sales Order" %}
		 <b>Datum:</b> {{ doc.get_formatted("transaction_date") }}<br>
		 {% else %}
		 <b>Datum:</b> {{ doc.get_formatted("posting_date") }}<br>
		 {% endif %}
		</td>
	</tr>
</table>

<br><br>



{%- endmacro -%}


{%- macro add_header3(page_num, max_pages, doc, letter_head, no_letterhead, footer) -%}

	{% if letter_head and not no_letterhead %}
		<div style="width:100% !important;">{{ letter_head }}</div>
	{% endif %}
    {% if doc.print_heading_template %}
        {{ frappe.render_template(doc.print_heading_template, {"doc":doc}) }}
    {% else %}
    <div class="">

    </div>
    {% endif %}
	{%- if doc.meta.is_submittable and doc.docstatus==0-%}

	{%- endif -%}
	{%- if doc.meta.is_submittable and doc.docstatus==2-%}

	{%- endif -%}
{%- endmacro -%} 

There is no problem rendering the fields expect one thing…num_pages and max_pages :wink: is there a clue to fix it?

Dan

So what is happening with num_pages and max_pages? Sorry I din’t get it.

1 Like

The function is not working inside the macro. It is not called.

Especially this:

  {% if page_num != max_pages %}

		{% if doc.doctype == "Sales Invoice" %}
			<big><big><big><big>Rechnung </big></big></big></big>
		{% elif doc.doctype == "Purchase Order" %}
			<big><big><big><big>Bestellung </big></big></big></big>
		{% elif doc.doctype == "Delivery Note" %}
		<big><big><big><big>Lieferschein </big></big></big></big>
		{% elif doc.doctype == "Sales Order" %}
		<big><big><big><big>Auftragsbestätigung </big></big></big></big>
		{% elif doc.doctype == "Quotation" %}
		<big><big><big><big>Angebot </big></big></big></big>
		{% elif doc.doctype == "Customer Issue" %}
		<big><big><big><big>Kundenreklamation </big></big></big></big>
		{% else %}

		<big><big><big><big>{{ doc.doctype }} </big></big></big></big>
		{% endif %}
    	{% endif %}

In add_header “original” Function it is working.

The API for add_header seems to have changed. Those values are no longer passed.

Well my question is not clear enough.

add header works with page_num and max_pages functions.

Only my self created add_header2 (own function) does not work with these two essential functions.
Basically this is something which is relevant for customization of erpnext in combination with creating of own macros for printout. I guess I will not be the only one who is interested in custom macros for printout - as I have read there are a lot of people seeking for a Tut on how to change the printout-behavior.

What I need is:

page_num and max_pages to work in custom macro (however it is called).

I am assuming you added your custom macros to standard_macros.html. This should actually work. Try troubleshooting it. Make sure you are calling it at right place and in right syntax.

I was making sure to get it called but the max_pages and page_num is not getting called at all. In a “non custom” macro it is called…but not in my custom macro.

Dan

Try printing {% page_num %} {% max_pages %}. Does it return anything?

Hi,

it says:

{{ undefined value printed: parameter ‘page_num’ was not provided }}

I am calling with:
<p class="text-right">{{ _("Page: {0}").format(page_num) }}</p>