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

Lack of documentation #10

Open
dsfser opened this issue Feb 24, 2016 · 6 comments
Open

Lack of documentation #10

dsfser opened this issue Feb 24, 2016 · 6 comments

Comments

@dsfser
Copy link

dsfser commented Feb 24, 2016

Hello,

Could you please provide us with some more documentation... for example how to execute the ShortcodeTest . (what values do we need to send to the function testParse...)

and could you give some more explanation in the documentation. where goes the class? where goes the Shortcode::register and where goes the function...

@adkanojia
Copy link

+1

@dexbarrett
Copy link

dexbarrett commented Apr 27, 2016

@dsfser @akashdeepkanojia

This is an example of how I implemented the library in my Laravel project.

First of all, use version 2.1 instead of 2.2. The latest seems to not work well with the different ways of registering a shortcode.

Once you installed the library through Composer, add the service provider and the facade to your config/app.php file on their respective arrays:

service provider:

'providers' => [

        // list of other providers, omitted for brevity
        Pingpong\Shortcode\ShortcodeServiceProvider::class,

facade:

'aliases' => [

        // other aliases here, omitted for brevity
        'Shortcode'  => Pingpong\Shortcode\ShortcodeFacade::class,

Then you need to create a new service provider. This is where you will register your shortcodes. I named mine ShortCodeServiceProvider (how original) but you can name yours whatever you want:

php artisan make:provider ShortCodeServiceProvider

The new file will be placed under app/Providers/ShortCodeServiceProvider.php

Now register your new provider just like you did with the library's:

'providers' => [

        // list of other providers, omitted for brevity
        Pingpong\Shortcode\ShortcodeServiceProvider::class,
       YourAppNamespace\Providers\ShortCodeServiceProvider::class,

Then add the code to register your shortcodes. This is an example of how I have mine. I created a protected array with the shortcodes available for my app, then I have a function than spins through this array and registers each one. This structure assumes there is a class called app\Shortcode.php. This class has a method per shortcode, each method name matching the name of the shortcode.

First, the content of app\Providers\ShortCodeServiceProvider.php. AKA, where you register the shortcodes:

<?php

namespace YourAppNamespace\Providers;

use YourAppNamespace\ShortCode;
use Illuminate\Support\ServiceProvider;

class ShortCodeServiceProvider extends ServiceProvider
{
    protected $shortcodes = ['alert', 'tooltip'];

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerShortCodes();
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {

    }

    protected function registerShortCodes()
    {
        foreach ($this->shortcodes as $shortcode) {
            $this->app['shortcode']->register($shortcode, "YourAppNamespace\ShortCode@{$shortcode}");
        }
    }
}

And here, the class that contains the methods to process each shortcode. In this example I only registered two shortcodes: alert and tooltip. And this class has an alert method and a tooltip method:

app/ShortCode.php:

<?php
namespace YourAppNamespace;

class ShortCode {

    public function alert($attr, $content = null, $name = null)
    {
        $options = ['type' => 'info'];
        $options = array_merge($options, $attr);

        return '<div class="alert alert-' . $options['type'] . '">' . $content . '</div>';    
    }

    public function tooltip($attr, $content = null, $name = null)
    {
        return '<span class="post-tooltip" title="' .  $attr['caption'] . '">' . $content . '</span>';
    }
}

Now, whenever you need to parse some text containing shortcodes just use the Shortcode facade. Just make sure you import it at the top of your class or reference it globally with the forward slash. For example, in a controller:

<?php
namespace YourAppNamespace\Http\Controllers;

use Shortcode;

class SomeController extends Controller
{
    public function index()
    {
       $compiledText = Shortcode::compile('[myshortcode]shortcode text[/myshortcode]');
    }
}

And pretty much that's it. Hope it helps you implement it.

@efcoders
Copy link

efcoders commented May 6, 2016

i tried to use this example but its showing me

Shortcode handler must be callable

i have checked all name spaces and paths are correct

any help?

@dexbarrett
Copy link

dexbarrett commented May 6, 2016

@efcoders Which version of the library are you using? I mention at the beginning of
my comment that you should use version 2.1 because latest versions don't
seem compatible with the ways to register shortcodes mentioned in the docs.

On Friday, May 6, 2016, efcoders [email protected] wrote:

i tried to use this example but its showing me

Shortcode handler must be callable

i have checked all name spaces and paths are correct

any help?


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
#10 (comment)

@visuaware
Copy link

i have same issue, i did try install with "2.1.0" as version, but i get a ton of errors with Conclusion dont install laravel framework (many version) and
pingpong/shortcode v2.1.0 requires illuminate/container 5.1.* -> satisfiable by illum
5.1.16, v5.1.2, v5.1.20, v5.1.22, v5.1.25, v5.1.28, v5.1.30, v5.1.31, v5.1.6, v5.1.8].

so guess i cant get older version that will install on laravel 5.2?

@wdmtech
Copy link

wdmtech commented Jun 14, 2016

@efcoders I had the same error - Try this if you're using Laravel 5:

$this->app->bind('shortcode', "App\\ShortCode@{$shortcode}");

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

6 participants