Move all common twig code into lib.php