Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include php file in my ,md #489

Closed
ghost opened this issue Mar 9, 2019 · 14 comments
Closed

include php file in my ,md #489

ghost opened this issue Mar 9, 2019 · 14 comments

Comments

@ghost
Copy link

ghost commented Mar 9, 2019

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
Copy link
Collaborator

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
Copy link
Author

ghost commented Mar 9, 2019 via email

@PhrozenByte
Copy link
Collaborator

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
Copy link

stale bot commented Mar 17, 2019

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! 👍

@stale stale bot added the info: Stale label Mar 17, 2019
@stale stale bot closed this as completed Mar 19, 2019
@kenotrajang
Copy link

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
Copy link
Collaborator

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 #489 (comment) - the PHP file with the function definition is included on line 7, the function is later called on line 13.

@kenotrajang
Copy link

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
Copy link
Collaborator

PhrozenByte commented Oct 8, 2019

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

@kenotrajang
Copy link

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
Copy link

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
Copy link
Collaborator

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
Copy link

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
Copy link
Collaborator

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
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants