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

for post in page.posts does not work with Swig #232

Closed
thom4parisot opened this issue Jul 6, 2013 · 6 comments
Closed

for post in page.posts does not work with Swig #232

thom4parisot opened this issue Jul 6, 2013 · 6 comments
Milestone

Comments

@thom4parisot
Copy link
Member

I think it's related to the iterable properties of page.posts.

If I have this layout.swig file for example:

<ul>
{% for post in page.posts %}
  <li>{{ loop.index }} {{ loop.key }} {{ post.title }}</li>
{% endfor %}
</ul>

It will output this:

<ul>
    <li>1 _name </li>
    <li>2 _parent </li>
    <li>3 _store </li>
    <li>4 _index </li>
    <li>5 _schema </li>
    <li>6 length </li>
</ul>

Any explanation would be warmly welcomed. Twig is absolutely not documented and I'm struggling using it (for example, I discovered I needed to do {{ body | raw }} in my layout.swig to output the content of index.swig.

Thanks!

@tommy351
Copy link
Member

tommy351 commented Jul 6, 2013

You should use page.posts.forEach or page.posts.each to iterate the posts. However, it seems that there's no way to write JS code directly in Swig template?

@thom4parisot
Copy link
Member Author

None of them works :-(

@tommy351
Copy link
Member

tommy351 commented Jul 6, 2013

Okay. I'll add Warehouse support for Swig in the next version.

@thom4parisot
Copy link
Member Author

Cool thanks :-)

When I iterate on page.posts._index I successfully retrieves the IDs, but obviously, it's not the populated objects.

EJS lets you execute real functions, while Swig does not want to.
To return a result, the function must be wrapped in another one (like in Mustache and Handlebars).

Thanks for answering so fast :-)

tommy351 added a commit that referenced this issue Jul 7, 2013
@drozzy
Copy link

drozzy commented May 5, 2014

So.. is this fixed? I am getting exactly the same problem. site.posts does not output anything when iterating through it with Swig's for each loop.

@tommy351
Copy link
Member

tommy351 commented May 6, 2014

Swig plugin is removed. Use site.posts.toArray() instead.

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

No branches or pull requests

3 participants