gornymedia / laravel-shortcodes by gornymedia

Laravel shortcodes package
32,587
24
1
Package Data
Maintainer Username: gornymedia
Maintainer Contact: tom@gornymedia.com (Tom Gorny)
Package Create Date: 2015-02-22
Package Last Update: 2019-03-12
Language: PHP
License: MIT
Last Refreshed: 2024-11-15 15:25:50
Package Statistics
Total Downloads: 32,587
Monthly Downloads: 343
Daily Downloads: 27
Total Stars: 24
Total Watchers: 1
Total Forks: 4
Total Open Issues: 1

Laravel-Shortcodes

Laravel Shortcodes Package

Laravel compatibility

Laravel | Shortcodes :---------|:---------- 5.8.x | 1.3.x 5.5.x | 1.2.x 5.4.x | 1.1.x 5.3.x | 1.1.x 5.2.x | 1.1.x 5.1.x | 1.0.x 5.0.x | 1.0.x

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).