Code Monkey home page Code Monkey logo

twig-bridge's Introduction

Spiral Framework: Twig Adapter

PHP Version Require Latest Stable Version phpunit psalm Codecov Total Downloads type-coverage psalm-level

Documentation | Framework Bundle

Installation

The extension requires spiral/views package.

composer require spiral/twig-bridge

To enable extension modify your application by adding Spiral\Twig\Bootloader\TwigBootloader:

class App extends Kernel
{
    /*
     * List of components and extensions to be automatically registered
     * within system container on application start.
     */
    protected const LOAD = [
        // ...
        
        Spiral\Twig\Bootloader\TwigBootloader::class,
    ];
}

Configuration

You can enable any custom twig extension by requesting Spiral\Twig\TwigEngine in your bootloaders:

class TwigExtensionBootloader extends Bootloader 
{
    public function boot(TwigEngine $engine)
    {
        $engine->addExtension(new Extension());
    }
}

Lazy Configuration

To configure TwigEngine on demand use functionality provided by TwigBootloader:

class TwigExtensionBootloader extends Bootloader 
{
    public function boot(TwigBootloader $twig)
    {
        $twig->addExtension('container.binding');
    }
}

Note Following methods are available setOption, addExtension, addProcessor.

License:

MIT License (MIT). Please see LICENSE for more information. Maintained by Spiral Scout.

twig-bridge's People

Contributors

anlamas avatar butschster avatar msmakouz avatar serafimarts avatar sergey-telpuk avatar sergeytelpuk avatar stylecibot avatar wolfy-j avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

twig-bridge's Issues

Twig autoescaping not working like expected

Currently I can not create a template like base.js.twig or base.html.twig.

It ends up in:

[Twig\Error\SyntaxError] An exception has been thrown during the compilation of a template ("Unable to load view default:base.html, file does not exist.") in "default:base.html". in /tmp/schranz-templating/examples/usages/spiral/vendor/twig/twig/src/Environment.php:521

I did try to adopt the TwigLoader here:

return new Source(
$source->getCode(),
\sprintf('%s:%s', $source->getNamespace(), $source->getName()),
$source->getFilename()
);

By changing this:

-            \sprintf('%s:%s', $source->getNamespace(), $source->getName()),
+            \sprintf('%s:%s', $source->getNamespace(), $source->getName() . '.twig'),

This way the twig template is rendered. But still not the correct escaper is used.

For example when creating a test.js.twig file:

<script>
    var test = 'test';

    {% set someHtml = '<h1>' ~ title ~ '</h1>' %}

    var test = "{{ someHtml }}";

    alert(test);
</script>

It is not escaped as expected:

Spiral:

Uses still HTML escaper:

Bildschirmfoto 2022-11-09 um 01 08 55

Symfony:

Use the JS escaper as expected:

Bildschirmfoto 2022-11-09 um 01 08 44

[Feature Request] Providing twig itself as a service

Twig itself or the Twig Environment would be nice to have as a service itself. So other libraries and systems build on top of Twig can easily be used inside Spiral without have to get Twig in hacky way or have two seperate instances of twig

Currently in schranz-templating/templating#40 (comment)

I'm using a very hacky solution to get Twig from this package:

        $container->bindSingleton('twig', function (Container $container) {
            // Use a hack to get Twig from the ViewManager as it is not available as an service yet
            $viewManager = $container->get(ViewManager::class);

            $engines = $viewManager->getEngines();

            $twigEngine = null;
            foreach ($engines as $engine) {
                if ($engine instanceof TwigEngine) {
                    $twigEngine = $engine;
                    break;
                }
            }

            if (null === $twigEngine) {
                throw new \LogicException(
                    \sprintf('Expected "%s" to be registered in the view manager.', TwigEngine::class)
                );
            }

            return $twigEngine->getEnvironment(new ViewContext());
        });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.