Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added indent filter #188

Merged
merged 4 commits into from
Jan 28, 2018
Merged

Added indent filter #188

merged 4 commits into from
Jan 28, 2018

Conversation

ilyapuchka
Copy link
Collaborator

@ilyapuchka ilyapuchka commented Jan 21, 2018

With this filter it is possible to indent content either of variable or included from another template.

Examples:

Indented text: {{ "first line\nsecond line"|indent:2,"\t"}}

will render:

Indented text: first line
\t\tsecond line

With filter tag indentation can be also applied to included template content:

Included text: {% filter indent:2 %}{% include "text.html" %}{% endfilter %}

will render:

Included text: first line
  second line

Resolves #95

Copy link
Collaborator

@kylef kylef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I have a few suggestions. Could you also please add the ident filter to the documentation https://github.com/kylef/Stencil/blob/master/docs/builtins.rst#built-in-filters with an example and description.

@@ -26,7 +26,7 @@ func defaultFilter(value: Any?, arguments: [Any?]) -> Any? {
}

func joinFilter(value: Any?, arguments: [Any?]) throws -> Any? {
guard arguments.count < 2 else {
guard arguments.count <= 1 else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you break this into separate PR and include tests for this change please?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not change the logic, I just prefer <= over <. I'll revert to not have unneeded changes


func indentFilter(value: Any?, arguments: [Any?]) throws -> Any? {
guard !arguments.isEmpty else {
throw TemplateSyntaxError("'indent' filter takes at least 1 argument")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd perhaps make this work without any arguments. Defaulting the width to 4 spaces.

{{ names|indent }}

}

guard let indentWidth = arguments[0] as? Int else {
throw TemplateSyntaxError("first argument should be Int")
Copy link
Collaborator

@kylef kylef Jan 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this error message could be clearer as the following, including filter name along with the argument label and what the user mistakenly put.

'indent' filter width must be an integer ('{{ the width user provided }}')

Similar for errors below.

let result = try template.render(Context(dictionary: ["value": "One\nTwo"]))
try expect(result) == " One\n Two"
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test where the value has a trailing new line. I think the behaviour is currently that the trailing new line will be removed due to the split/join. Would be great to confirm and fix if so.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fine as lines are then joined with new line separator and when they are split empty lines will be preserved. Added that to the test for empty lines.

let result = try template.render(Context(dictionary: ["value": "One\nTwo"]))
try expect(result) == " One\n Two"
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a test for a value that has empty lines? For example: Line 1\n\n\nLine 3\n. The condition for empty lines is not tested:

return result + [(line.isEmpty ? "" : "\(indentation)\(line)")]

@ilyapuchka
Copy link
Collaborator Author

ilyapuchka commented Jan 22, 2018

@kylef all your comments addressed, thanks for taking time to review this =)

@kylef
Copy link
Collaborator

kylef commented Jan 22, 2018

I think one was missed (documentation): #188 (review), otherwise good to go.

@ilyapuchka
Copy link
Collaborator Author

Ah, yes, didn't notice that. Documentation should be rebuilt for other PR's too, so I was thinking to do it later with one PR, but can do it now too.

@kylef
Copy link
Collaborator

kylef commented Jan 22, 2018

I'm fine with that too, so will merge when CI is finished and you can make a separate PR.

@kylef kylef added this to the 0.11.0 milestone Jan 22, 2018
@AliSoftware
Copy link
Collaborator

\cc @djbe we should now use that instead of our {{sp}} trick in our templates 😉

@ilyapuchka
Copy link
Collaborator Author

@kylef this is ready to be merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants