Programatically render block with contextual links (in php)
You can use the Twig Tweak module to insert the 'local tasks' menu in your template file: 

{{ drupal_entity('block', 'THEME_local_tasks', check_access=true) }}