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><b>Hello World</b></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
);
}
}