I needed a way for the the block template to decide whether the title needs an icon or not before it. Here is a hacky way for the template in Drupal 8 to add icon to block title.
Since the block labels were short and this was in a very specific block template that was only used for a certain type of blocks I was able to do the following.
In the template file block-MODULE-NAME.html.twig I added the following.
{%
set classes = [
'block',
'block-' ~ configuration.provider|clean_class,
'block-' ~ plugin_id|clean_class,
'clearfix',
]
%}
{%
set icon = [
label == 'Subscribed' ? '<i class="fa fa-tags" aria-hidden="true"></i>' :
label == 'Viewed' ? '<i class="fa fa-id-card-o" aria-hidden="true"></i>' :
label == 'Requested' ? '<i class="fa fa-reply" aria-hidden="true"></i>' :
label == 'Active' ? '<i class="fa fa-hourglass-half" aria-hidden="true"></i>' :
label == 'Resolved' ? '<i class="fa fa-check" aria-hidden="true"></i>'
]
%}
<section{{ attributes.addClass(classes) }}>
{{ title_prefix }}
{% if label %}
<h2{{ title_attributes.addClass('block-title') }}>{{ icon.0|raw }} {{ label }}</h2>
{% endif %}
{{ title_suffix }}
{% block content %}
{{ content }}
{% endblock %}
</section>
I created an icon array, where the first item in it has a huge Twig expression. Basically a concatenated If-else series. that tests for the label and sets the necessary Font Awesome markup.
{{ icon.0|raw }}
prints the icon as is.
I would love to hear you comments. Click the heart icon if this helped, Cheers!