0

In Silex2 I am able to use Twig templates but I want to use the PHP engine of Twig, instead of the Twig syntax. For example this guide describes how to do it for Symfony but not Silex 2.

My Silex index.php looks like:

$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/views',
));

$app->get('/', function() use ($app) {
    return $app['twig']->render('index.html.php', array(
        'name' => 'Bob',
    ));
});

My index.html.php looks like:

<p>Welcome to the index <?php echo $view->name; ?></p>

When I run the app in the browser and view the source, I see the literal string <?php echo $view->name; ?> which hasn't been executed.

I suspect there may be a Twig config setting to tell it I want to use the PHP style templates. To clarify, if I use the Twig syntax instead, e.g.:

<p>Welcome to the index {{ name }} </p>

Then it works and I see the name Bob, therefore I know this is not a web server or PHP config problem.

This code didnt work for me:

$app['template'] = function($app) use($settings) {
    $loader = new \Symfony\Component\Templating\Loader\FilesystemLoader([
        BASE_DIR . $settings->templates_path
    ]);
    $nameParser = new \Symfony\Component\Templating\TemplateNameParser();
    $templating = new \Symfony\Component\Templating\PhpEngine($nameParser, $loader);
    return $templating;
};

though, this is didnt work too:

$app->register(new NS\ViewServiceProvider(), [
    'view.path' => BASE_DIR . $settings->templates_path,
]);

ViewServiceProvider:

class ViewServiceProvider implements ServiceProviderInterface
{
    public function register(Container $app)
    {
        $app['view'] = function($app) {
            $engine  = $app['view.engine'];
            $helpers = isset($app['view.helpers']) ? $app['view.helpers'] : array();
            $helpers = array_merge($helpers, $app['view.default_helpers']);
            $engine->setHelpers($helpers);
            return $engine;
        };
        $app['view.engine'] = function($app) {
            return new PhpEngine($app['view.parser'], $app['view.loader']);
        };
        $app['view.loader'] = function($app) {
            $paths = isset($app['view.path']) ? $app['view.path'] : array();
            return new FileSystemLoader($paths);
        };
        $app['view.parser'] = function() {
            return new TemplateNameParser;
        };
        $app['view.default_helpers'] = function() {
            return array(
                new \Symfony\Component\Templating\Helper\SlotsHelper
            );
        };
    }
}

Both throws an error, that cannot find the view.php file

7
  • 1
    Here is how twig is registered in Silex : silex.sensiolabs.org/doc/2.0/providers/twig.html you can deregister it if you don't want to use it. Actually I'm curious to know why you prefer to write <?php echo $view->name; ?> instead of {{ name }} ? it's so much easier ! Commented Feb 13, 2017 at 21:56
  • 1
    As you can see in Silex included providers, the Templating component is not built-in. You'll need to create your own ServiceProvider. Commented Feb 13, 2017 at 22:53
  • @mickdev If you don't want to use Twig you don't need to "deregister" it, you just won't register it. It's not enabled by default. Also, as pointed out, Twig does not have a "PHP engine", Twig is a templating engine, if you don't want to use it and prefer to use the PHP Templating Engine Component, you need to create a service provider for it and register it (like you can do with the out of the box Twig provider). PS: I would not follow that route... Commented Feb 14, 2017 at 18:10
  • @mTorres I think we are talking about the same thing, the way we are saying it is just different. Anyway, Alain Tiembo has already 'translate' what I was thinking in the first place. Commented Feb 14, 2017 at 18:31
  • This answer might help. Commented Feb 15, 2017 at 6:26

0

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.