Download the PHP package nystudio107/craft-twig-sandbox without Composer

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

Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status

Craft Twig Sandbox

Allows you to easily create a sandboxed Twig environment where you can control what tags, filters, functions, and object methods/properties are allowed

Requirements

Craft Twig Sandbox requires Craft CMS 5.x

Installation

To install Craft Twig Sandbox, follow these steps:

  1. Open your terminal and go to your Craft project:

    cd /path/to/project
  2. Then tell Composer to require the package:

    composer require nystudio107/craft-twig-sandbox

About Craft Twig Sandbox

Rather than just creating a new Twig Environment for the sandbox, Craft Twig Sandbox sub-classes the Craft View class, which has a few benefits:

It also implements an ErrorHandler that sub-classes the Craft ErrorHandler which is used to handle exceptions that happen when rendering Twig templates. This allows it to properly handle and display exceptions such as:

Additionally, if the Craft Closure package is installed, it will automatically be added to the sandbox for use in any of the Twig template rendering functions.

Using Craft Twig Sandbox

In its simplest form, you can create a Twig Sandbox like so:

This will create a new SandboxView that works just like the Craft web View class so you can use any of the View render methods for Twig templates:

...and they will be rendered using the default BlacklistSecurityPolicy so blacklisted Twig tags, filters, functions, and object methods/properties will not be allowed.

If any tags, filters, functions, or object methods/properties are used that are not allowed by the security policy, a SecurityError exception will be thrown.

N.B.: For performance reasons, you should create a SandboxView once, and use it throughout your application's lifecycle, rather than re-creating it every time you want to render Twig using it.

BlacklistSecurityPolicy

The BlacklistSecurityPolicy is a SecurityPolicy that specifies the Twig tags, filters, functions, and object methods/properties that are not allowed.

It defaults to reasonable subset of blacklisted Twig tags, filters, and functions, but you can customize it as you see fit:

You can also control what object methods and properties are allowed to be accessed. By default, the BlacklistSecurityPolicy does not restrict access to any object methods or properties.

For example, if you didn't want people to be able to access the password property of the DbConfig object via:

or

...you would do:

If you don't want any properties or methods to be able to be accessed on a given object, you can pass in a * wildcard:

WhitelistSecurityPolicy

The WhitelistSecurityPolicy is a SecurityPolicy that specifies the Twig tags, filters, functions, and object methods/properties that are allowed.

It defaults to reasonable subset of whitelisted Twig tags, filters, functions, and object methods/properties, but you can customize it as you see fit:

You can also control what object methods and properties are allowed to be accessed. By default, the WhitelistSecurityPolicy restricts access to all object methods or properties.

That means you must explicitly specify each object property or method.

For example, if you wanted to grant access to:

or

...you would do:

If you want all properties or methods to be able to be accessed on a given object, you can pass in a * wildcard:

Custom SecurityPolicy

You can also create your own custom SecurityPolicy to use, it just needs to conform to the Twig SecurityPolicyInterface:

Adding a SandbowView via config/app.php

If you want to make a Twig sandbox available globally in your Craft application, you can add the following to your config/app.php:

This will create a globally available component that you can use via:

You can even globally replace the default Craft view with a SandboxView if you want:

Craft Twig Sandbox Roadmap

Some things to do, and ideas for potential features:

Brought to you by nystudio107


All versions of craft-twig-sandbox with dependencies

PHP Build Version
Package Version
Requires craftcms/cms Version ^5.0.0
twig/twig Version ^3.0.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 nystudio107/craft-twig-sandbox contains the following files

Loading the files please wait ....