sartography / spiff-arena

SpiffWorkflow is a software development platform for building, running, and monitoring executable diagrams
https://www.spiffworkflow.org/
GNU Lesser General Public License v2.1
48 stars 36 forks source link

Markdown rendering issue #1503

Open sashayar13 opened 3 weeks ago

sashayar13 commented 3 weeks ago

There is potentially a rendering issue with my markdown (see below).

I want to display the note_index sequentially for each item with notes. When this script is rendered it displays all notes with index = 1.

### Items  
| Merchant | Item | Receipt | Sub-Category | Qty | Currency | Unit Price | Total Price |  Unit Price, {{ agreement_cur }} | Total {{ agreement_cur }} Price | Unit Price, USD | Total USD Price |
| -------- | ---- | ------- | ------------ | --- | -------- | ---------- | ----------- | -------------- | --------------- | --------------- | ------------- |
{% set note_index = 0 %}
{% for item in exp_details["item"] %}
    {% if item["item_note"] %}
        {% set note_index = note_index + 1 %}
        | {{ item["merchant"] }} | {{ item_name_list[loop.index0] | sanitize_for_md }}<sup>[{{ note_index }}]</sup> | {{ item["file_links"] | sanitize_for_md }} | {{ item["sub_category_name"] | sanitize_for_md }} | {{ item["qty"] }} | {{ item["currency"] }} | {{ "{:,.2f}".format(item["unit_price_num"]) }} | {{ "{:,.2f}".format(item["unit_price_total"]) }} | {{ "{:,.2f}".format(item["converted_to_unit_price_agreement_cur"]) }} | {{ "{:,.2f}".format(item["unit_price_total_agreement_cur"]) }} | {{ "{:,.2f}".format(item["converted_to_unit_price_base_cur"]) }} | {{ "{:,.2f}".format(item["unit_price_total_base_cur"]) }} |
    {% else %}
        | {{ item["merchant"] }} | {{ item_name_list[loop.index0] | sanitize_for_md }} | {{ item["file_links"] | sanitize_for_md }} | {{ item["sub_category_name"] | sanitize_for_md }} | {{ item["qty"] }} | {{ item["currency"] }} | {{ "{:,.2f}".format(item["unit_price_num"]) }} | {{ "{:,.2f}".format(item["unit_price_total"]) }} | {{ "{:,.2f}".format(item["converted_to_unit_price_agreement_cur"]) }} | {{ "{:,.2f}".format(item["unit_price_total_agreement_cur"]) }} | {{ "{:,.2f}".format(item["converted_to_unit_price_base_cur"]) }} | {{ "{:,.2f}".format(item["unit_price_total_base_cur"]) }} |
    {% endif %}
{% endfor %}
----------
### Item Notes
{% set note_index = 0 %}
{% for item in exp_details["item"] %}
    {% if item["item_note"] %}
        {% set note_index = note_index + 1 %}
        - [{{ note_index }}] **{{ item_name_list[loop.index0] | sanitize_for_md }}**: {{ item["item_note"] | sanitize_for_md }}
    {% endif %}
{% endfor %}

I've tried to apply it to the process model https://dev.app.spiff.status.im/editor/process-models/manage-finance:accounts-payable:process-expense-reports:capture-expense-data/files/capture-expense-details.bpmn Task - Review expense report. However, due to failure, I ended up using this script (which is not ideal). This script uses the item index for note_index.

### Items  
| Merchant | Item | Receipt | Sub-Category | Qty | Currency | Unit Price | Total Price |  Unit Price, {{ agreement_cur }} | Total {{ agreement_cur }} Price | Unit Price, USD | Total USD Price |
| -------- | ---- | ------- | ------------ | --- | -------- | ---------- | ----------- | -------------- | --------------- | --------------- | --------- |
{% for item in exp_details["item"] %}
| {{ item["merchant"] }} | {{ item_name_list[loop.index0] | sanitize_for_md }}{% if item["item_note"] %}<sup>[{{ loop.index }}]</sup>{% endif %} | {{ item["file_links"] }} | {{ item["sub_category_name"] }} | {{ item["qty"] }} | {{ item["currency"] }} | {% if item["currency_type"] == "crypto" %}{{ "{:,.2f}".format(item["unit_price_num"]) }}{% else %}{{ "{:,.2f}".format(item["unit_price_num"]) }}{% endif %} | {% if item["currency_type"] == "crypto" %}{{ "{:,.2f}".format(item["unit_price_total"]) }}{% else %}{{ "{:,.2f}".format(item["unit_price_total"]) }}{% endif %} | {{ "{:,.2f}".format(item["converted_to_unit_price_agreement_cur"]) }} | {{ "{:,.2f}".format(item["unit_price_total_agreement_cur"]) }} | {{ "{:,.2f}".format(item["converted_to_unit_price_base_cur"]) }} | {{ "{:,.2f}".format(item["unit_price_total_base_cur"]) }} |
{% endfor %}

### Item Notes

{% for item in exp_details["item"] %}
{% if item["item_note"] %}
- [{{ loop.index }}] {{ item_name_list[loop.index0] | sanitize_for_md }}: {{ item["item_note"] | sanitize_for_md }}
{% endif %}
{% endfor %}
jasquat commented 3 weeks ago
{% set note_index = 0 %}
{% for item in ['hey', 'yo'] %}
    {% set note_index = note_index + 1 %}
    {{ item }}: {{ note_index }}: {{ loop.index0 }}: {{ loop.index }}
{% endfor %}

does demonstrate this issue. Looks like you can't update a variable in a loop in an easy way. What is the issue with loop.index? Seems preferable to use something builtin rather than an additional variable. Is it because the index counter only increases for some of the records?

jasquat commented 3 weeks ago

Here is an example of how it could be done:

{% set note_index = {"num": 0} %}
{% for item in ['hey', 'yo', 'more', 'items'] %}
    {% if note_index.update({"num": note_index["num"] + 1}) %}{% endif %}
    {{ item }}: {{ note_index["num"] }}
{% endfor %}
jasquat commented 3 weeks ago

And here is using namespaces:

{% set note_index = namespace(num=0) %}
{% for item in ['hey', 'yo', 'more', 'items'] %}
    {% set note_index.num = note_index.num + 1 %}
    {{ item }}: {{ note_index.num }}
{% endfor %}