3

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.

5
  • A blank page suggest you are running in production mode while the error log indicates you are in development mode. Double check that you are in development mode. Do you get a profile bar without the extension? It looks like your config is correct, I think you are running in production mode and that you have something else wrong. Commented Dec 6, 2014 at 14:32
  • Nah i'm in devMode. As soon as i remove the code in the service everything is ok (including the debug-bar). Commented Dec 6, 2014 at 14:34
  • Ok but in development mode I would expect errors to be shown right in the browser. Commented Dec 6, 2014 at 15:01
  • 1
    Yes i see errors in the most cases - maybe just an apache-misconfig or the like. But Twig-Extension should work, but i get the same message even when i copy/paste the given example... Commented Dec 6, 2014 at 18:39
  • 1
    If it was me, I would resolve the errors. Maybe update your question with a few of them. Commented Dec 6, 2014 at 18:48

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.