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

How to pass the post.link property to JavaScript (when it is not available)? #2267

Closed
kuworking opened this issue Jun 10, 2020 · 2 comments
Closed

Comments

@kuworking
Copy link

kuworking commented Jun 10, 2020

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 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?

@kuworking 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
@gchtr
Copy link
Member

gchtr commented Jun 11, 2020

Hey @kuworking

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?

I see that this might be confusing at first. I answered your question in https://stackoverflow.com/a/62323866/1059980, but basically, the thing is this:

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 hope that answers your question.

@kuworking
Copy link
Author

Absolutely, and sorry for the redundancy, now this makes complete sense :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants