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

Snippets not working #2

Closed
bricebou opened this issue Apr 21, 2015 · 8 comments
Closed

Snippets not working #2

bricebou opened this issue Apr 21, 2015 · 8 comments

Comments

@bricebou
Copy link

Hi,

I'm trying to set up a small new project using Phile in which I have to display an audio player. I tried to use your plugin but I can't make it work and I have no errors from Phile.

I'm working locally (on a Xubuntu up to date). I've just installed Phile using composer

composer create-project --no-dev phile-cms/phile

I've installed your plugin using composer:

composer require infostreams/snippets:*

and updated my config file plugin section:

$config['plugins'] = array(
    'infostreams\\snippets' => array('active' => true)
);

In a content index.md when I try to use

(youtube: http://www.youtube.com/watch?v=mSB71jNq-yQ)

(youtube: https://www.youtube.com/watch?v=mSB71jNq-yQ width:640)

(link: cnn.com)

(file: Catherine_awards.xls text:our brochure)

I only got the same, I mean it isn't parse.

I've tried to deactivate the cache from the default config but without success ; here is the plugin section from the default:

$config['plugins'] = array(
    // key = vendor\\pluginName (vendor lowercase, pluginName lowerCamelCase
    'mycompany\\demoPlugin'            => array('active' => false),
    'phile\\errorHandler'              => array(
        'active' => true,
        'handler' => \Phile\Plugin\Phile\ErrorHandler\Plugin::HANDLER_DEVELOPMENT
    ), // the default error handler
    'phile\\parserMarkdown'            => array('active' => true), // the default parser
    'phile\\parserMeta'                => array('active' => true), // the default parser
    'phile\\templateTwig'              => array('active' => true), // the default template engine
    'phile\\phpFastCache'              => array('active' => false), // the default cache engine
    'phile\\simpleFileDataPersistence' => array('active' => false), // the default data storage engine
);

I've tried to use the Github version but without success.

It seems I have the 1.5.0 version of Phile (from the Changelog) and here is my composer.json if it can help:

{
    "name": "phile-cms/phile",
    "type": "project",
    "description": "A file-based CMS with a swappable parser, template engine, cache and storage services, error handler, and meta parser.",
    "keywords": ["cms", "markdown", "flat", "file", "twig", "plugins"],
    "homepage": "http://philecms.com",
    "license": "MIT",
    "authors": [
        {
            "name": "Frank Nägler",
            "email": "mail@naegler.net",
            "homepage": "http://www.frank-naegler.de",
            "role": "Developer"
        },
        {
            "name": "James Doyle",
            "email": "james2doyle@gmail.com",
            "homepage": "http://ohdoylerules.com/",
            "role": "Developer"
        },
        {
            "name": "Schlaefer",
            "email": "openmail+sourcecode@siezi.com",
            "homepage": "http://siezi.com/",
            "role": "Developer"
        }
    ],
    "config": {
        "vendor-dir": "lib/vendor"
    },
    "require": {
        "php": ">=5.4.0",
        "twig/twig": "1.15.*",
        "michelf/php-markdown": ">=1.4",
        "phpfastcache/phpfastcache": "~3.0",
        "phile-cms/plugin-installer-plugin": ">=1.0.2",
        "gibbs/phile-sub-navigation": "1.*",
        "infostreams/snippets": "*"
    },
    "require-dev": {
        "phpdocumentor/phpdocumentor": "2.6.*",
        "phing/phing": "2.*",
        "phpunit/phpunit": "*"
    }
}

Thanks in advance.

@bricebou
Copy link
Author

Hi again,

I've intalled the phileMarkdownExtraExtended plugin ; in order to do so I had to add to the composer.json this line

"minimum-stability": "dev"

I've launched a composer update but even after a lot of upgrades, your plugin doesn't work :(

Thanks in advance,

@infostreams
Copy link
Owner

Hi! It seems you were completely right. With the new version 1.5 Phile changed the way it initializes plugins, and how you should register events, and that broke the plugin. I've updated it and released a new version on composer, so that it should work again. Can you do a composer update and let me know if that fixed the problem? Thanks!

@bricebou
Copy link
Author

Great ! The update works :)

Two question :

  • is it possible to create a snippet generating both javascript and html ? Any particular precautions ?
  • just to check if I understand well : new snippets are defined in the config.php ?

Thanks !

@infostreams
Copy link
Owner

Awesome! Regarding your questions:

  1. You can pretty much generate anything you want, as long as it's text. So javascript, html, css, svg, and xml are all very much possibilities.
  2. Yes, any new snippets need to be registered and made available to the plugin, and you can only do that via the configuration file (config.php). If you have more than a few snippets I'd suggest to use a Snippets class, because otherwise your config.php will become a mess.

Cheers,
Ed

@bricebou
Copy link
Author

I've tried and it works perfectly !

In the second case, using a Snippets class : where do we have to save it ? I haven't really understand that in your README.

More questions after my test:

  • is there a way to get the base_url variable when defining a snippet ?
  • is there a way to pass the current url to the snippet without having to write it, as in this example:
(sound: content/ec_01/effet_coleoptere_01_sex_drugs_rocknroll.mp3 text:Sex, Drugs & Rock\'n\'roll)

Thanks again.

@infostreams
Copy link
Owner

We're not really talking about the bug anymore, but I'll allow it for now (and then close this ticket).

Regarding where to put your Snippets class - you can either do a require_once('<somefolder>/MySnippets.php'); in your index.php or config.php, but that's pretty dirty. Instead, you should probably follow this procedure to have it autoloaded like the rest of Phile:

  1. First, think of a name (a single word) for the namespace. Use your own name, or your company name, or the name of your site. In this example I use mynamespace. Replace any occurrences with the name you've chosen.

  2. Update your composer.json to include the following (put it at the end of the file, directly after require or require-dev):

    "autoload": {
        "psr-0": {
            "<mynamespace>": "src/"
        }
    }

    My composer.json now reads something like:

    "require": {
        "php": ">=5.4.0",
        "twig/twig": "1.15.*",
        "michelf/php-markdown": ">=1.4",
        "phpfastcache/phpfastcache": "~3.0",
        "phile-cms/plugin-installer-plugin": ">=1.0.2",
        "infostreams/snippets": "1.*"
    },
    "require-dev": {
        "phpdocumentor/phpdocumentor": "2.6.*",
        "phing/phing": "2.*",
        "phpunit/phpunit": "*"
    },
    "autoload": {
        "psr-0": {
            "mynamespace": "src/"
        }
    }
  3. Run composer update (or equivalent) to have composer generate a new autoloader

  4. Open lib/vendor/composer/autoload_namespaces.php and check if it contains an entry for <mynamespace>

  5. If it does, create a directory named src in the root of your site, relative to where composer.json is. You can obviously name this directory anything you want, but I went with 'src'.

  6. In that directory, create a new directory named <mynamespace>.

  7. Add a file called MySnippets.php to the src/<mynamespace>/ directory. Put the following contents in that file:

    <?php
    namespace mynamespace;
    
    class MySnippets extends \Phile\Plugin\Infostreams\Snippets\Snippets {
    public function skype($name, $action="call", $text=null) {
        if (is_null($text)) {
            $text = "Contact '$name' on Skype";
        }
        return "<a href='skype:$name?$action'>$text</a>";
    }
    
    public function facebook($who, $attribute, $other_attribute) {
        // ...
    }
    
    public function scroller($pictures) {
        // ...
    }
    
    public function youtube_popup($link) {
        // ...
    }
    }
  8. Finally, in your site's config.php, add the following code:

    $config['plugins']['infostreams\\snippets']['snippets'] = array(
    new \mynamespace\MySnippets()
    );
  9. That should do it! You can now define your own snippets in the MySnippets class. If it doesn't work, read up on the Composer autoloader, for example here.

Regarding your other two questions: you can get the base URL with (new Router)->getBaseUrl() and the current URL with (new Router)->getCurrentUrl() - but since your original issue is resolved and these are basically questions about Phile itself I will now close this question.

Happy hacking!

Ed

@bricebou
Copy link
Author

Hi,

Thanks a lot, it works perfectly ; maybe you could add this howto on your readme for noobs like me :)

Thanks again !

@infostreams
Copy link
Owner

Cool, that's great to hear! I already linked to this comment from the project README, so hopefully people should be able to find it and save themselves some troubles.

Cheers,
Ed

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

No branches or pull requests

2 participants