-
Notifications
You must be signed in to change notification settings - Fork 6
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
Use Twig function to call CakePHP helpers. #26
Conversation
45e49fd
to
799488a
Compare
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.
Love that these don't require the use of |raw
. I'm not a big fan of the helper($nameMethod)
as it is more typing and feels more magical. The dynamic function approach feels more consistent with how twig works in other frameworks too.
@@ -198,6 +198,7 @@ protected function initializeExtensions(): void | |||
$this->twig->addExtension(new Extension\StringsExtension()); | |||
$this->twig->addExtension(new Extension\TimeExtension()); | |||
$this->twig->addExtension(new Extension\UtilsExtension()); | |||
$this->twig->addExtension(new Extension\ViewExtension()); |
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.
Should this be ViewHelperExtension
?
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.
I intended to add more view related function to the extension hence the name. For e.g. a fetch
function so that we can use {{ fetch('block') }}
instead of {{ _view.fetch('block')|raw }}
.
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.
I was thinking it might be cleaner to add functions/filters for TwigView itself directly in TwigView.
Seems odd to allow disabling TwigView-specific functions. But I won't argue if this feels correct to you.
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.
I was thinking it might be cleaner to add functions/filters for TwigView itself directly in TwigView.
That would would make the TwigView class too bloated IMO.
We aren't actually adding any new functionality to the View classes. This extension class just adds an additional layer of abstraction for accessing the View class methods/properties.
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.
ok. We can add the element and cell functions to it.
@@ -0,0 +1,2 @@ | |||
{{ helper_TestSecond_useElement() }} | |||
{{ helper_Text_autoParagraph('I love CakePHP') }} |
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.
Some other options that may be worse or better could be:
helper:Text:autoParagraph(...)
Is visually smaller but still the same amount of letters.helper_text_autoParagraph(...)
Using camelBack for the variables instead of PascalCase.h:Text:autoParagraph(...)
Shorter prefix?
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.
Supporting :
char would require adding additional token parsing code. Currently replacing the underscores with colons generates error Twig\Error\SyntaxError: Unexpected token "punctuation" of value ":" ("end of print statement" expected).
Not really looking forward to adding more customizations if not really necessary.
helper_text_autoParagraph(...) Using camelBack for the variables instead of PascalCase.
Using text
instead of Text
would mean additional inflection to find matching helper property. It's better to keep the casing same and corresponding PHP code.
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.
We could go with h_Text_autoParagraph()
to reduce typing.
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.
I think making the helper name and function look similar to other cake documentation/code is best so Text_autoParagraph
looks similar to Text->autoParagraph()
.
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.
Can you create Twig autocomplete on such functions? Then it would be similar again to normal PHP templating.
But I don't know enough about the IDE compatibility here.
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.
https://www.jetbrains.com/help/phpstorm/symfony-twig.html
I doubt any IDE would provide autocompletion for custom functions.
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.
I see, someone would need to port https://plugins.jetbrains.com/plugin/7219-symfony-support to cakephp version then.
If we aren't removing the helper context variables then should we make them globals so they can be used directly in macros? |
That's no necessary as helpers can already be accessed through view variable. Also the helper variable are kept just to prevent BC break, they will be eventually removed. |
This avoids having to preload helpers as with current usage and also avoids having to add |raw at the end of calls to prevent escaping.
This avoids having to preload helpers as with current usage and
also avoids having to add |raw at the end of calls to prevent escaping.