0
votes

I am currently using v1.12.2 Twig as a standalone templating engine.

I wrote a Twig extension called Utility_Twig_Extension in a file called UtilityExtension.php

and an index.php

//index.php
require_once '../vendor/twig/twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem(OEBPS);
$twig = new Twig_Environment($loader, array(
    'cache' => APP . DS . 'cache',
));

require_once '../vendor/twig/twig/ext/utility/UtilityExtension.php';

$twig->addExtension(new Utility_Twig_Extension());

Here is the UtilityExtension.php //UtilityExtension.php namespace UtilityTwigExtension;

class Utility_Twig_Extension extends Twig_Extension
{
    public function getName()
    {
        return "utility";
    }

}

Here is my directory structure:

src
 |__app
 |    |__ index.php
 |__vendor
      |__twig
           |__twig
               |__ext
               |__lib

I cannot even load the file properly.

I have traced the issue to the fact that the extension class tries to extend Twig_Extension.php.

So I require_once the Twig_Extension which is the Extension.php file in UtilityExtension.php. However, still not working.

Most documentation talks about adding a custom Twig Extension in the context of Symfony.

I am using Twig standalone, so I have yet to find any documentation on that.

Please advise.

UPDATE1:

By not working, I meant that I get the 500 server error. I ran error_reporting(E_ALL) was to no avail.

The error was relieved the moment I removed the words extends Twig_Extension from the extension class.

UPDATE2: I realized it was a namespace issue. because I removed the namespace UtilityTwigExtension; from the UtilityExtension.php and the server 500 error was gone.

So I put the namespace UtilityTwigExtension; back and then call

require_once '../vendor/twig/twig/ext/utility/UtilityExtension.php';

$twig->addExtension(new UtilityTwigExtension\Utility_Twig_Extension());

the error came back.

Question: How do I call the TwigExtension if I insist on using the namespace? Is there a better way of using namespace?

UPDATE3:

I still get server 500 after trying Luceos answer.

error_reporting(E_ALL);
require_once 'constants.php';

require_once 'ZipLib.php';

require_once '../vendor/twig/twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem(OEBPS);

$twig = new Twig_Environment($loader, array(
    'cache' => APP . DS . 'cache',
));

require_once '../vendor/twig/twig/ext/utility/UtilityExtension.php';

use UtilityTwigExtension\Utility_Twig_Extension;

$twig->addExtension(new Utility_Twig_Extension());

the UtilityExtension.php

namespace UtilityTwigExtension;

class Utility_Twig_Extension extends Twig_Extension
{
    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName() {
        return 'utility';
    }

}
1
This page on the Twig website shows you how to do it and doesn't mention symfony. First Google result for "Twig add extension"Colin M
I followed the steps there. unless I overlooked something?Kim Stacks
Yes but you realize your extension doesn't actually do anything, right? How do you know it's not being loaded? "It's not working" is not helpful.Colin M
namespace UtilityTwigExtension? Did you create the UtilityTwigExtension in its own namespace? You would then need to add the extension: $twig->addExtension(new UtilityTwigExtension\Utility_Twig_Extension());Luceos
@ColinMorelli I apologize. I meant that I had a server 500 error. I have made the corrections.Kim Stacks

1 Answers

1
votes

So let's put the comments in an answer and move on from there without crowding the comments:

First of call the extension from the correct namespace:

use UtilityTwigExtension\Utility_Twig_Extension;
$twig->addExtension(new Utility_Twig_Extension());

Use and namespaces calls are normally placed at the top of the file.

You can also try calling the namespace + object directly by using:

$twig->addExtension(new UtilityTwigExtension\Utility_Twig_Extension());

Update 3

The Utility_Twig_Extension extends Twig_Extension from the namespace UtilityTwigExtension, which does not exist. I assume Twig_Extension is not in any namespace so you'll use \Twig_Extension:

namespace UtilityTwigExtension;

class Utility_Twig_Extension extends \Twig_Extension
{
    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName() {
        return 'utility';
    }

}