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

[Review] Include block improvements #114

Closed

Conversation

yonaskolb
Copy link
Collaborator

@yonaskolb yonaskolb commented May 22, 2017

Fixes #95

This adds 2 features to the include block including tests:

  1. If the include block is indented with any whitespace, any content in the included file is indented by the same amount
  2. Optional argument using which lets you pass a context to the included file using the format: {% include "myFile.stencil" using myContext %}

@yonaskolb
Copy link
Collaborator Author

Any comments on this @kylef?

@ilyapuchka
Copy link
Collaborator

+1

@ilyapuchka
Copy link
Collaborator

@kylef this looks good to me and feels like very useful fix. Any chance you can look into it any time soon or you ok with me merging it? =)

@yonaskolb
Copy link
Collaborator Author

Any updates on this?

if nodes.count > 1,
let indentedNode = nodes[nodes.count - 1] as? Indented,
let textNode = nodes[nodes.count - 2] as? TextNode {
indentedNode.indent = textNode.text.components(separatedBy: "\n").last!.trimmingCharacters(in: CharacterSet.whitespaces.inverted)
Copy link
Collaborator

@ilyapuchka ilyapuchka Jan 21, 2018

Choose a reason for hiding this comment

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

I was looking into this and I think there might be an issue with this implementation. What if include tag line is prefixed with something? i.e.

Something
  Something:  {% include \"include.html\" %}

If I understand the logic here you are getting the last line of the text preceding include tag, so this will be " Something: ". And then you are trimming everything which is not a whitespace. In this case nothing will be trimmed, so you will write to the indent the whole string, which will then add it to each line of included content.

Something
  Something:  first line
  Something:  second line

Right?

Also what do you think should be behaviour for indentation in this case. Should the second line of included content be indented at all? And if it should then how much? To match the offset of first line of included text or to match the offset of previous line in template? I.e. it should be

Something
  Something:  first line
              second line

or it should be

Something
  Something:  first line
  second line

? Should we give user an option to control this?

Also there is this pr that touches indentation too, so that if you do {%+ include ... %} it should keep spaces before tag. So this might conflict with each other.

I would therefore extract another feature that you've implemented here, for passing sub context to included template, into separate PR and maybe rethink indentation implementation. Maybe it should be done only after #92

@kylef what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe alternative solution for this could be indent node that will wrap included content (or any other content, i.e. result of macro call) and will define how all the content lines should be indented. Jinja2 has indent filter for that, but that will require to render content into variable to which the filter can be then applied, which is not yet supported by Stencil (though it is implemented in StencilSwiftKit). I think we can do both, node and filter to cover all scenarios, similar to how we have filter node in addition to filters applicable to variables.

@kylef thoughts?

Copy link
Collaborator

@ilyapuchka ilyapuchka Jan 21, 2018

Choose a reason for hiding this comment

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

Here is what I ended up with #188 , there was no need for a node, only for a filter, thanks to filter tag.

@ilyapuchka ilyapuchka changed the title Include block improvements [Review] Include block improvements Jan 21, 2018
@yonaskolb
Copy link
Collaborator Author

Closing this in favour of #188 in terms of indenting.

I might open a new PR for passing the context however. It seems the direction recently is to go with unnamed arguments, is that preferred to using?

@yonaskolb yonaskolb closed this Apr 8, 2018
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.

2 participants