kolber / stacey

Cheap & easy content management
http://staceyapp.com
MIT License
1.04k stars 131 forks source link

Get specific page child (latest post widget) #122

Closed teguhw closed 10 years ago

teguhw commented 10 years ago

Hi thank You for hardwork. Stacey app really great!! :dart:

I have problem with populate several post from specific page folder. Let say I want extract child from projects folder. I have trial and error put this code {% set page = get('/projects') %} on various location within code below but it's seems didnt work. Please someone shine a light how to solve this problem please... thank You

This code suppose to be on homepage and will list 5 latest child from "/projects" page

{% for child in page.root %}
{% if child.children %}
<ul class="projects">
{% for child in slice (child.children|reverse, 0,5) %}
  <li class="col seven {{ child.id }}">
    <p class="date col one">{{ child.date | date("Y, M-") }}</p>
    <h2 class="col five">
      {% if child.thumb %}
        <img src="{{ child.thumb.url }}" width="{{ child.thumb.width }}" height="{{ child.thumb.height }}" class="project-thumb" alt="">
      {% endif %}
      <a href="{{ child.url }}">{{ child.title }}</a>
    </h2>
  </li>
{% endfor %}
</ul>
{% endif %}
{% endfor %}
teguhw commented 10 years ago

Finally solved. :D Here is the snippets to load latest 5 projects child.

{% set page = get('/projects') %}
    {% for child in slice (page.children|reverse, 0,5) %}
      <li class="col seven {{ child.id }}">
        <p class="date col one">{{ child.date | date("Y, M-") }}</p>
        <h2 class="col five">
          {% if child.thumb %}
            <img src="{{ child.thumb.url }}" width="{{ child.thumb.width }}" height="{{ child.thumb.height }}" class="project-thumb" alt="">
          {% endif %}
          <a href="{{ child.url }}">{{ child.title }}</a>
        </h2>
      </li>
    {% endfor %}
o-l-e commented 10 years ago

Hey, don't forget to isolate the "set page" by wrapping your code in a block. It can really be confusing if the code/variables that come after "set page" on a document suddenly changes context. Like if your page.title in your footer suddenly changes completely because your context is now "/projects".

For example:

{% block get_projects_block %}
    {% set page = get('/projects') %}
    stuff here
{% endblock %}

And remember if you are using more than one "set page" on your document, create a unique name for each block so you don't get conflicting contexts, like so:

{% block get_blog_block %}
    {% set page = get('/blog') %}
    stuff here
{% endblock %}

In other words, dont set two blocks with identical names on a page, and use underscores not dashes in the block names if you have to, otherwise it won't work.

teguhw commented 10 years ago

Hi o-l-e Sorry for late response. just checked again this thread. thank You for your help mate. :+1: It's very helpful for newbie like me. didn't know about isolate with block lately. :dancer:

o-l-e commented 10 years ago

@teguhw glad it helped. I am a newbie at so much myself, and have learned so much from others through threads like this, so if i can help anyone i am happy to.