D
D
dmitriy2016-12-01 01:04:26
PHP
dmitriy, 2016-12-01 01:04:26

How to catch an Exception thrown in a twig custom function?

//botstrup.php
$twig->addFunction(new Twig_SimpleFunction('generateSchoolList', function($class){
    if(!class_exists($class))
  throw new \NotFoundClassException($class);

    echo (new $class)->sendOutput();
}));

//controller
class Controller{

    function index(){
  try{
      return $this->twig->render("home");
  } catch(\Exception $e){
      return $e->getMessage();
  }
    }
}
//home.twig
<td>{{generateModule('Project\\UserList') }}</td>

When NotFoundClassException is thrown, the processing and error output occurs inside twig, in the place where {{generateModule('Project\\UserList') }} is used, how can it be passed to the controller level?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
1alexandr, 2016-12-01
@dmitriylanets

try {
    $content = $this->twig->render('tmp');
} catch(\Exception $e) {
    return $e->getPrevious()->getMessage();
}
return $content;

twig throws Twig_Error_Runtime in previous your exception

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question