Laravel Shortcodes

repository·master·Indexed 42 days ago

https://github.com/webwizo/laravel-shortcodes

Provides WordPress-like shortcode functionality for Laravel applications, allowing developers to use bracketed tags within Blade templates or Mailables that are compiled into HTML. Includes a Shortcode facade for registration and management, an Artisan make:shortcode command for scaffolding, and support for nested tags and attributes.

Tokens
3.4K
Snippets
24
Records
26
Agent score
53%

What's inside Laravel Shortcodes

  1. Understand shortcode syntax and parsing rules

    master

    The compiler scans templates from left to right. Key behaviors include:

    • Attributes: Supports quoted values with spaces and hyphenated keys (e.g., data-size="large").
    • Void / self-closing: Tags without a matching closing tag (e.g., [alert type="info"]) are treated as self-closing.
    • Nested same-name tags: Uses balanced opening/closing pairs to preserve innermost structures.
    • Unknown [ text: Text in square brackets that doesn't match a registered shortcode is left untouched.
    • Escaping: Use double brackets [[b]] to output a literal [b].
  2. Register new shortcodes

    master

    To register shortcodes, create a ServiceProvider and use the Shortcode::register() method in the register() or boot() method. You can register a class that implements a default register method, or a class with a specific custom method.

    // In a ServiceProvider
    public function register()
    {
        // Registering a class with a default 'register' method
        Shortcode::register('b', BoldShortcode::class);
    
        // Registering a class with a specific custom method name
        Shortcode::register('i', 'App\Shortcodes\ItalicShortcode@custom');
    }
  3. Enable shortcodes in Mailables

    master

    You can enable shortcode compilation for mailable views by calling withShortcodes() within the build() method of your Mailable class.

    use Illuminate\Mail\Mailable;
    
    class NewsletterMail extends Mailable
    {
        public function build()
        {
            return $this
                ->subject('Weekly newsletter')
                ->view('emails.newsletter', [
                    'content' => 'Mail content',
                ])
                ->withShortcodes();
        }
    }
  4. Register the Shortcodes Service Provider manually

    master

    If your Laravel setup does not support automatic package discovery, add the Webwizo\Shortcodes\ShortcodesServiceProvider to your config/app.php or bootstrap/providers.php file.

    Webwizo\Shortcodes\ShortcodesServiceProvider::class,
  5. Implement a shortcode class

    master

    Shortcode classes receive several arguments in their callback method: $shortcode (the attribute object), $content (inner content), $compiler, $name, and $viewData. Use $shortcode->get($key, $default) to safely retrieve attributes.

    namespace App\Shortcodes;
    
    class BoldShortcode {
      public function register($shortcode, $content, $compiler, $name, $viewData)
      {
        // Using get() to handle attributes with a fallback
        $class = $shortcode->get('class', 'default');
        return sprintf('<strong class="%s">%s</strong>', $class, $content);
      }
    }