picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.84k stars 614 forks source link

load specific content in sub pages #403

Closed M4rtine closed 6 years ago

M4rtine commented 6 years ago

Is it possible to load in 1 page all contents of the sub pages

i need loading in portfolio/index all the content of each projects folders

PhrozenByte commented 6 years ago

Sure, simply create a appropriate Twig template. You can include the parsed contents of arbitrary pages using Pico's content Twig filter. To include e.g. the content's of project01/index.md, use {{ "project01/index"|content }} in your Twig template (e.g. portfolio.twig) and add Template: portfolio to the YAML Frontmatter of portfolio-directory/index.md.

M4rtine commented 6 years ago

is it possible to load dynamically in spite of a name variable ? Do you understand ?

PhrozenByte commented 6 years ago

Sure, simply use a meta variable. Add e.g. the following to the YAML Frontmatter of portfolio-directory/index.md

projects:
  - project01
  - project02

and use the following in your portfolio.twig:

{% for project in meta.projects %}
    <div>{{ (project ~ "/index")|content }}</div>
{% endfor %}
stefanocpp commented 6 years ago

Hello,

I think I have a similar question:

I'd like to have few pages rendered in a single one, i.e. few .md documents that end up in the same html output one below the other. I thought I could copy and paste them in the same .md doc, but this isn't the best solution (because, for one, it's manual) and doesn't allow me to put footnotes at the end of each article instead of at the end of the whole page. I think this could be done via Twig, but I'm not a developer or anything and while I began studying Twig manuals, I'm weeks away from understanding how to make it work. What I'd like is the index page to load the "n" most recent articles I wrote either by saying "only articles in the last 2 months" or "only 7 most recent articles written". I'm not interested in having older ones kept in "previous" pages since I'm to archive them in the appropriate section of the website manually.

Thanks in advance and thank you for all the work you pour into Pico!

PhrozenByte commented 6 years ago

Try one of the following Twig snippets (I didn't test them):

  1. List articles of the last 2 months only
{% for page in pages|sort_by("time")|reverse %}
    {% if page.time >= date("-2 months") %}
        <h2>{{ page.title }}</h2>
    {% endif %}
{% endfor %}
  1. List the 7 most recent articles only
{% for page in pages|sort_by("time")|reverse|slice(0, 7) %}
    <h2>{{ page.title }}</h2>
{% endfor %}
stefanocpp commented 6 years ago

Thank you very much! I used snippet 2 and worked. Since I didn't need titles but rather content I changed it to:

{% for page in pages|sort_by("time")|reverse|slice(0, 7) %}  
    {{page.id|content}}  
{% endfor %}

and it worked. I now have a page that gets filled in a manner I cannot figure out. I'd like to have it picking the 'Date' in YAML frontmatter so that if I change one bit of an old article it doesn't jump to the top. I also have few "about" pages which I don't want to end in the index.twig output and I'm wondering how to rule them out - but if I manage to get the YAML date working I could date them 1900 and not worry about them anymore.

thank you for any further help!

PhrozenByte commented 6 years ago

Simply add a a condition, like the {% if ... %}...{% endif %} in the first snippet.

To exclude all pages without a date, simply use the condition {% if page.time %}...{% endif %}. To explicitly hide a page from your pages list, add the line unlisted: true to the YAML front matter of said page and use the condition {% if not page.meta.unlisted %}. Alternatively you can ignore all pages in a specific path ({% if page.id starts with "about/" %}...{% endif %}). Multiple conditions can be consolidated using and (or or, depending on what you're trying to do; use brackets (( and )) for even more complex conditions). The snippet might look like the following in the end:

{% for page in pages|sort_by("time")|reverse|slice(0, 7) %}
    {% if page.time and not page.meta.unlisted %}
        {{page.id|content}}
    {% endif %}
{% endfor %}

fyi: The pages are not sorted by the last modification time of the .md file, but rather by the Date meta header in your page's YAML front matter. Add e.g. Date: 2018-01-04 to a page's YAML front matter and that page will be sorted before a page with Date: 2018-01-01 (actually the newer page is sorted after the older one, but the reverse Twig filter instructs Pico to reverse the sorting, resulting in the desired behavior). Pages without this meta header are ignored due to the {% if page.time %}...{% endif %} condition.

stefanocpp commented 6 years ago

thank you very very much!