Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument 1 passed to Twig_Filter::__construct() must be an instance of string, string given

Tags:

php

twig

silex

I have a problem with TWIG. This code works at school but absolutely not with my laptop. I tried with a simple code but I have the error:

Catchable fatal error: Argument 1 passed to Twig_Filter::__construct() must be an instance of string, string given, called in /opt/lampp/htdocs/webalizer/projetSilex/vendor/twig/twig/lib/Twig/Extension/Core.php on line 139 and defined in /opt/lampp/htdocs/webalizer/projetSilex/vendor/twig/twig/lib/Twig/Filter.php on line 35

I use php 5.6 / SILEX 2.0 / Twig 2.0

Thanks for your help.

My code is very simple but doesn't work:

require_once __DIR__.'/vendor/autoload.php';

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

  $app->get('/', function(){
return "hello";
  });

 $app->get('/hello/{name}', function($name)use($app){

 return $app['twig']->render('hello.twig',
                            array("name"=>$name
                                ));

 });

 $app->run();>
like image 405
Ben Avatar asked Jan 26 '17 18:01

Ben


1 Answers

@CharlotteDunois pointed this out, but Twig 2.0 requires >=PHP7.0, so in your environment (php 5.6) you can't use Twig 2.0. From the Twig official documentation:

Prerequisites

Twig needs at least PHP 7.0.0 to run.

Notice that for PHP5.x branch you still have Twig 1.x aviable

like image 150
mTorres Avatar answered Oct 24 '22 09:10

mTorres