Create a link pointing to # I am trying to make a link pointing to # but am unable to find a method that works.

The output i would like to see is

&lt;a href="#"&gt;Example&lt;/a&gt;


I have tried a few variations of the following without success.

$url = Url::fromUserInput('#');
$link_item = Link::fromTextAndUrl(t('Example'), $url);
return $link_item-&gt;toRenderable();


I have also tried using  ::fromUri etc and am unable to find a method that produces tehd esired output, please help.
It's probably not possible to attach an empty fragment. See this code in  UrlGenerator::generateFromRoute():

if (isset($options['fragment'])) {
  if (($fragment = trim($options['fragment'])) != '') {
    $fragment = '#' . $fragment;
  }
}


If the fragment is empty it is ignored and no '#' shows up. This doesn't change if you provide the fragment in Url::fromUserInput(), because the fragment is transfered to the options and later processed by the same or similar code.

So you have to provide an anchor in the fragment:

$url = Url::fromUserInput('#anchor1');


or

$url = Url::fromRoute('&lt;current&gt;', [], ['fragment' =&gt; 'anchor2']);


As alternative option you can place a link with an empty fragment

&lt;a href="#"&gt;{{ examplevariable }}&lt;/a&gt;


either in a twig template or in an inline template in php.