Skip to content

Commit

Permalink
pyinvoice: actually expose the "templated_text()" function to templat…
Browse files Browse the repository at this point in the history
…es and extend the documentation accordingly
  • Loading branch information
FelixSchwarz committed Jul 21, 2023
1 parent 64fad93 commit b09aa83
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ will be rendered as a currency value.
</item>
```

To apply templating you need to call the `tt()` template function in your html template, for example like this:

```html
{% for item in invoice.get_invoice_items() %}
<tr class="position-row">
<td>
{% if item.get_title() %}
<div class="pos-title">{{ tt(item.get_title(), item) }}</div>
{% endif %}
{{ tt(item.get_subtext(), item) }}
</td>
<td class="net-amount">
{{ f.amount(item.get_price(net=True)) }}
</td>
</tr>
{% endfor %}
```

If the invoice is set to English/USD the rendered string will be "My work: 16.75 h at $60.00".
For German/EUR the output is "My work: 16,75 h at 60,00 €".

9 changes: 8 additions & 1 deletion src/schwarz/pyinvoice/pdf_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from babel.support import Format, Translations
import jinja2

from .templating import templated_text


__all__ = ['generate_pdf', 'is_weasyprint_available']

Expand All @@ -27,7 +29,12 @@ def generate_pdf(invoice, invoice_cfg, target_path, *, with_logo=False):
template_dir = template_path.parent
template = load_invoice_template(template_path, locale=invoice.language)
_format = build_formatter(invoice.language, currency=invoice.currency)
template_params = {'f': _format, **invoice_cfg, 'with_logo': with_logo}
template_params = {
'tt': lambda text, dbobj: templated_text(text, dbobj, _format),
'f': _format,
**invoice_cfg,
'with_logo': with_logo,
}

html_str = template.render(invoice=invoice, **template_params)
with store_generated_html(template_dir, html_str) as path_html:
Expand Down

0 comments on commit b09aa83

Please sign in to comment.