Skip to content

Update Object Getter function names in Twig #2144

@gchtr

Description

@gchtr

In #2073 (comment), we discussed whether we should keep the Post, Term, … functions in Twig:

For the API in Twig, we still have Post, PostQuery, etc. For consistency, should we consider renaming these functions to get_post(), get_posts(), etc?

I think renaming and deprecating the old Twig functions is probably a good idea. Seeing "Use Timber::get_posts() instead of new Timber\PostQuery()" and then a paragraph later seeing "You can also use PostQuery in Twig" is a little jarring, and it's one less thing for new users to have to remember.

This is about the following functions in Twig:

timber/lib/Twig.php

Lines 49 to 70 in ca43edc

/**
* Timber object functions.
*/
$twig->addFunction(new TwigFunction('Post', function( $post_id, $PostClass = 'Timber\Post' ) {
return self::maybe_convert_array( $post_id, $PostClass );
} ) );
$twig->addFunction( new TwigFunction( 'PostQuery', function( $args ) {
return new PostQuery( $args );
} ) );
$twig->addFunction(new TwigFunction('Image', function( $post_id, $ImageClass = 'Timber\Image' ) {
return self::maybe_convert_array( $post_id, $ImageClass );
} ) );
$twig->addFunction(new TwigFunction('Term', array($this, 'handle_term_object')));
$twig->addFunction(new TwigFunction('User', function( $post_id, $UserClass = 'Timber\User' ) {
return self::maybe_convert_array( $post_id, $UserClass );
} ) );
$twig->addFunction( new TwigFunction( 'Attachment', function( $post_id, $AttachmentClass = 'Timber\Attachment' ) {
return self::maybe_convert_array( $post_id, $AttachmentClass );
} ) );

This leaves us with the following todo:

  • Deprecate Post, add get_post()
  • Rename PostQuery to get_posts() (no need to deprecate, added for 2.x)
  • Deprecate Term, add get_term()
  • Deprecate User, add get_user()
  • Deprecate Image, add get_image()
  • Rename Attachment to get_attachment() (no need to deprecate, added for 2.x)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions