picocms / Pico

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

Question: How to use page yaml-header meta-data in php for a plugin? #698

Closed new-on-github closed 1 month ago

new-on-github commented 2 months ago

Hello all together,

I'm using pico with the pico-rssmaker: https://github.com/MattByName/Pico-RssMaker https://github.com/MattByName/Pico-RssMaker/blob/master/RssMaker.php it works fine. Now I want to add a image to the other sub items of the rss-feed:

//Page loop
            foreach ($reverse_pages as $page) {
                if (!empty($page['date']) && !$page['hidden']) {
                    $rss .= '<item>';
                    $rss .= '<title>';
                    $rss .= $page['title'];
                    $rss .= '</title>';

                    $rss .= '<description>';
                    $rss .= $page['description'];
                    $rss .= '</description>';

                    $rss .= '<link>';
                    $rss .= $page['url'];
                    $rss .= '</link>';

                    $rss .= '<pubDate>';
                    $rss .= date(DATE_RFC2822, $page['time']);
                    $rss .= '</pubDate>';

                    $rss .= '<guid>';
                    $rss .= $page['url'];
                    $rss .= '</guid>';

                    $rss .= '</item>';
                }

Now some of the meta-data of the yaml-header:

--- 
title: my title
date: ...
style: blog
description: my description
image: %assets_url%/path-to-image
hidden: false
mastodon: false
--- 

can be accessed via $page['meta-item-name']; in php. But this works only with title, description but not with my own meta-tags and also not with the image-tag. I tried this:

$rss .= '<enclosure url="';
$rss .= $page['image'];
$rss .= '"/>';

But this doesn't work.

I already asked the maintainer of Pico-RssMaker. He tried to help, but there is still no solution: https://github.com/MattByName/Pico-RssMaker/issues/6

Does someone know how to access the meta-data-items in php?

Thanks al lot.

PhrozenByte commented 1 month ago

Try $page['meta']['image'] :+1:

Only basic meta data (like titles) can be accessed directly via e.g. $page['title']. I know, it's confusing...

new-on-github commented 1 month ago

Thanks a lot, this works now.

But there is now a other problem: It gives not the correct link back. Instead of https://my-domain/path-to-image it gives %assets_url%/image back. Can this also be solved without giving the full correct path in the meta-data-tag?

new-on-github commented 1 month ago

Ok, thinking before asking would help...

I could achieve this in php with the following code:

                    $rss .= '<enclosure url="';
                    $rss .= str_replace('%assets_url%', $this->baseURL."assets", $page['meta']['image']);
                    $rss .= '"/>';

Thanks a lot for the help and for pico cms!

PhrozenByte commented 1 month ago

Instead of https://my-domain/path-to-image it gives %assets_url%/image back.

Try $this->getPico()->substituteUrl($page['meta']['image']) instead (it's the same as Pico's url Twig filter).