-
-
Notifications
You must be signed in to change notification settings - Fork 364
[Turbo] Add Twig Extensions for meta
tags
#2618
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# CHANGELOG | ||
|
||
## 2.24.0 | ||
|
||
- Add Twig Extensions for `meta` tags | ||
|
||
## 2.22.0 | ||
|
||
- Add `<twig:Turbo:Stream>` component | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,12 @@ | |
*/ | ||
final class TwigExtension extends AbstractExtension | ||
{ | ||
private const REFRESH_METHOD_REPLACE = 'replace'; | ||
private const REFRESH_METHOD_MORPH = 'morph'; | ||
|
||
private const REFRESH_SCROLL_RESET = 'reset'; | ||
private const REFRESH_SCROLL_PRESERVE = 'preserve'; | ||
|
||
public function __construct( | ||
private ContainerInterface $turboStreamListenRenderers, | ||
private string $default, | ||
|
@@ -32,6 +38,12 @@ public function getFunctions(): array | |
{ | ||
return [ | ||
new TwigFunction('turbo_stream_listen', $this->turboStreamListen(...), ['needs_environment' => true, 'is_safe' => ['html']]), | ||
new TwigFunction('turbo_exempts_page_from_cache', $this->turboExemptsPageFromCache(...), ['is_safe' => ['html']]), | ||
new TwigFunction('turbo_exempts_page_from_preview', $this->turboExemptsPageFromPreview(...), ['is_safe' => ['html']]), | ||
new TwigFunction('turbo_page_requires_reload', $this->turboPageRequiresReload(...), ['is_safe' => ['html']]), | ||
new TwigFunction('turbo_refreshes_with', $this->turboRefreshesWith(...), ['is_safe' => ['html']]), | ||
new TwigFunction('turbo_refresh_method', $this->turboRefreshMethod(...), ['is_safe' => ['html']]), | ||
new TwigFunction('turbo_refresh_scroll', $this->turboRefreshScroll(...), ['is_safe' => ['html']]), | ||
]; | ||
} | ||
|
||
|
@@ -52,4 +64,95 @@ public function turboStreamListen(Environment $env, $topic, ?string $transport = | |
|
||
return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic); | ||
} | ||
|
||
/** | ||
* Generates a <meta> tag to disable caching of a page. | ||
* | ||
* Inspired by Turbo Rails | ||
* ({@see https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb}). | ||
*/ | ||
public function turboExemptsPageFromCache(): string | ||
{ | ||
return '<meta name="turbo-cache-control" content="no-cache">'; | ||
} | ||
|
||
/** | ||
* Generates a <meta> tag to specify cached version of the page should not be shown as a preview on regular navigation visits. | ||
* | ||
* Inspired by Turbo Rails | ||
* ({@see https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb}). | ||
*/ | ||
public function turboExemptsPageFromPreview(): string | ||
{ | ||
return '<meta name="turbo-cache-control" content="no-preview">'; | ||
} | ||
|
||
/** | ||
* Generates a <meta> tag to force a full page reload. | ||
* | ||
* Inspired by Turbo Rails | ||
* ({@see https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb}). | ||
*/ | ||
public function turboPageRequiresReload(): string | ||
{ | ||
return '<meta name="turbo-visit-control" content="reload">'; | ||
} | ||
|
||
/** | ||
* Generates <meta> tags to configure both the refresh method and scroll behavior for page refreshes. | ||
* | ||
* Inspired by Turbo Rails | ||
* ({@see https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb}). | ||
* | ||
* @param string $method The refresh method. Must be either 'replace' or 'morph'. | ||
* @param string $scroll The scroll behavior. Must be either 'reset' or 'preserve'. | ||
* | ||
* @return string The <meta> tags for the specified refresh method and scroll behavior | ||
*/ | ||
public function turboRefreshesWith(string $method = self::REFRESH_METHOD_REPLACE, string $scroll = self::REFRESH_SCROLL_RESET): string | ||
{ | ||
return $this->turboRefreshMethod($method).$this->turboRefreshScroll($scroll); | ||
} | ||
|
||
/** | ||
* Generates a <meta> tag to configure the refresh method for page refreshes. | ||
* | ||
* Inspired by Turbo Rails | ||
* ({@see https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb}). | ||
* | ||
* @param string $method The refresh method. Must be either 'replace' or 'morph'. | ||
* | ||
* @return string The <meta> tag for the specified refresh method | ||
* | ||
* @throws \InvalidArgumentException If an invalid refresh method is provided | ||
*/ | ||
public function turboRefreshMethod(string $method = self::REFRESH_METHOD_REPLACE): string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some PHPDoc and a link to the documentation |
||
{ | ||
if (!\in_array($method, [self::REFRESH_METHOD_REPLACE, self::REFRESH_METHOD_MORPH], true)) { | ||
throw new \InvalidArgumentException(\sprintf('Invalid refresh option "%s".', $method)); | ||
} | ||
|
||
return \sprintf('<meta name="turbo-refresh-method" content="%s">', $method); | ||
} | ||
|
||
/** | ||
* Generates a <meta> tag to configure the scroll behavior for page refreshes. | ||
* | ||
* Inspired by Turbo Rails | ||
* ({@see https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb}). | ||
* | ||
* @param string $scroll The scroll behavior. Must be either 'reset' or 'preserve'. | ||
* | ||
* @return string The <meta> tag for the specified scroll behavior | ||
* | ||
* @throws \InvalidArgumentException If an invalid scroll behavior is provided | ||
*/ | ||
public function turboRefreshScroll(string $scroll = self::REFRESH_SCROLL_RESET): string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add some PHPDoc and a link to the documentation |
||
{ | ||
if (!\in_array($scroll, [self::REFRESH_SCROLL_RESET, self::REFRESH_SCROLL_PRESERVE], true)) { | ||
throw new \InvalidArgumentException(\sprintf('Invalid scroll option "%s".', $scroll)); | ||
} | ||
|
||
return \sprintf('<meta name="turbo-refresh-scroll" content="%s">', $scroll); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.