picocms / Pico

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

include php file in my ,md #489

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hi,

Very new to all of this. I cam trying to move from another .html based website to pico.

I was using a script called Ubergallery (and still want to). I called in in my .html file as follows: <?php include_once('/home/user/public_html/ubergallery/resources/UberGallery.php'); $gallery = UberGallery::init()->createGallery('2019_NZ'); ?>

How do I do this in my .md file?

PhrozenByte commented 5 years ago

Short answer: You can't. On purpose.

Anyway, Pico comes with a very powerful plugin system to do what you want. Just create a simple Pico plugin (create plugins/PicoUberGallery.php with the following contents) and utilize a Twig function:

<?php
class PicoUberGallery extends AbstractPicoPlugin
{
    public function __construct(Pico $pico)
    {
        parent::__construct($pico);
        require_once('/home/user/public_html/ubergallery/resources/UberGallery.php');
    }

    public function onTwigRegistered(Twig_Environment &$twig)
    {
        $twig->addFunction(new Twig_SimpleFunction('gallery', function ($name) {
            return UberGallery::init()->createGallery($name);
        }));
    }
}

You can then use it in your Twig templates (e.g. themes/my_theme/index.twig) using:

{{ gallery("2019_NZ") }}
ghost commented 5 years ago

Thanks!

…but does that mean I cannot call the plugin directly from my .md file? I ask because I was imagining a .md for each gallery that could use the same plugin.

Cheers, James.

On 9 Mar 2019, at 19:33, Daniel Rudolf notifications@github.com wrote:

Short answer: You can't. On purpose.

Anyway, Pico comes with a very powerful plugin system to do what you want. Just create a simple Pico plugin (create plugins/PicoUberGallery.php with the following contents) and utilize a Twig function:

<?php class PicoUberGallery extends AbstractPicoPlugin { public function construct(Pico $pico) { parent::construct($pico); require_once('/home/user/public_html/ubergallery/resources/UberGallery.php'); }

public function onTwigRegistered(Twig_Environment &$twig)
{
    $twig->addFunction(new Twig_SimpleFunction('gallery', function ($name) {
        return UberGallery::init()->createGallery($name);
    }));
}

} You can then use it in your Twig templates (e.g. themes/my_theme/index.twig) using:

{{ gallery("2019_NZ") }} — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/picocms/Pico/issues/489#issuecomment-471214746, or mute the thread https://github.com/notifications/unsubscribe-auth/AhCiwDi9VZ5WKobnWywOYu7G5Ib0uBLbks5vVAyfgaJpZM4bmsh7.

PhrozenByte commented 5 years ago

No, you can't call this in your content files. Pico isn't different to virtually any other CMS here, the concept behind is called "separation of concerns". Your content files are supposed to define your contents, not the layout/structure of your website. This is up to your theme.

Anyway, naturally you can still use the same template and plugin for any gallery on your website. Simply replace the currently static "2019_NC" string with a meta variable. For example, add the following to the YAML Front Matter your content file (e.g. content/my_2019_nz_gallery.md):

---
title: This is my 2019 NZ Gallery
gallery: 2019_NZ
---

And use this snippet in your Twig templates (e.g. themes/my_theme/index.twig):

{% if meta.gallery %}
    {{ gallery(meta.gallery) }}
{% endif %}

See http://picocms.org/docs/#text-file-markup and http://picocms.org/docs/#themes

stale[bot] commented 5 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:

kenotrajang commented 4 years ago

Hi, After browsing for solutions, I know that this is not possible to include a Php file when I find this discussion. Then I tried to make a plugin but it is beyond my grasp, I tried to learn (trial and error) from the existing plugins to no avail, but I really like Pico.

What I want to do is to include a php file which printing / output a list of links via a function. But I don't understand how Class work :(. Can you give me pointers please?

PhrozenByte commented 4 years ago

This highly depends on the actual code you're trying to include resp. what code you're trying to "include". If you're just trying to call some function, see https://github.com/picocms/Pico/issues/489#issuecomment-471214746 - the PHP file with the function definition is included on line 7, the function is later called on line 13.

kenotrajang commented 4 years ago

Hi, Here is what I tried, maybe you can point out what I did wrong.

I make a RandomLinks.php inside the Plugin dir

`<?php class RandomLinks extends AbstractPicoPlugin { public function construct(Pico $pico) { parent::construct($pico); require_once('pathto/thelinks.php'); // outputing function printRandomLinks() }

public function onTwigRegistered(Twig_Environment &$twig)
{
    $twig->addFunction(new Twig_SimpleFunction('linkslist', function ($name) {
        return printRandomLinks($name);
    }));
}

} ?>`

For testing, I just do echo inside the function printRandomLinks()

function printRandomLinks() { echo "<h3>Random Links</h3>"; }

then I call {{ linkslist }} inside my twig template, but nothing show up. My PHP is lacking, I have no idea what is wrong.

PhrozenByte commented 4 years ago

{{ linkslist }} prints the variable linkslist. If you want to call the function linkslist, you must use {{ linkslist() }}.

kenotrajang commented 4 years ago

Hi,

Ah, a noob mistake lol. Ok, so I tried to call the function but got this error : Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "linkslist" function.'

Something wrong that it failed to add the linkslist function to twig?

I'm sorry if I wasn't very clear because of my lack of knowledge. I want to pass the result from printRandomLinks() to twig. I thought Twig_SimpleFunction() is the way to go, but I don't know how it works.

kenotrajang commented 4 years ago

Let's say my plugin code become like this:

class RandomLinks extends AbstractPicoPlugin { public function printRandomLinks($links) { $links = "<h1>The Links</h1>"; echo $links; } public function onTwigRegistered(Twig_Environment &$twig) { $twig->addFunction(new Twig_SimpleFunction('linkslist', function ($name) { return printRandomLinks($name); })); } }

calling {{ linkslist() }} give error : Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "linkslist" function.'

PhrozenByte commented 4 years ago

The API_VERSION constant is missing.

<?php
class RandomLinks extends AbstractPicoPlugin
{
    const API_VERSION = 2;

    public function printRandomLinks($links)
    {
        $links = "<h1>The Links</h1>";
        echo $links;
    }

    public function onTwigRegistered(Twig_Environment &$twig)
    {
        $twig->addFunction(new Twig_SimpleFunction('linkslist', function ($links) {
            return $this->printRandomLinks($links);
        }));
    }
}
kenotrajang commented 4 years ago

It says : ( ! ) Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "linkslist" function.' in D:\Ampps\www\pico\themes\skelepico\post.twig on line 12 ( ! ) Twig_Error_Syntax: Unknown "linkslist" function. in D:\Ampps\www\pico\themes\skelepico\post.twig on line 12

PhrozenByte commented 4 years ago

Works for me. Make sure to put it below plugins/RandomLinks/RandomLinks.php.

btw: You should return the links in your function instead of echoing them, and calling the function currently requires a parameter, i.e. you'll have to call it like {{ linkslist("some links") }}.

kenotrajang commented 4 years ago

Yes! Thank you soooo much... For being patient with a noob like me.

My mistake was: I don't put the plugin file in its own folder.

Thank you again! I really like Pico! God bless you!