stencilproject/Stencil

Insert newline (empty line) in template file

Emailsbi opened this issue · 2 comments

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 %}

i met the same question

djbe commented

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 🎉