-
-
Notifications
You must be signed in to change notification settings - Fork 188
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
FEATURE: Allow usage of is*/has* accessors in Fluid templates directly #108
Conversation
Not sure why this is necessary, doesn't https://github.com/neos/flow/blob/master/Classes/TYPO3/Flow/Reflection/ObjectAccess.php#L166 already do this? AFAIR |
Yes, but the use case is to use the more explicit and expressive "object.isHidden" syntax inside Fluid. This would currently create the necessary getter "getIsHidden()" on the object, which is suboptimal for code readability. Edit: Note to self: Change IDE indenting to whitespace... |
35eaddb
to
2d54367
Compare
👍 Maybe we need to check with @bwaidelich if that be in sync with the standalone fluid ? |
try { | ||
$subject = ObjectAccess::getProperty($subject, $pathSegment); | ||
if (preg_match('/^(is|has)([A-Z].*)/', $pathSegment, $matches) > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more a theoretical issue probably, but this won't match all allowed php method names starting with "is" or "has". Checking with substring
like it's done in ObjectAccess
would be safer probably
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind - as you're only comparing the first letter after "is"/"has" this is not a real problem!
Nice one, thanks for this. if (ObjectAccess::isPropertyGettable($subject, $propertyName)) { and only change it if that's not the case |
Thanks for the feedback, will push new commits soonish. Edit: |
@bwaidelich I think it's more readable now and fully b/c. I also split the tests and added another one for the getIs/Has* case. PS: The failing styleCI check is unrelated, seems some CI conflict got merged/styleCI config is wrong? |
Fixed the StyleCI issue with a rebase |
👍 by reading. Thanks for taking care!! |
👍 looks good |
Just added a small documentation reference to the new behaviour. Will merge with previous votes. |
Rebased, maybe this helps the Travis hickup... |
This changeset adds support for accessor methods is* and has* to be used directly for property access. This allows to use such accessors in Fluid templates, which makes the template code more readable and avoids getIs* and getHas* methods in domain models. Example:: <f:if condition="{someObject.isSomething}"></f:if> This will call someObject->isSomething() method.
- Fix is/has* case ending up calling get* instead
Ok, after the Travis issue is fixed, will merge with previous votes now. |
… paths This fixes the try/catch for undefined Fluid variables which start with "is" or "has". Currently, exceptions here are not being caught because an `\Error` is being thrown. As a safety measure for PHP7, we're now catching any `\Throwable` as well. Also added is_callable check. Specific example: a fluid section/partial with an optional argument called "isSomething" - if rendered while defining isSomething in the arguments, there's no problem - if rendered without isSomething in the arguments (implicit falsy), we then get `Call to undefined method TYPO3\Fluid\Core\ViewHelper\TemplateVariableContainer::isSomething()` Related to: neos#108, neos#343
BUGFIX: Fix ObjectAccessorNode::getPropertyPath for is/has access in PHP7 This fixes the try/catch for undefined Fluid variables which start with "is" or "has". Currently, exceptions here are not being caught because an `\Error` is being thrown. Since non-callable method call should not be attempted in the first place, this change adds a is_callable check. Therefore a try/catch block is no longer necessary, as any exception thrown inside the callable method should bubble up. Specific example: a fluid section/partial with an optional argument called "isSomething" - if rendered while defining isSomething in the arguments, there's no problem - if rendered without isSomething in the arguments (implicit falsy), we then get `Call to undefined method TYPO3\Fluid\Core\ViewHelper\TemplateVariableContainer::isSomething()` Related to: #108, #343, #348, whythecode/flow-development-collection#1
This changeset adds support for accessor methods is* and has* to be
used directly for property access.
This allows to use such accessors in Fluid templates, which makes
the template code more readable and avoids getIs* and getHas* methods
in domain models.
Example::
This will call someObject->isSomething() method.