In PHP I have:
protected function registerTwigFunctions()
{
return [
'count_VKcomments' => 'countVKCommentsFunction'
];
}
public function countVKCommentsFunction($context, $pdo) {
$url = $context['url'];
$sql = "SELECT id, COUNT(*) FROM level_1 WHERE url = ?";
$count = $pdo->prepare($sql)->execute([$url])->fetchColumn();
return $count;
}
In Twig I have:
{% set url = global.request.uri %}
{{ count_VKcomments({ 'url': url }) }
But it only works if I remove the $pdo
argument from countVKCommentsFunction
, otherwise some temporary file complains about too few arguments being passed from Twig. But I need to include that $pdo
argument just to avoid duplicating connection to the database, which is already defined as $pdo
in another function. At the same time, I don't understand how to properly call countVKCommentsFunction
provided that it needs $pdo
as an argument.