Store a return function
I needed to store a function that would otherwise echo for use in a Twig template:
function doPagination($cpt_query){
wp_pagenavi( array( 'query' => $cpt_query ) );
}
ob_start();
doPagination($cpt_query);
$context['pagination'] = ob_get_contents();
ob_end_clean();