You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to pass all posts information to a JavaScript variable, but I see that (at least) post.link is not available at that moment
Is there any strategy I could follow to pass all the Timber information?
OLD
Based on this #1434 I've seen why I wasn't getting my post.link attribute
However, I was using Timber starter, so I am wondering why isn't it used in the starter (?)
The starter, in index.php
$context['posts'] = new Timber\PostQuery();
Later, in index.twig
{% for post in posts %}
{% include ['tease-' ~ post.post_type ~ '.twig', 'tease.twig'] %}
<script>
var t = JSON.parse('{{post|json_encode(constant('JSON_HEX_APOS'))|e('js')}}')
console.log(t)
</script>
{% endfor %}
There is no link property in the array, probably because it is yet not injected there
If I write {{ post.link }} it appears, so I guess it is injected on demand only?
Then, if in the starter I use
$context['posts'] = Timber::get_posts();
Then the link is already included and present in the javascript object
I image this is on purpose, is it looking for speed?
The text was updated successfully, but these errors were encountered:
kuworking
changed the title
Timber starter uses PostQuery() instead of get_posts(), so .link property is not available, what's the reason?
How to pass the post.link property to JavaScript (when it is not available)?
Jun 10, 2020
When you use {{ post.link }}, you would think that link is a property of the post object, but it’s actually a function. It’s just that in Twig, we don’t necessarily have to write {{ post.link() }}`, it also works without the parentheses.
And functions don’t show up if you dump a post object, so that’s why you won’t see link when you dump post or convert it to JSON.
I image this is on purpose, is it looking for speed?
A lot of functionality is built with functions. If we would request all the data an object can have when loading, this would truly affect performance and make your site slower. By having we functions, you can get data "on demand". So speed is definitely a reason why it’s built like that.
I rephrase the question since it was wrong
I am trying to pass all posts information to a JavaScript variable, but I see that (at least)
post.link
is not available at that momentIs there any strategy I could follow to pass all the Timber information?
OLD
Based on this #1434 I've seen why I wasn't getting my
post.link
attributeHowever, I was using Timber starter, so I am wondering why isn't it used in the starter (?)
The starter, in
index.php
Later, in
index.twig
There is no
link
property in the array, probably because it is yet not injected thereIf I write {{ post.link }} it appears, so I guess it is injected on demand only?
Then, if in the starter I use
Then the link is already included and present in the
javascript
objectI image this is on purpose, is it looking for speed?
The text was updated successfully, but these errors were encountered: