3

This is kind of extension to my other question Python Jinja2 call to macro results in (undesirable) newline.

My python program is

import jinja2
template_env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True, autoescape=False, undefined=jinja2.StrictUndefined)
template_str = '''
{% macro print_car_review(car) %}
    {% if car.get('review')  %}
  {{'Review: %s' % car['review']}}
    {% endif %}
{% endmacro %}
hi there
car {{car['name']}} reviews:
{{print_car_review(car)}}
  2 spaces before me
End of car details
'''
ctx_car_with_reviews = {'car':{'name':'foo', 'desc': 'foo bar', 'review':'good'}}
ctx_car_without_reviews = {'car':{'name':'foo', 'desc': 'foo bar'}}
print 'Output for car with reviews:'
print template_env.from_string(template_str).render(ctx_car_with_reviews)
print 'Output for car without reviews:'
print template_env.from_string(template_str).render(ctx_car_without_reviews)

Actual output:

Output for car with reviews:

hi there
car foo reviews:
  Review: good

  2 spaces before me
End of car details
Output for car without reviews:

hi there
car foo reviews:

  2 spaces before me
End of car details

Expected output:

Output for car with reviews:
hi there
car foo reviews:
  Review: good
  2 spaces before me
End of car details
Output for car without reviews:
hi there
car foo reviews:
  2 spaces before me
End of car details

What is undesirable (per car) is an extra newline at the beginning and an extra line before the line '2 spaces before me'

Thanks Rags

Community
  • 1
  • 1

1 Answers1

1

Complete edit of answer. I see what you're going for now, and I have a working solution (I added in one if statement to your template). Here is what I used, with all other lines of your code unchanged:

template_str = '''{% macro print_car_review(car) %}
    {% if car.get('review')  %}
  {{'Review: %s' % car['review']}}
    {% endif %}
{% endmacro %}
hi there
car {{car['name']}} reviews:
{% if 'review' in car %}
{{print_car_review(car)-}}
{% endif %}
  2 spaces before me
End of car details
'''

The spacing is exactly how I'm running it on my end, giving exactly the desired output you put in your question. I confess that I am myself confused by one point, which is that I had to move the first line, {% macro print_car_review(car) %}, up onto the same line as template_str = '''. Based on my understanding of the docs, setting trim_blocks=True should make that unnecessary, but I must be understanding it wrong.

Hopefully that gets you what you needed.

coralvanda
  • 6,431
  • 2
  • 15
  • 25
  • See my edited question and let me know if something is still unclear. – Rags Rachamadugu Sep 28 '16 at 18:25
  • Edited my answer to match with your edited question. – coralvanda Sep 28 '16 at 21:15
  • 1
    This works but this is a workaround. My whole point of creating macro was to avoid the caller having to do this if. I have to call this macro 10s of times in my case and each caller is now 2-extra lines. And in reality the condition in my macro is more complicated than this example. Doesn't this look like a bug in JINJA? The use of minus shouldn't be required given trim_blocks=true plus empty new lines shouldn't occur.. – Rags Rachamadugu Sep 29 '16 at 23:51
  • I see. You might be right. You can try submitting it over at the [jinja github](https://github.com/pallets/jinja). Sorry I couldn't provide a better solution for you. – coralvanda Sep 30 '16 at 08:46
  • 2
    Thank you for the github, I was struggling to find a place to submit the issue. Filed https://github.com/pallets/jinja/issues/612 – Rags Rachamadugu Sep 30 '16 at 15:53