picocms / Pico

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

name of the current page #587

Closed omniperspective closed 3 years ago

omniperspective commented 3 years ago

Please help,

I can't seem to get it working, so it must be officious, but I can't get it to work. how do I get the name (xyz.md) of the current page?

{{ current_page }} ?? but how

thanks Henk

mayamcdougall commented 3 years ago

Hi there. 👋🏻

If you're looking for the file name/path of the file, you'll want {{ current_page.id }}, and if you're looking for the YML Title, it's {{ current_page.title }}.

You won't actually get the .md extension from this however. This unique id is also what Pico uses when generating its URLs, so it drops the file extension from it.

Hope this helps.

Let me know if this doesn't work for you or if you have any other questions. 😁

omniperspective commented 3 years ago

Maya,

Another question:

I have the file index.md to get the content of the file I use {{ content }}

I have an extra file called "_index.md" (note the underscore) and I want to use the content of that file: This works {{ pages["index"].meta.template }} (for a YAML content) This works {{ current_page.id }} (note the underscore) this will get me the _index

This doesn't work. {{ pages["_{{ current_page.id }}"].meta.template }}

And then comes the question how do I get the data, what you normally get with {{ content }} of that file.

Or is there a better way to het the content of the file _index.md if the current file is called index.md

Ohh please help, this drives me nuts. Henk

mayamcdougall commented 3 years ago

Ah, okay, so, I think where you're getting confused is with how Twig works.

Pico themes are written using the Twig templating engine. Their docs can be a really good source for working out the syntax of your code, and/or learning about new tags. The page variables you're experimenting with come from Pico, and get imported on top of everything you'd normally get from Twig.

The {{ curly brackets }} in your code are basically shorthand for "print the result of this Twig code to the page". Inside the brackets, you're writing in Twig, while outside you're writing HTML. (Twig also uses {% these brackets %} to indicate function code, like logic and loops.)

The brackets just start and end the Twig block. Once you've opened a pair of brackets, you're writing in Twig until you close them. So, there's never going to be a time where you have brackets inside of brackets, like your {{ pages["_{{ current_page.id }}"].meta.template }} above.

Twig is a lot more like actual programming than HTML. So, the current_page.id variable you're looking to access is actually a String. You want to add an underscore _ to the beginning of it. We should be able to do that by concatenating the strings together with a ~ like this:

{{ pages["_" ~ current_page.id].meta.template }}*

(I'm just going off the top of my head with this, I haven't tested it, but it should* work. Let me know if it doesn't.)

So, to break it down. We start a block with {{. We're accessing the pages array, and looking up a page with a specific ID, which goes inside the square brackets [ ]. To get the ID you want, we're combining the String "_" (the quotes make it a String), with the value of current_page.id (which isn't in quotes because we want the value of this variable, we don't want to literally write "_current_page.id"). Then, once we have our "page" item, we're accessing the array of YML Metadata for that page and looking up the value of its template. Then we close out our Twig block, }}.

*(Okay, technically in Twig it's a Hash, and it has Key-Value pairs like a Dictionary from many programming languages, but I swear I just call everything an "array" 😅.)

Now, this is going to print the value of your page's template to the HTML on the page. This might not be what you want, but you can still use that same code from inside the brackets (pages["_" ~ current_page.id].meta.template) anywhere else that you're writing Twig. (So for example, in some logic like {% if pages["_" ~ current_page.id].meta.template == some_value %}, etc. ***

Hopefully this isn't explaining too much. I'm sort of assuming that you're new at this due to the whole "Brackets inside the brackets" thing. I figured it would be better to over explain than under explain. 😅

And to be honest, our own Pico Docs are inadequate, and have been a bit of a mess for years now. 😒

I've always found it easier to learn by doing, so just play around with some stuff from the Twig docs and you'll probably get the hang of it really quickly.

Hope this was helpful. Good Luck. 😉


Edit: Right, so you did mention what you were looking to do. Sorry, kind of forgot by the end of writing this. 😅

If you want to get the content of a page beside the one you're on, you can use the content filter, which looks like {{ page.id|content }}.

So, in your case, you should be able to access the content of your _ page with something like:

{{ pages["_" ~ current_page.id]|content }}*

*(Again, assuming the bit from before behaves like I expect it to, lol. 😂) See below.

mayamcdougall commented 3 years ago

There we go, all updated. Leaving another comment in case you're waiting for a notification. 😉

Probably could have just written the edit here instead, but I didn't think of that until just now... 🤣

mayamcdougall commented 3 years ago

I also find that looking at other peoples code can help immensely when you're trying to figure out syntax problems.

Take a look at the default Pico Theme (it used to just be in the the Themes folder, but now it's hidden somewhere deeper, so it'll just be easier to look at the repo).

Also, you can take a look at NotePaper, a theme I wrote a couple years ago. It's not the cleanest code, but there are a lot of complex, "what the heck was I thinking back then?", examples of Twig syntax.

Finally, Stellar is a theme I ported to Pico just a couple months ago. There should be some fairly readable examples in there... especially next to NotePaper. 😉(😅)

omniperspective commented 3 years ago

Maya,

thanks a million for this extended support. I will download the code and look at the Twig templating engine documentation to get more grip on what I'm seeing.

Kind regards Henk.

omniperspective commented 3 years ago

Maya,

tested your proposal: {{ pages["_" ~ current_page.id].meta.template }} does work!!

{{ pages["_" ~ current_page.id]|content }} doesn't work, but doesn't understand it for now.

reagrds Henk.

mayamcdougall commented 3 years ago

Sorry, been away for most of the day.

My bad on this. 😔

I misunderstood my old code and got a little confused trying to combine it with the earlier examples I gave you. 😅

The content filter actually takes a page id as the input, not the page array object itself.

So, it's actually a little simpler. It should be just:

{{ ("_" ~ current_page.id)|content }}

So, make a String of the page id we want (using the parenthesis to combine it before running the filter), then pass it to the content filter, which replaces the String with the desired page content. (Filters are a Twig thing where a pipe/bar symbol | can be used to format data and strings on the fly, such as |upper to convert a String to uppercase).

Anyway, sorry about that. 😅

I guess I jinxed it by mentioning I hadn't tested it first. Should be good this time, since I actually tested the code. 😂

mayamcdougall commented 3 years ago

Oh, another really good tool for figuring out what's going wrong in some cases is the |json_encode filter. It can dump an array object into JSON text, which can be handy for those times when you're like "I swear I'm accessing the right variable, but it isn't not working right." For example, you could dump your whole pages array ({{ pages|json_encode }}), just a single page ({{ pages["page_id"]|json_encode }}), or its metadata ({{ pages["page_id"].meta|json_encode }}), and "see" what it is that Pico/Twig sees.

omniperspective commented 3 years ago

Thanks Maya,

I'm now reviewing you code from the themes, to learn something. Implemented your code, and on the road again.

watch your mailbox.

regards Henk.

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1:

mayamcdougall commented 3 years ago

I'm letting this issue be closed for now, but feel free to keep asking questions here if you come across any more. There's no reason we can't keep discussing things on a closed issue, so don't be shy about it.

Hope your Pico adventure is going well. 😉

omniperspective commented 3 years ago

Beste Maya,

your info and direction gave a boot to my project. Studied your code, and took your advice. All is now manifesting in a new bigger world. Thanks again, and will keep your on your promise.

regards Henk.

Op 1 jun. 2021, om 18:37 heeft Maya McDougall @.***> het volgende geschreven:

I'm letting this issue be closed for now, but feel free to keep asking questions here if you come across any more. There's no reason we can't keep discussing things on a closed issue, so don't be shy about it.

Hope your Pico adventure is going well. 😉

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.