Ferret
Ferret

Reputation: 1440

Symfony2 creating twig extension

I want to create a simple twig extension ({{imgWidth(...)}}) that calls getimagesize() and returns the width and height of an image on the server.

I followed the instuctions you can find here.

When I reload my page I only can see a blank page - the error.log tells me that

PHP Fatal error: Class 'Fms\MediaBundle\Twig\Extension\ImgsizeExtension' not found in /var/www/fms/app/cache/dev/appDevDebugProjectContainer.php on line 4773

The service in MediaBundle\Resources\config\services.yml looks like:

services:
    twig.extension.imgsize:
        class: Fms\MediaBundle\Twig\Extension\ImgsizeExtension
        tags:
            - name: twig.extension

The class is:

<?
// src/Fms/MediaBundle/Twig/Extension/ImgsizeExtension.php
namespace Fms\MediaBundle\Twig\Extension;

class ImgsizeExtension extends \Twig_Extension
{
    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('imgsize', array($this, 'imgWidth'))
        );
    }

    public function imgWidth($mediaId = 0, $mediaSize = 'L')
    {
        // ...
        return $mediaId;
    }

    public function getName()
    {
        return 'imgsize';
    }
}

Clearing cache via console or manually didnt help too.

Upvotes: 3

Views: 457

Answers (1)

Grzegorz Krauze
Grzegorz Krauze

Reputation: 1148

Change <? to <?php. I copied your code and in with this modification symfony finally finds this class.

Upvotes: 2

Related Questions