gornymedia/laravel-shortcodes

Laravel 短代码包

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

This package is not auto-updated.

Last update: 2024-09-18 06:33:19 UTC


README

Laravel Shortcodes Package

Laravel 兼容性

安装

在您的 composer.json 中添加此包并更新 composer。

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

将服务提供者添加到 config/app.php

Gornymedia\Shortcodes\ShortcodesServiceProvider::class,

将类别名添加到 config/app.php

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

用法

创建短代码

在服务提供者内部,您可以使用属性创建短代码。

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"]

在短代码中包含部分文件

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"]

在短代码内容内编译短代码

use Gornymedia\Shortcodes\Facades\Shortcode;

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

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

视图渲染

compileShortcodes()

渲染视图并编译短代码

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

stripShortcodes()

渲染视图并移除短代码

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

许可证

本包是开源软件,根据 MIT 许可证授权。

本包使用了 WordPress 短代码方法。WordPress 软件发布的许可证是 GPLv2(或更高版本)。