What is the correct way to implement a custom block with twig template? I know there are tons of posts for this but I think I have a problem with understanding how a custom block with a custom template is meant to work in Drupal 8.
Problem
My aim:
To place a custom block to display social icons. The block needs some logic (generate a url for example).
My main problem:
Everything seems to work so far but I do not get the variables passed through the twig-template (url and urlencode). They always contain the default value from the module-file (null).
What I have so far:
Files under /modules/custom/wzm_social_icons:

wzm_social_icons.info.yml
wzm_social_icons.module
src/Plugin/Block/WzmSocialIconsBlock.php
templates/block--wzmsocialicons.html.twig

I installed the module and also placed a custom block with id &quot;socialicons&quot;. The block itself is just a placeholder with no special fields.
Files
wzm_social_icons.module
function wzm_social_icons_theme($existing, $type, $theme, $path) {
  return array('block__wzm_social_icons' =&gt;
    array(
      'variables' =&gt; array(
        'url' =&gt; null,
        'urlencoded' =&gt; null
      ),
      'template' =&gt; 'block--wzmsocialicons'
    )
  );
}

WzmSocialIconsBlock.php
class WzmSocialIconsBlock extends BlockBase {
  /**
   * {@inheritdoc}
   */
  public function build() {
    $oUrl = Url::fromRoute('&lt;current&gt;');
    $sUrl = $oUrl-&gt;toString();
    $sUrl = \Drupal::request()-&gt;getSchemeAndHttpHost() . $sUrl;
    $sUrlEncoded = urlencode($sUrl);

    return array(
      '#url' =&gt; $sUrl,
      '#urlencoded' =&gt; $sUrlEncoded
    );
  }
}

Nothing special in the twig-template - just the output for {{ url }} and {{ urlencode }}.
I think my main problem is to understand what needs to be declared with which key in the module-file and if the custom-block-id is used somewhere. No clue so far.
Thanks for any help!
Cheers
You're almost there. In your WzmSocialIconsBlock.php return the following:

return array(
  '#theme' =&gt; 'block_wzm_social_icons',
  '#url' =&gt; $sUrl,
  '#urlencoded' =&gt; $sUrlEncoded,
);


You might also want to simplify your hook_theme by letting it autodetect your template by name:

return array('block_wzm_social_icons' =&gt;
  array(
    'variables' =&gt; array(
      'url' =&gt; null,
      'urlencoded' =&gt; null
    ),
);


And name your template templates/block-wzm-social-icons.html.twig.