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

Insert newline (empty line) in template file #309

Closed Emailsbi closed 1 year ago

Emailsbi commented 2 years ago

Hi,

I'm trying to make output properly formatted and the only thing that is missing now is newlines. I've tried just inserting newlines in stencil file by pressing enter, tried specifying \n or "\n", tried passing newline symbol via params. Nothing works, Stencil just erases them.

Here is the example of a modified macro I'm using from SwiftGen (inside of two "if not forloop.first") Thanks

{% macro recursiveBlock table item %}
    {% for string in item.strings %}
    {% if not forloop.first %}\n{% endif %}
    {% if not param.noComments %}
    /// {{string.translation}}
    {% endif %}
    {% if string.types %}
    {{accessModifier}} static func {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> String {
        R.String.resolve("{{table}}", "{{string.key}}", {% call argumentsBlock string.types %})
    }
    {% else %}
    {# custom localization function is mostly used for in-app lang selection, so we want the loc to be recomputed at each call for those (hence the computed var) #}
    {{accessModifier}} static var {{string.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}: String {
        R.String.resolve("{{table}}", "{{string.key}}")
    }
    {% endif %}
    {% endfor %}
    {% for child in item.children %}
    {% if not forloop.first %}\n{% endif %}
    {{accessModifier}} enum {{child.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
        {% filter indent:4 %}{% call recursiveBlock table child %}{% endfilter %}
    }
    {% endfor %}
{% endmacro %}
shushengyuan commented 2 years ago

i met the same question

djbe commented 1 year ago

Your question is more of a SwiftGen question (in your specific case).

Your extra lines are getting removed, because SwiftGen has a workaround for issues with whitespace control in Stencil, see here for the workaround: https://github.com/SwiftGen/StencilSwiftKit/blob/stable/Sources/StencilSwiftKit/StencilSwiftTemplate.swift

Luckily, we've finally merged the equivalent of Jinja whitespace control into Stencil, and'll release it soon, so it'll be available in SwiftGen as well afterwards 🎉