dave-howard / vsdx

vsdx - A python library for processing .vsdx files
BSD 3-Clause "New" or "Revised" License
67 stars 25 forks source link

second+ loop statement on page do not work #24

Closed Vladimir-Ilyin closed 3 years ago

Vladimir-Ilyin commented 3 years ago

Hello!

I try to use 5 loops on one page, but have an error

jinja2.exceptions.TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.

Only one loop statement is work

Vladimir-Ilyin commented 3 years ago

i think we need change this in jinja_create_for_loop_if

            if previous_shape:
                previous_shape.xml.tail = jinja_loop_text  # add jinja loop text after previous shape, before this element
            else:
                shape.parent.xml.text = jinja_loop_text  # add jinja loop at start of parent, just before this element
            shape.text = text.lstrip(jinja_loop_text)  # remove jinja loop from <Text> tag in element

            # add closing 'endfor' to just inside the shapes element, after last shape
            shape.xml.tail = '{% endfor %}'  # add text at end of Shape element

to this

            if previous_shape:
                if not previous_shape.xml.tail:
                    previous_shape.xml.tail = jinja_loop_text  # add jinja loop text after previous shape, before this element
                else:
                    previous_shape.xml.tail = previous_shape.xml.tail+jinja_loop_text  # add jinja loop text after previous shape, before this element
            else:
                shape.parent.xml.text = jinja_loop_text  # add jinja loop at start of parent, just before this element
            shape.text = text.lstrip(jinja_loop_text)  # remove jinja loop from <Text> tag in element

            # add closing 'endfor' to just inside the shapes element, after last shape
            if not shape.xml.tail:
                shape.xml.tail = '{% endfor %}'  # add text at end of Shape element
            else:
                shape.xml.tail = shape.xml.tail+'{% endfor %}'  # add text at end of Shape element

and more loops on page work without error

dave-howard commented 3 years ago

HI - I guess this is where the jinja template has nested loops in the same shape. Something like: {% for list_item in my_list %}{% for j in list_item.list_property %}{{ j }}{% endfor %}{% endfor %}

Your code changes look good to me, it's great to see an issue raised with a solution :)

I will update the code and add a test for this scenario to the package.

Vladimir-Ilyin commented 3 years ago

Hello!

in my visio template, different loops went besides to each other, it turned out that the endfor of the previous loop was overwrited by the for statement of the next

dave-howard commented 3 years ago

I think I have fixed this now - at least I have a working test with nested loops in a single shape, which was failing previosuly (sounds similar to what you describe) and now test is passing. That change has been pushed up in this repo and to PyPI so please update your end and let me know if this resolves the issue, thanks

Vladimir-Ilyin commented 3 years ago

Hello!

Now all work fine Thank You very much!

Vladimir-Ilyin commented 3 years ago

one more thing how can i hide some shape inside the loop? showif statement inside loop do not work

Vladimir-Ilyin commented 3 years ago

i make some adds for showif statement to work in loops only required: shape with showif should be at the top of the group (in xml the last one in order), otherwise I just can't get into the correct parent ...and need to correct a little, so other statements in shape with showif do not removed

- 1
  579:
  580:
  581:
  582:
  583:
  584:
  585:
  586:
  587:
+ 2
  579:
  580:                          prev_shape2 = shape
  581:                          for shape2 in shape.sub_shapes():  # type: VisioFile.Shape
  582:                              # manage for loops in template
  583:                              shape2_id = VisioFile.jinja_create_for_loop_if(shape2, prev_shape2)
  584:                              prev_shape2 = shape2
  585:                              # manage 'set self' statements
  586:                              #VisioFile.jinja_set_selfs(shape, context)
  587:

- 1
  694:              # add closing 'endfor' to just inside the shapes element, after last shape
  695:              shape.xml.tail = '{% endif %}'  # add text at end of Shape element
  696:
+ 2
  694:              # add closing 'endfor' to just inside the shapes element, after last shape
  695:              shape.xml.tail = str(shape.xml.tail or '')+'{% endif %}'  # add text at end of Shape element
  696:
dave-howard commented 3 years ago

there are a few scenarios to think about here:

I'll starting thinking about those...

dave-howard commented 3 years ago

Hi - so I think I have resolved this now, at least the test I have added works - would be good to know if this helps your use case also?

A few issues resolved:

All included in release v0.4.8 and pushed to this repo

Thanks Dave

Vladimir-Ilyin commented 3 years ago

Hi Dave!

now all looks fine, all showif statements works well Thank You very much!

dave-howard commented 3 years ago

My pleasure - closing issue now resolved.