2.x Update Timber Object Getter function names in Twig#2178
2.x Update Timber Object Getter function names in Twig#2178acobster merged 16 commits into2.x-posts-apifrom
Conversation
|
Sorry for the slow response on this @gchtr — didn't realize you had some open Qs blocking you ...
I don't think so. Feels like something that could be done in the future, but not something we need to add for now.
<img src="{{ get_post( my_image_id ).src }}" />which is just, kinda misleading to read. It'd be very easy for someone to read that and not really understand what's going on. Even if these function(s) extended <img src="{{ get_image( my_image_id ).src }}" />makes total sense for someone to read-through ( |
# Conflicts: # docs/guides/posts.md # docs/guides/terms.md # lib/Twig.php # tests/test-timber-twig-objects.php
5393189 to
aa8d7ae
Compare
23c3421 to
b0d086a
Compare
|
|
||
| In Twig, you could use functions that were called the same as classes to convert objects or IDs of objects into Timber object. To have the same names as in Timber’s public API, we’ve added the following functions. | ||
|
|
||
| - `{{ get_post() }}` |
There was a problem hiding this comment.
| - `{{ get_post() }}` | |
| - `{{ get_attachment() }}` | |
| - `{{ get_attachments() }}` | |
| - `{{ get_image() }}` | |
| - `{{ get_images() }}` | |
| - `{{ get_post() }}` |
| - The `Timber\Image` class now extends the `Timber\Attachment` class. All your code should already be compatible with this change. But in the future, you might want to use the new `Timber\Attachment` class if you work with an attachment that is not an image. | ||
| - We’ve added new methods for `Timber\Attachment`. See the section below (@todo: Add anchor link) | ||
| - We’ve added a new Twig function `Attachment()`. (@todo: Add link to documentation) | ||
| - To get attachments from attachment IDs in Twig, you can use `{{ get_post(attachment_id) }}`. (@todo: Add link to documentation) |
There was a problem hiding this comment.
| - To get attachments from attachment IDs in Twig, you can use `{{ get_post(attachment_id) }}`. (@todo: Add link to documentation) | |
| - To get attachments from attachment IDs in Twig, you can use `{{ get_attachment(attachment_id) }}`. (@todo: Add link to documentation) |
the URL/file path use-case brings up an interesting question here. Can we accomplish everything we need with just |
Resolved conflicts manually, favoring changes from #2178. ~ Conflicts: ~ lib/Twig.php ~ tests/test-timber-image.php ~ tests/test-timber-twig-objects.php
I didn‘t manage to get a hold of the mess that is #2170 now, after the base branch update to
2.x-factories😅. So here it is again, with the same commits cherry-picked against2.x-factories.Ticket: #2144
Issue
See #2144.
Solution
Timber::get_*functions.Post(), it can either beget_post()orget_posts(), depending on what’s at hand. The same goes forTermandUser.{{ Attachment() }}function as well as deprecated the{{ Image() }}function without replacement, because we can use{{ get_post() }}and{{ get_posts() }}. The Class Maps will handle returning either aTimber\ImageorTimber\Attachment.Impact
A more unified way of interacting with Timber’s API throughout PHP and Twig.
Usage Changes
New function names. Pretty much the same functionality, except for handling singular and multiple objects.
Considerations
Timber::get_*functions, likeTimber::get_user_by()orTimber::get_post_by_slug()? I think, currently we don’t have to do that.AttachmentandImageposts:{{ get_attachment }}and{{ get_image() }}? If we have them in Twig, we should also have them in PHP, asTimber::get_*functions.Testing
Tests are currently breaking. We first need to resolve the Considerations about adding a
get_attachment()and aget_image()functions, so it’s easier to update the tests. If we just switch toget_post()now and then want to addget_attachment()orget_image()later, it will be harder to find the test methods that need these functions. Now, we can still search for{{ Attachment() }}and{{ Image() }}.Todo
AttachmentandImageclass.