Download the PHP package zenstruck/twig-service-bundle without Composer

On this page you can find all versions of the php package zenstruck/twig-service-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 twig-service-bundle

zenstruck/twig-service-bundle

CI codecov

Making data from your app's services available in twig templates can be done by either:

  1. Injecting the service/data into the template when rendering.
  2. Creating a twig extension that has access to the service/data.

For #1, this isn't always a viable option (ie you need this data in your layout). With #2, there is a bit of boilerplate and if done incorrectly (ie not using a runtime or service proxy for heavy services), it could lead to performance issues.

This bundle provides an easy way to make functions, static methods, service methods, and even full service objects available in your twig templates.

Installation

[!NOTE] If not added automatically by symfony/flex, enable ZenstruckTwigServiceBundle.

Usage

[!NOTE] The output for the following functions/filters will be escaped. If your function\filter returns html that you don't want escaped, use the |raw filter.

Service Methods as Functions/Filters

You can mark any public method in your configured services with the #[AsTwigFunction] attribute to make them available within your twig templates with the fn() twig function/filter:

In your twig template, use the fn() function/filter to call:

Dynamic functions/filters are made available. The following is equivalent to above:

User Defined as Functions/Filters

You can mark any of your custom functions with the #[AsTwigFunction] attribute to make them available within your twig templates with the fn() twig function\filter:

In your twig template, use the fn() function/filter to call:

Dynamic functions/filters are made available. The following is equivalent to above:

3rd-Party Functions/Filters

If you need to make functions, static/service methods available in your twig templates for code you do not control (ie internal PHP functions/3rd party package), you can configure these in the bundle config:

Service Function

Mark any service you'd like to make available in twig templates with the #[AsTwigService].

[!NOTE] While you can mark any service as a twig service, it is not recommended to mark services that have nothing to do with templating (ie repositories) as such. You can think of twig services as lightweight-lazy-twig-extension-functions whose purpose is to break up/simplify large custom twig extensions.

You're now ready to access the service in any twig template:

Each service alias is made available as a dynamic function. The following is equivalent to above:

Invokable Service Filters

You can turn any twig service into a twig filter by having it implement __invoke():

In your template, use the service twig filter:

Each service alias is made available as a dynamic filter. The following is equivalent to above:

Parameter Function

You can access any service container parameter with the provided parameter() twig function:

zenstruck:twig-service:list Command

Use this command to list all functions/filters/services configured by this bundle and available in your twig templates.

[!NOTE] This command is only available when debug: true.

Full Default Bundle Configuration


All versions of twig-service-bundle with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
symfony/framework-bundle Version ^5.4|^6.0|^7.0
symfony/twig-bundle Version ^5.4|^6.0|^7.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 zenstruck/twig-service-bundle contains the following files

Loading the files please wait ....