Download the PHP package squirrelphp/twig-php-syntax without Composer

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

PHP Syntax for Twig

Test Coverage PHPStan Packagist Version PHP Version

Enables syntax known from PHP in Twig, so PHP developers can more easily create and edit Twig templates. This is especially useful for small projects, where the PHP developers end up writing Twig templates and it is not worth it to have a slightly different syntax in your templates.

Installation

composer require squirrelphp/twig-php-syntax

Configuration

Add PhpSyntaxExtension to Twig:

You can also have a look at the extension definition and create your own extension class to only include some of the features, if you do not like all of them.

Symfony integration

If you use autoconfigure (which is the default) you just need to load the PhpSyntaxExtension class in services.yaml in the config directory of your project (the first four lines should already be there, just add the line with the PhpSyntaxExtension class at the end of the file):

If you do not use autoconfigure, you can add the twig extension tag to the service definition:

Features

=== / !== strict comparison operators

Twig has the same as test, which mimicks === in PHP, but has a syntax that can be hard to get used to. Using the strict comparison operators from PHP (=== and !==) reduces friction, is familiar and less verbose.

strtotime filter

Comparing timestamps in templates when the data only has (date) strings is a bit cumbersome in Twig, as there is no strtotime filter - this library adds it exactly as it is in PHP:

foreach loops

Twig uses for to create loops, with a slightly different syntax compared to foreach in PHP. With this library foreach becomes available in Twig with the same syntax as in PHP:

Internally it behaves the exact same way as for: it actually creates ForNode elements, so you have the same functionality like in for loops, including the loop variable and else. else works the same as with for:

break and continue

Sometimes it can be convenient to break loops in Twig, yet there is no native support for it. This library adds break and continue and they work exactly as in PHP:

You can use break with a number to break out of multiple loops, just like in PHP: (continue does not support this)

While you can often circumvent the usage of break and continue in Twig, it sometimes leads to additional nesting and more complicated code. Just one break or continue can clarify behavior and intent in these instances. Yet I would advise to use break and continue sparingly.

Variable type tests (string, array, true, callable, etc.)

Adds tests known from PHP, so you can test a value for being:

Convert to type: intval, strval, floatval and boolval filters

Converting a variable to a specific type is not something Twig encourages and it probably should be avoided, if possible. Yet there are situations where you just want to convert something to an integer or string so you can be sure a comparison is type safe or that there is no unexpected behavior because one value has the wrong type.

These filters mainly behave like the ones in PHP (and use the corresponding PHP functions internally), but there is some additional behavior to detect or avoid likely errors:

&& and ||

If you want to make expressions even more like PHP, you can use && instead of and and || instead of or. This might be the least useful part of this library, as and and or are already short and clear, yet it is another easily remedied difference between Twig and PHP, and && and || can be easier to spot in comparison to and and or.


All versions of twig-php-syntax with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0.2
twig/twig Version ^3.15
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 squirrelphp/twig-php-syntax contains the following files

Loading the files please wait ....