How to add pagination to programmatically rendered search result template I am trying to implement the pagination in drupal 8 custom page.

I have a custom array which I am rendering using theme, I need to add pagination for it.

    $internal_result = array_merge($internal_result, $basic_data);
    $response = new AjaxResponse();
    $terms['internal'] = $internal_result;
    $renderable = array(
        '#theme' =&gt; 'search_result_template',
        '#data' =&gt; $terms
    );
    $rendered = \Drupal::service('renderer')-&gt;render($renderable);


I need to add pagination for above result. Can anyone help me?
I tried with the search_result template and it worked, hope this helps you :


$output['prefix']['#markup'] = '&lt;h2&gt;Test page&lt;/h2&gt;';
//This sets up necessary global variables for the type =&gt; #pager
$page = pager_default_initialize(5, 2);
$render = [];
  $output[] = [
    '#theme' =&gt; 'search_result',
    '#result' =&gt; 's',
  ];
$pager = [
  '#type' =&gt; 'pager',
];
$output['suffix']['#markup'] = '&lt;/ol&gt;' . \Drupal::service('renderer')-&gt;render($pager);
return $output;



We just need to set up the global variables which help to process rendered array to output pagers items. And add a pager to the suffix.