Download the PHP package brightnucleus/shortcodes without Composer
On this page you can find all versions of the php package brightnucleus/shortcodes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brightnucleus/shortcodes
More information about brightnucleus/shortcodes
Files in brightnucleus/shortcodes
Package shortcodes
Short Description Config-driven WordPress shortcodes.
License MIT
Informations about the package shortcodes
Bright Nucleus Shortcodes Component
Config-driven WordPress shortcodes.
This is a WordPress shortcodes component that lets you define shortcodes through a config file, complete with dependencies, localization and Shortcake UI.
Table Of Contents
- Installation
- Basic Usage
- Configuration Schema
- Registering A Basic Shortcode
- Configuration File
- Template File
- Initialization
- Using Custom Classes
- Using Relative Views
- Adding Additional Context
- Using An External DI
- Contributing
- License
Installation
The best way to use this component is through Composer:
Basic Usage
To use this component, you'll need to:
- instantiate the
ShortcodeManager
class; - inject an object implementing the
BrightNucleus\Config\ConfigInterface
through its constructor; - call its
register()
method.
Configuration Schema
Registering a basic shortcode
For the following example, we'll register a new shortcode that provides a simple [button]
shortcode. We want the shortcode to be configurable through Shortcake.
Configuration File
First, we need to define the shortcode through a configuration file.
Template file
Then, we'll need to write a template that can be rendered by the shortcode.
Initialization
Finally, we'll need to initialize the ShortcodeManager
with the configuration file, and let it register()
its shortcodes.
Using Custom Classes
The actual implementations to be used for the following interfaces can be changed through the Config files:
BrightNucleus\Shortcode\ShortcodeInterface
BrightNucleus\Shortcode\ShortcodeAttsParserInterface
BrightNucleus\Shortcode\ShortcodeUIInterface
The Config files accepts a key for overriding each of these. You can pass either a fully qualified class name or a callable that acts as a factory.
When using a callable, the arguments that are passed to that callable are the same as the constructor gets for the default implementation of each of these.
Using Relative Views
The underlying implementation uses the brightnucleus/view
package to render the actual views for each shortcode. The default behavior already deals with absolute paths and can render any type of views that the Views engine being used can handle. If no specific instance of a ViewBuilder
was injected, then the ShortcodeManager
will rely on the one provided by the Views
Facade.
To adapt the locations that the view engine looks in for relative view URIs or to configure the available rendering engines, you can either adapt the instance centrally available through the Facade, or, preferably, inject your own custom ViewBuilder
instance into the ShortcodeManager
as the third constructor argument.
Once you've added one or more locations in this way, you can use relative URIs in your config file. They do not even need to contain the extension, as long as that can be inferred by the engines that are known to the view builder. This makes overriding the views later on very flexible, in that you can not only override shortcode markup in your theme that was defined in your plugin, but you can also use a different engine than was originally use.
Refer to the brightnucleus/view
documentation on how to go about configuring a ViewBuilder
instance.
Adding Additional Context
The default implementation allows you to add_context()
to the shortcode, which will then also be passed to the view to be rendered.
This can be with a dependency injector, for example, to further prepare a shortcode with additional data after it has already been instantiated.
Using An External DI
To use an external dependency injector with the default Shortcode
implementation, you can pass an object to the Shortcode::with_injector()
method that provides a make( $class, $args )
method.
You might need to provide an adapter to modify the $args
argument as needed.
Contributing
All feedback / bug reports / pull requests are welcome.
License
Copyright (c) 2016 Alain Schlesser, Bright Nucleus
This code is licensed under the MIT License.
All versions of shortcodes with dependencies
brightnucleus/exceptions Version >=0.2
brightnucleus/dependencies Version >=0.2
brightnucleus/contracts Version >=0.1
gamajo/template-loader Version >=1.2
brightnucleus/invoker Version >=0.2.0
brightnucleus/view Version >=0.4.2