eudicots / Cactus

Static site generator for designers. Uses Python and Django templates.
BSD 3-Clause "New" or "Revised" License
3.46k stars 314 forks source link

How to display latest post on the index page #211

Open lahtikarri opened 8 years ago

lahtikarri commented 8 years ago

Not exactly an issue issue; I'm using the portfolio template for a webcomic.

My question is how could I change the index.html page so that it would show just the latest comic, instead of a grid of image thumbnails that link to series of individual comics.

I've tried to call:

{% for comic in comics %} <article class="comic"> <section class="comic-body"> {% block body %} This is where the comic contents are. {% endblock body %} </section> </article> {% endfor %}

But that leads to total code destruction. Any ideas how this could be done? Or should I just forget it and use the template as it is?

Thanks!

steakscience commented 8 years ago

I've also tried to figure this out but I have no idea how to do it. I believe you have to use some kind for for loop in the blog.py and define it into a variable. But I'm not sure how to make it work.

dwightgunning commented 8 years ago

@lahtikarri In the snippet you post, the use of a "block" fragment inside a "for" loop is going to give you problems. If you remove the 'body' block (and the associated endblock tag), it'll probably sort itself out.

That'll loop through each comic object. From your question, it sounds like you're looking to only render the latest one. This can be achieved using the "with" tag to pull the first item in the 'comics' array (assuming they're sorted in reverse-chronological order).

{% with comics.0 as lastest_comic %}
  {{ latest_comic.attribute_name }}
{% endwith %}