Download the PHP package performing/twig-components without Composer
On this page you can find all versions of the php package performing/twig-components. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download performing/twig-components
More information about performing/twig-components
Files in performing/twig-components
Package twig-components
Short Description Twig components extension
License MIT
Homepage https://github.com/giorgiopogliani/twig-components
Informations about the package twig-components
Twig components extension
This is a PHP package for automatically create Twig components as tags. This is highly inspired from Laravel Blade Components.
Installation
You can install the package via Composer:
Configuration
This package should work anywhere where Twig is available.
To enable the package just pass your Twig environment object to the function and specify your components folder relative to your Twig templates folder.
Configuration for Craft CMS
In Craft CMS you should do something like this:
The
if
statement ensure you don't get'Unable to register extension "..." as extensions have already been initialized'
as error.
Configuration for Symfony
In Symfony you can do something like this:
Configuration for October CMS / Winter CMS
In October CMS and Winter CMS you need to hook into cms.page.beforedisplay
event inside your plugin's boot method in order to access Twig instance.
Then you can use your plugin hint path to choose a views
subfolder as components' folder.
All features, like subfolders are supported. For example
<x-forms.input></x-forms.input>
will refer toplugins/namespace/pluginname/views/forms/input.twig
.
Usage
Components are just Twig templates in a folder of your choice (e.g. /components
) and can be used anywhere in your Twig templates:
The slot variable is any content you will add between the opening and the close tag.
To reach a component you need to use the dedicated tag x
followed by :
and the filename of your component without extension:
It will render:
Custom tags
The same behaviour can be obtained with a special HTML syntax.
Attributes
You can also pass any params like you would using an include
. The benefit is that you will have the powerful attributes
variable to merge attributes or to change your component behaviour.
With custom tags you can pass any attribute to the component in different ways. To interprate the content as Twig you need to prepend the attribute name with a :
, but it works also in other ways.
Subfolders
To reach components in subfolders you can use dot-notation syntax.
Named slots
In case of use of multiple slots you can name them.
Use with standard syntax:
Use with custom tags syntax:
Twig namespaces
In addition to the specified directory, you can also reference components from a Twig namespace by prepending the component name with <namespace>:
.
Use with standard syntax:
Use with custom tags syntax:
Dynamic components
Sometimes you may need to render a component but not know which component should be rendered until runtime.
In this situation, you may use the built-in dynamic-component
to render the component based on a runtime value or variable:
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Testing
License
The MIT License (MIT). Please see License File for more information.