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

[Turbo] Add Twig Extensions #2618

Draft
wants to merge 15 commits into
base: 2.x
Choose a base branch
from

Conversation

seb-jean
Copy link
Contributor

@seb-jean seb-jean commented Mar 2, 2025

Q A
Bug fix? no
New feature? yes
Issues Fix #...
License MIT

Hi,

I added Twig extension:

turbo_refresh_method

Configure method to perform page refreshes
For more information: https://turbo.hotwired.dev/handbook/page_refreshes#morphing

Usage

To use this Twig extension you must write:

{# templates/base.html.twig #}
{{ turbo_refresh_method() }}

{# renders as: #}
<meta name="turbo-refresh-method" content="replace">

The possible values are morph or replace (the default).

turbo_refresh_scroll

Configure scroll strategy for page refreshes.
For more information: https://turbo.hotwired.dev/handbook/page_refreshes#scroll-preservation

Usage

To use this Twig extension you must write:

{# templates/base.html.twig #}
{{ turbo_refresh_scroll() }}

{# renders as: #}
<meta name="turbo-refresh-scroll" content="reset">

The possible values are preserve or reset (the default).

turbo_refreshes_with

A combination of the 2 previous Twig extensions.

Usage

To use this Twig extension you must write:

{# templates/base.html.twig #}
{{ turbo_refreshes_with() }}

{# renders as: #}
<meta name="turbo-refresh-method" content="replace"><meta name="turbo-refresh-scroll" content="reset">

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@carsonbot carsonbot added Turbo Status: Needs Review Needs to be reviewed labels Mar 2, 2025
seb-jean added 3 commits March 2, 2025 15:08

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@Kocal Kocal added the Feature New Feature label Mar 4, 2025
Copy link
Member

@Kocal Kocal left a comment

Choose a reason for hiding this comment

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

Hi, can you also add some tests and update the CHANGELOG (for 2.24)?

Thanks!

PS: please for the next time, keep the PR template 🙏🏻

return $this->turboRefreshMethod($method).$this->turboRefreshScroll($scroll);
}

public function turboRefreshMethod(string $method = self::REFRESH_METHOD_REPLACE): string
Copy link
Member

Choose a reason for hiding this comment

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

Please add some PHPDoc and a link to the documentation

return \sprintf('<meta name="turbo-refresh-method" content="%s">', $method);
}

public function turboRefreshScroll(string $scroll = self::REFRESH_SCROLL_RESET): string
Copy link
Member

Choose a reason for hiding this comment

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

Please add some PHPDoc and a link to the documentation

@carsonbot carsonbot added Status: Needs Work Additional work is needed and removed Status: Needs Review Needs to be reviewed labels Mar 4, 2025
@smnandre
Copy link
Member

smnandre commented Mar 5, 2025

I don't think we need to have 3 methods here. What about keeping just the first two ?

Also i would not set the default values in the methods and force users to be explicit (they do not need this function to set default value... and the day Turbo changes its default value it would be a real mess).

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@carsonbot carsonbot added Status: Needs Review Needs to be reviewed and removed Status: Needs Work Additional work is needed labels Mar 16, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@seb-jean
Copy link
Contributor Author

Hi to you both :),

Thank you for your feedback.

I added the PHPDocs. and updated CHANGELOG.
I will add the documentation later :).

I don't think we need to have 3 methods here. What about keeping just the first two ?

These tags are generally used together. Turbo Rails and Turbo Laravel also use this third function.

# https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb

#   turbo_refreshes_with(method: :morph, scroll: :preserve)
def turbo_refreshes_with(method: :replace, scroll: :reset)
  provide :head, turbo_refresh_method_tag(method)
  provide :head, turbo_refresh_scroll_tag(scroll)
end

Also i would not set the default values in the methods and force users to be explicit (they do not need this function to set default value... and the day Turbo changes its default value it would be a real mess).

For Morphing, Turbo says the default is replace. Similarly, for Scroll preservation, Turbo indicates that the default value is reset.

These default values ​​are also found in Turbo Rails:

# https://github.com/hotwired/turbo-rails/blob/main/app/helpers/turbo/drive_helper.rb

# Configure method to perform page refreshes. See +turbo_refreshes_with+.
def turbo_refresh_method_tag(method = :replace)
  raise ArgumentError, "Invalid refresh option '#{method}'" unless method.in?(%i[ replace morph ])
  tag.meta(name: "turbo-refresh-method", content: method)
end

# Configure scroll strategy for page refreshes. See +turbo_refreshes_with+.
def turbo_refresh_scroll_tag(scroll = :reset)
  raise ArgumentError, "Invalid scroll option '#{scroll}'" unless scroll.in?(%i[ reset preserve ])
  tag.meta(name: "turbo-refresh-scroll", content: scroll)
end

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@Kocal
Copy link
Member

Kocal commented Mar 18, 2025

Works for me then, thanks!
Maybe it could be a good idea to document (in the PHPDoc) that these new methods are "copied" from Turbo Rails?

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@seb-jean
Copy link
Contributor Author

I put a few words (Inspired by Turbo Rails ...) per function. Is it better if I put those few words at the class level instead?

@Kocal
Copy link
Member

Kocal commented Mar 19, 2025

That's perfect, thanks!

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels Mar 19, 2025
@seb-jean seb-jean marked this pull request as draft March 22, 2025 15:55

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@seb-jean seb-jean changed the title [Turbo] Add Twig Extensions for page refreshes [Turbo] Add Twig Extensions Mar 22, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature Status: Reviewed Has been reviewed by a maintainer Turbo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants