picocms / Pico

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

_meta use and abuse #631

Closed digitalinferno closed 2 years ago

digitalinferno commented 2 years ago

I'm trying to make my theme completely separate from the contents, taking a cue from the social links section, I used the _meta file to display some information (statistics and skills)

---
skill:
    - title:
      bar:
      icon:

stats:
    - title:
      count:
      icon:

social:
    - title: 
      url: 
      icon:

---

So in my twig page I have three for cycles {% for var in pages["_meta"].meta.var %} of course it works, but I was wondering if it was a correct use or to be avoided.

mayamcdougall commented 2 years ago

I might be a little tired this morning still... but it took me too long to realize you probably meant var in your example above to be a placeholder for skill, stats, and social. 🤦🏻‍♀️

But yes, if that's the case, you're using them correctly. 😉

You can actually place extra data like this wherever is most convenient to you. _meta.md is nice, because it's clean and hidden away, but depending on your use case, you could do the same for any page. I often put configuration in index.md, since I tend to port a lot of "one page" themes that don't actually display the contents (that is, the markdown part) of index.md, and this gives it a purpose.

But for example if you had some variables that were specific to your content page, and you wanted to put them in a contact.md file (along with other contents), that would work too. It can go wherever you'd like. 😁

Also, take a look at a recent discussion on https://github.com/picocms/Pico/issues/629 if you're curious about how the capitalization of your metadata variables (or lack thereof) plays into accessing them in Twig. Since your example is all lowercase, but you got the idea from Pico's _meta.md, I'm guessing you might have run into this discrepancy by now.

Seems like you're in good shape, but let me know if you have any other questions. 😁👍🏻

digitalinferno commented 2 years ago

Ok we can put meta where we want, but what misleads me is the part of the guide that discourages the use of the pages variable with for loops

mayamcdougall commented 2 years ago

Yes, the pages variable should be used with discretion, especially with a large number of pages. The pages() function is what should usually be used instead, because it lets you choose exactly which part of your page tree you're interested in.

This part of the Documentation is a little funny, and I agree that it's not worded great right now. (I've got an entire Docs rewrite in the works, but that's another matter). 😅

Fetching specific pages via pages["page_id"] is fine, so is using for page in pages(), since by default the pages() function will only return one generation of children.

But if you had say 300+ pages, and you used for page in pages (with the pages variable), you'd be iterating over every single page on your site, and you'd probably slow page rendering down by a couple milliseconds.

It's really not as much of an issue as the Docs makes it sound. But this was a change introduced in Pico 2.0, and I think @PhrozenByte just wanted to make sure that everyone switched to using pages() instead of pages going forward so that Pico would truly be as fast as it possibly can be. 😉

So yeah, it's not about the loop, or about pages itself... it's about making sure you don't waste time iterating over tons and tons of pages you're not actually interested in.