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

Use Twig function to call CakePHP helpers. #26

Merged
merged 1 commit into from
Apr 3, 2020
Merged

Conversation

ADmad
Copy link
Member

@ADmad ADmad commented Apr 2, 2020

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.

@ADmad ADmad added this to the 1.0 milestone Apr 2, 2020
@ADmad ADmad linked an issue Apr 2, 2020 that may be closed by this pull request
@ADmad ADmad force-pushed the helper-function branch 2 times, most recently from 45e49fd to 799488a Compare April 2, 2020 13:46
Copy link
Member

@markstory markstory left a 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());
Copy link
Member

Choose a reason for hiding this comment

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

Should this be ViewHelperExtension?

Copy link
Member Author

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 }}.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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') }}
Copy link
Member

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?

Copy link
Member Author

@ADmad ADmad Apr 3, 2020

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.

Copy link
Member Author

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.

Copy link
Member

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().

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

@othercorey
Copy link
Member

If we aren't removing the helper context variables then should we make them globals so they can be used directly in macros?

@ADmad
Copy link
Member Author

ADmad commented Apr 3, 2020

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.
@othercorey othercorey merged commit 19d687d into master Apr 3, 2020
@othercorey othercorey deleted the helper-function branch April 3, 2020 20:11
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.

Improve helper integration so templates don't need raw
4 participants