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

Allow conditions in variable node #243

Merged
merged 4 commits into from
Sep 22, 2018
Merged

Conversation

ilyapuchka
Copy link
Collaborator

Resolves #241

@ilyapuchka ilyapuchka changed the title Use condition in variable node Allow conditions in variable node Sep 20, 2018
@yonaskolb
Copy link
Collaborator

Thoughts on {{ variable if condition }} vs {% variable if condition %}?

@ilyapuchka
Copy link
Collaborator Author

From implementation point of view it is easier to have it as extension of variable node, not a variation of if node. We can also add support for this in other nodes, like include, extend, filter

self.elseExpression = nil
}

init(variable: Resolvable, token: Token? = nil, condition: Expression?, elseExpression: Resolvable?) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not add the condition and elseExpression parameters to the public init above? (and make them nil by default)

Copy link
Collaborator Author

@ilyapuchka ilyapuchka Sep 21, 2018

Choose a reason for hiding this comment

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

Expression is not public, at least in this branch. TBH I don't even know why VariableNode needs to be public, but didn't want to make unrelated changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}

public init(variable: String, token: Token? = nil) {
self.variable = Variable(variable)
self.token = token
self.condition = nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

}

public func render(_ context: Context) throws -> String {
if let condition = self.condition {
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be one if statement. I'd also move the rest into an else clause, for clarity purposes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

what do you mean? it needs to be anwrapped here so we don't need to dance with optional chaining later. And it should continue execution if this is nil or if it evaluates to true, that's why the rest is not in else. It's kind of guard but using ifs. Can be merged into one though

Copy link
Collaborator Author

@ilyapuchka ilyapuchka Sep 21, 2018

Choose a reason for hiding this comment

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

now I read This can be one again and see that it's what you mean)

var components = token.components()

func hasToken(_ token: String, at index: Int) -> Bool {
return components.count > (index + 1) && components[index] == token
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I find ~= harder to grasp than > 🤷‍♂️

let elseExpression: Resolvable?

if hasToken("if", at: 1) {
let components = Array(components.suffix(from: 2))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to convert this into an Array? #226 has made me more conscious of performance issues, and this causes an allocation/copy that may not be needed.

Copy link
Collaborator Author

@ilyapuchka ilyapuchka Sep 21, 2018

Choose a reason for hiding this comment

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

yes, we can do it later in else branch

@ilyapuchka ilyapuchka merged commit df2e193 into master Sep 22, 2018
@ilyapuchka ilyapuchka deleted the variable-node-condition branch September 22, 2018 11:09
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