Download the PHP package mediagone/twig-powerpack without Composer

On this page you can find all versions of the php package mediagone/twig-powerpack. 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-powerpack

Twig PowerPack

⚠️ This project is in experimental phase.

Latest Version on Packagist Total Downloads

This package provides code-quality features to your Twig templates:

  1. Type-safety checks for template context variables.
  2. Register global data/resources from any template.
  3. Instantiate classes in templates.

Along with new functionalities:

Installation

This package requires PHP 7.4+ and Twig 2+.

Add it as Composer dependency:

If you're using Symfony, enable the extension in services.yaml:

Introduction

Twig templating engine is seriously lacking types....

Features

1) Context Variables type-checking

Templates usually require specific external data, but there is no native way to check the type of supplied variables. The expect tag allows you to declare required variables in your Twig files, making them also self-documenting. If the data is invalid, an exception will be thrown.

Primitive types

Supported scalar types are: bool, float, int and string.

Note: TITLE, ENABLED, AMOUNT and COUNT represent the names of required variables.

Objects

Because they don't guarantee any data structure, anonymous objects (stdClass) are not supported. However, usage of named classes is strongly encouraged to expose data in your templates. Therefore, a Fully Qualified Class Name (FQCN) can also be supplied:

Nullable

Sometimes, you may want to ensure that a variable is defined while making it optional by using the nullable keyword:

Arrays

You can also check if a variable is an array of a given type by using the array of keywords:

Arrays can also be nullable:

Or contain nullable elements:

And even nullable array of nullable elements!

Note: Checking array's items type might induce a slight overhead, but unless you have thousands of elements it should be negligible.


2) Register global data from any template

You may occasionally declare specific data in your templates, used in the global scope. For example if your templates dynamically add CSS classes to HTML body, or if they require optional CSS or JavaScript resources you only want to include on demand.

String Data

Short string data can be registered from anywhere in your templates using the {% register <data> in <registry> %} tag:

And retrieved elsewhere through the registry() function:

Optional registry clause

For convenience, the registry name can be automatically inferred from the data when it represents a path with an extension, making usage of in <registry> optional. The following lines are equivalent:

Body Data

Because you may need longer or dynamically generated data, the tag also supports a block syntax to allow a content body to be provided. In this case you cannot define data in the opening tag and the registry clause is mandatory: {% register in <registry> %} <body data> {% endregister %}

For example if you want to declare inline scripts from a template:

And include it at the end of the html page:

Unicity

Data can be declared as unique, so if multiple templates register the same value, it will be included only once. It's required most of the time, just add the once keyword to the tag:

It also works with body data:

However, unicity is only enforced within the same registry, so both following statements will be taken into account:

Priority

As you cannot always predict in which order data will be registered, you'll sometime need to ensure a data comes first, for example in the case of a script library required by others. Then, add the priority keyword at the end of your tag followed by a priority number (lower values come first*).

Tags without priority always come after prioritized ones.

Note: the order of data with the same priority (or undefined) is not guaranteed.


3) Instantiate classes in templates

Although it's better to do it in the controller when possible, you may need to create class instances directly in a template. The new(string $fqcn, ...$args) function allows you to call the constructor of a given class:

Given the following View Model class:

License

Twig PowerPack is licensed under MIT license. See LICENSE file.


All versions of twig-powerpack with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4|^8.0
ext-json Version *
twig/twig Version ^2.12|^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 mediagone/twig-powerpack contains the following files

Loading the files please wait ....