stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.33k stars 221 forks source link

Lots of blank empty lines in output #22

Closed malkomalko closed 8 years ago

malkomalko commented 9 years ago

It appears if you use an if block, and it doesn't pass the truth test, it's still adding blank lines to the output. Have you found this to be correct?

kylef commented 9 years ago

@malkomalko Would you be able to share an example template showing this behaviour?

malkomalko commented 9 years ago

Any if block adds an empty line even if it returns false, but I'll try to create something basic and post up as a test case.

kylef commented 8 years ago

Closing due to inactivity, please re-open if there if a sample showing there is a bug in Stencil.

AliSoftware commented 8 years ago

I'm having a similar issue.

Template

enum Asset : String {
{% for image in images %}
    case {{ image }} = "{{ image }}"
{% endfor %}
}

Context used

let ctx = Context(dictionary: [
    "images": ["GreenApple", "RedApple", "Pears"]
])

Actual output

enum Asset : String {

    case GreenApple = "GreenApple"

    case RedApple = "RedApple"

    case Pears = "Pears"

}

Expected output

enum Asset : String {
    case GreenApple = "GreenApple"
    case RedApple = "RedApple"
    case Pears = "Pears"
}
AliSoftware commented 8 years ago

Writing a PR rn

AliSoftware commented 8 years ago

@kylef If you need more examples and tests, you can find plenty on the stencil branch of SwiftGen

Before that commit is what I expected to have, after the commit is what I actually have (= the changes I did to make my tests pass until this issue is solved on Stencil)

AliSoftware commented 8 years ago

@kylef Here's a dirty workaround I use to fix that issue until we have better. I don't like it very much (feels like cheating) but at least it works in the meantime.

kylef commented 8 years ago

Well, for now this works as expected (although we are talking about changing the behaviour). How it works now is not broken it's just how it was designed.

You don't need to do those workarounds, instead you could control how the newlines are used in your templates.

enum Suit { {% for suit in suits %}
  case {{ suit }}{% endfor %}
}
AliSoftware commented 8 years ago

…and make unreadable templates, yeah :smile: (And I though you were the perfectionist in the CoreTeam :troll face:)

I get your point, but the current behavior makes templates really hard to read and the new behavior would seem much more natural I think. So sure it's not a bug, but it's still a feature request :wink:

yonaskolb commented 7 years ago

Any there any updates on this? The solution that was discussed in https://github.com/kylef/Stencil/pull/32 seems to be the right one. Preventing newlines when control flow blocks (eg. if and for) are on their own line

bejar37 commented 7 years ago

I'd also love to see this feature! My templates have become unwieldy all too quickly.

yonaskolb commented 7 years ago

I've just implemented this in #85

paulosilva commented 4 years ago

Hi,

Can we do something like jinja2 for Whitespace Control ?

https://jinja.palletsprojects.com/en/master/templates/#whitespace-control

Thanks

yonaskolb commented 4 years ago

@paulosilva There's a PR that adds this here https://github.com/stencilproject/Stencil/pull/287

arielpollack commented 4 years ago

Can this be merged and closed? It's really hard generating code from templates without compromising the output or the template. It's either creating ugly and unmaintainable templates or just code with a lot of empty lines.

subdan commented 3 years ago

I have the same issue. Output contains a lot of blank lines.