gornymedia/laravel-shortcodes

Laravel shortcodes package

v1.3 2019-03-12 11:39 UTC

This package is not auto-updated.

Last update: 2024-11-13 07:17:23 UTC


README

Laravel Shortcodes Package

Laravel compatibility

Installation

Require this package in your composer.json and update composer.

"gornymedia/laravel-shortcodes": "^1.3",

Add the service provider to config/app.php

Gornymedia\Shortcodes\ShortcodesServiceProvider::class,

Add the class alias to config/app.php

'Shortcode' => Gornymedia\Shortcodes\Facades\Shortcode::class,

Usage

Creating shortcodes

Inside a service provider you can create the shortcodes with attributes.

use Gornymedia\Shortcodes\Facades\Shortcode;

Shortcode::add('example', function($atts, $content, $name)
{
  $a = Shortcode::atts([
    'name' => $name,
    'foo' => 'something',
    ], $atts);
    
  return "foo = {$a['foo']}";
});

Usage : [example foo="something else"]

Include partial files in shortcode

use Gornymedia\Shortcodes\Facades\Shortcode;

Shortcode::add('widget', function($atts, $content, $name) 
{
 $a = Shortcode::atts([
  'name' => $name,
  'foo' => 'something'
  ], $atts);
  
 $file = 'partials/' . $a['name'] ; // ex: resource/views/partials/ $atts['name'] .blade.php
 
 if (view()->exists($file)) {
  return view($file, $a);
 }
});

Usage : [widget name="maps"]

Compile shortcodes inside shortcode content

use Gornymedia\Shortcodes\Facades\Shortcode;

Shortcode::add('strong', function($atts, $content, $name) {
 $content = Shortcode::compile($content);
 
 return "<strong>$content</strong>";
});

Usage: [strong][example][/strong]

View rendering

compileShortcodes()

To render the view and compile the Shortcodes:

return view('view')->compileShortcodes();

stripShortcodes()

To render the view and remove the Shortcodes

return view('view')->stripShortcodes();

License

This package is open-source software licensed under MIT License.

This package uses WordPress shortcodes methods. The license under which the WordPress software is released is the GPLv2 (or later).