PHP code example of tehwave / laravel-shortcodes

1. Go to this page and download the library: Download tehwave/laravel-shortcodes library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tehwave / laravel-shortcodes example snippets




use tehwave\Shortcodes\Shortcode;

$compiledContent = Shortcode::compile('[uppercase]Laravel Shortcodes[/uppercase]');

// LARAVEL SHORTCODES



namespace App\Shortcodes;

use tehwave\Shortcodes\Shortcode;

class ItalicizeText extends Shortcode
{
    /**
     * The code to run when the Shortcode is being compiled.
     *
     * You may return a string from here, that will then
     * be inserted into the content being compiled.
     *
     * @return string|null
     */
    public function handle(): ?string
    {
        if (isset($this->attributes['escape_html']) && $this->attributes['escape_html'] === 'true')) {
            return sprintf('<i>%s</i>', htmlspecialchars($this->body));
        }

        return sprintf('<i>%s</i>', $this->body);
    }
}



namespace App\Shortcodes;

use tehwave\Shortcodes\Shortcode;

class ItalicizeText extends Shortcode
{
    /**
     * The tag to match in content.
     *
     * @var string
     */
    protected $tag = 'italics';
}



use tehwave\Shortcodes\Shortcode;

$compiledContent = Shortcode::compile('[italics escape_html="true"]<b>Hello World</b>[/italics]');

// <i>&lt;b&gt;Hello World&lt;/b&gt;</i>



use tehwave\Shortcodes\Shortcode;

$shortcodes = collect([
    new ItalicizeText,
]);

$compiledContent = Shortcode::compile('[uppercase]Hello World[/uppercase]', $shortcodes);

// [uppercase]Hello World[/uppercase]



namespace App\Shortcodes;

use tehwave\Shortcodes\Shortcode;
use Carbon\Carbon;

class ExampleShortcode extends Shortcode
{
    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'is_active' => 'boolean',
        'count' => 'integer',
        'price' => 'float',
        'name' => 'string',
        'tags' => 'array',
        'options' => 'collection',
        'metadata' => 'object',
        'config' => 'json',
        'published_at' => 'date',
    ];

    /**
     * The code to run when the Shortcode is being compiled.
     *
     * @return string|null
     */
    public function handle(): ?string
    {
        $publishedAt = $this->attributes['published_at'] instanceof Carbon
            ? $this->attributes['published_at']->toFormattedDateString()
            : 'N/A';

        $tags = implode(', ', $this->attributes['tags']);

        $options = $this->attributes['options']->implode(', ');

        return sprintf(
            'Active: %s, Count: %d, Price: %.2f, Name: %s, Published At: %s, Tags: %s, Options: %s',
            $this->attributes['is_active'] === true ? 'Yes' : 'No',
            $this->attributes['count'],
            $this->attributes['price'],
            $this->attributes['name'],
            $publishedAt,
            $tags,
            $options
        );
    }
}



use tehwave\Shortcodes\Shortcode;

$compiledContent = Shortcode::compile('[example is_active="1" count="10" price="99.99" name="Sample" published_at="2023-06-29" tags=\'["tag1","tag2","tag3"]\' options=\'["option1","option2"]\']');

// Active: Yes, Count: 10, Price: 99.99, Name: Sample, Published At: Jun 29, 2023, Tags: tag1, tag2, tag3, Options: option1, option2



namespace App\Shortcodes;

use tehwave\Shortcodes\Shortcode;

class ExampleShortcode extends Shortcode
{
    protected $casts = [
        'is_active' => 'boolean',
        'count' => 'integer',
    ];

    public function handle(): ?string
    {
        // Access attributes as properties
        $isActive = $this->is_active;
        $count = $this->count;

        return sprintf('Active: %s, Count: %d', $isActive === true ? 'Yes' : 'No', $count);
    }
}

$parsedDescription = (new Parsedown())
    ->setSafeMode(true)
    ->setUrlsLinked(false)
    ->text($this->description);

$compiledDescription = Shortcode::compile($parsedDescription);
bash
php artisan make:shortcode ItalicizeText