Download the PHP package webfactory/shortcode-bundle without Composer

On this page you can find all versions of the php package webfactory/shortcode-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package shortcode-bundle

WebfactoryShortcodeBundle

A Symfony bundle to resolve [shortcode] markup in Twig templates, using the thunderer/Shortcode library.

It allows you to define shortcodes and their replacements in a jiffy. Shortcodes are special text fragments that can be replaced with other content or markup. E.g. a user could use the following in a comment:

In analogy to living style guides, this bundle provides a shortcode guide that lists all registered shortcodes with an optional description and example.

Installation

As usual, install via Composer and register the bundle in your application:

composer require webfactory/shortcode-bundle

Usage

Twig Filter

The bundle will set up a shortcodes Twig filter. What you pass through this filter will be processed by the Processor class (see docs).

Using Controllers as Shortcode Handlers

This bundle comes with a helper class that allows to use Symfony's Fragment Sub-Framework and the technique of embedding controllers to have controllers generate the replacement output for shortcodes.

To give an example, assume the following configuration:

Then, when doing something like this in Twig:

... the AppBundle\Controller\EmbeddedImageController::show() controller method will be called. Additional shortcode attributes, like url in the above example, will be passed as parameters to the controller. The response returned by the controller will be used to replace the shortcode in the given content. The controller can generate the response directly, or use Twig to render a template to create it.

Rendering with Edge Side Includes

You can also use ESI rendering for particular shortcodes. The advantage of ESI is that single shortcode replacements can be stored in edge caches and/or reverse proxies like Varnish and possibly be reused on multiple pages.

⚠️ Take care: Due to the way ESI works, the (master) Request visible to controllers is no longer the one where the shortcode was used. Keep that in mind when you, for example, want to log the URLs where shortcodes are being used.

To use ESI-based embedding for a particular shortcode, use a configuration like the following:

Registering Handlers as Services

In the thunderer/Shortcode package, handlers transform shortcodes into desired replacements. You can register services from the Symfony Dependency Injection Container to be used as shortcode handlers by tagging them with webfactory.shortcode and adding a shortcode attribute to the tag indicating the shortcode name.

Removing <p> Tags around Shortcodes

By default, the RemoveWrappingParagraphElementsEventHandler contained in this bundle will be used to remove <p>...</p> tags around shortcodes, if the shortcode is the only text content in that paragraph.

Activating the Shortcode Guide

The optional Shortcode Guide is a controller providing an overview page of all configured shortcodes. For every shortcode, there is also a detail page including a rendered example.

To use the Shortcode Guide, include the routing configuration from @WebfactoryShortcodeBundle/Resources/config/guide-routing.xml.

⚠️ You probably want to do this only for your Symfony dev and/or test environment, and possibly restrict access in your security configuration in addition to that.

With the route prefix defined as above, visit /shortcodes/ to see a list of all defined shortcodes. If you want to add descriptions to shortcodes and/or provide the example shortcode that shall be rendered on the detail page, you can add this information when configuring shortcodes:

Other Configuration Parameters

In most cases, the default values should work fine. But you might want to configure something else, e.g. if the default parser needs too much memory for a large snippet. See thunderer's documentation on parsing and configuration so you understand the advantages, disadvantages and limitations:

Testing your Shortcodes

This section provides a few hints and starting pointers on testing your shortcode handlers and bundle configuration.

Direct Unit Tests

In general, try to start with unit testing your shortcode handlers directly.

No matter whether your handler is a simple class implementing the __invoke magic method or a Symfony Controller with one or several methods: Direct unit tests are the easiest way to have full control over the handler's (or controller's) input, and to get immediate access to its return value. This allows you to test also a broader range of input parameters and verify the outcomes. In this case, you will typically use Mock Objects to substitute some or all other classes and services your handler depends upon.

If your shortcode handler produces HTML output, the Symfony DomCrawler might be helpful to perform assertions on the HTML structure and content.

Functional Tests for shortcode-handling Controllers

When using a controller to handle a shortcode, and the controller uses Twig for rendering, you might want to do a full functional (integration) test instead of mocking the Twig engine.

The Symfony documentation describes how Application Tests can be performed. This approach, however, is probably not suited for your shortcode controllers since these typically are not reachable through routes and so you cannot perform direct HTTP requests against them.

Instead, write an integration test where you retrieve the controller as a service from the Dependency Injection Container and invoke the appropriate method on it directly. Then, just like described in the section before, perform assertions on the Response returned by the controller.

Here is an example of what a test might look like.

Testing Configuration

After you have written some tests that verify your handlers work as expected for different input parameters or other circumstances (e. g. database content), you also want to make sure a given handler is registered correctly and connected with the right shortcode name. Since we are now concerned with how this bundle, your configuration and your handlers all play together, we're in the realm of integration testing. These tests will be slower, since we need to boot a Symfony Kernel, fetch services from the Dependency Injection Container and test how various parts play together.

This bundle contains the \Webfactory\ShortcodeBundle\Test\ShortcodeDefinitionTestHelper class and a public service of the same name. Depending on the degree of test specifity you prefer, you can use this service to verify that...

For all these tests, you probably need to use KernelTestCase as your test base class (documentation). Basically, you will need to boot the kernel, then get the ShortcodeDefinitionTestHelper from the container and use its methods to check your shortcode configuration.

Maybe you want to have a look at the tests for ShortcodeDefinitionTestHelper itself to see a few examples of how this class can be used.

Remember – this type of test should not test all the possible inputs and outputs for your handlers; you've already covered that with more specific, direct tests. In this test layer, we're only concerned with making sure all the single parts are connected correctly.

Full End-to-End Tests

If, for some reason, you would like to do a full end-to-end test for shortcode processing, from a given string containing shortcode markup to the processed result, have a look at the \Webfactory\ShortcodeBundle\Test\EndToEndTestHelper class.

This helper class can be used in integration test cases and will do the full shortcode processing on a given input, including dispatching sub-requests to controllers used as shortcode handlers.

A test might look like this:

Assuming that your application configuration registers a handler for the text shortcode, which might also be a controller, this test will perform a full-stack test.

Credits, Copyright and License

This bundle was started at webfactory GmbH, Bonn.

Copyright 2018-2023 webfactory GmbH, Bonn. Code released under the MIT license.


All versions of shortcode-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
ext-json Version *
ext-mbstring Version *
psr/log Version ^1|^2|^3
symfony/config Version ^5.4|^6.0|^7.0
symfony/dependency-injection Version ^5.4|^6.0|^7.0
symfony/deprecation-contracts Version ^2.5|^3.0
symfony/form Version ^5.4|^6.0|^7.0
symfony/http-foundation Version ^5.4|^6.0|^7.0
symfony/http-kernel Version ^5.4|^6.0|^7.0
thunderer/shortcode Version ^0.6.5|^0.7
twig/twig Version ^2.0|^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package webfactory/shortcode-bundle contains the following files

Loading the files please wait ....