diff --git a/lib/SimpleSAML/Locale/Translate.php b/lib/SimpleSAML/Locale/Translate.php
index e4c84cb05538431cc9026ead1a7656c80bb511fb..768e5302983f13d405a8d0efdf02c916b85948f3 100644
--- a/lib/SimpleSAML/Locale/Translate.php
+++ b/lib/SimpleSAML/Locale/Translate.php
@@ -468,7 +468,7 @@ class Translate
     {
         $text = BaseTranslator::$current->gettext($original);
 
-        if (func_num_args() === 1) {
+        if (func_num_args() === 1 || $original === null) {
             return $text;
         }
 
diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index 4dd75b68fbc4cea674b88a2704f96e25fe428a08..e584d825de589b62b9f13c79d09cc021b8397fa2 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -306,8 +306,9 @@ class Template extends Response
 
         // set up translation
         $options = [
-            'cache' => $cache,
             'auto_reload' => $auto_reload,
+            'cache' => $cache,
+            'strict_variables' => true,
             'translation_function' => [Translate::class, 'translateSingularGettext'],
             'translation_function_plural' => [Translate::class, 'translatePluralGettext'],
         ];
@@ -513,7 +514,11 @@ class Template extends Response
         if ($this->controller) {
             $this->controller->display($this->data);
         }
-        return $this->twig->render($this->twig_template, $this->data);
+        try {
+            return $this->twig->render($this->twig_template, $this->data);
+        } catch (\Twig\Error\RuntimeError $e) {
+            throw new \SimpleSAML\Error\Exception(substr($e->getMessage(), 0, -1) . ' in ' . $this->template, 0, $e);
+        }
     }
 
 
diff --git a/modules/core/templates/base.twig b/modules/core/templates/base.twig
index 96fcc07baeb873b82c4f94a4e6a17a002a40b500..83b2b9a14985e56ffedc9f4b630b7f762d0f4faa 100644
--- a/modules/core/templates/base.twig
+++ b/modules/core/templates/base.twig
@@ -1,8 +1,11 @@
 {% extends "base.twig" %}
 {% block contentwrapper %}
-{% if tabname %}
+
 <div id="portalmenu" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
+
     <ul class="tabset_tabs ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
+
+    {% if links is defined %}
     {% for name, link in links %}
     {% if name == pageid %}
         <li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active">
@@ -14,12 +17,13 @@
         </li>
     {% endif %}
     {% endfor %}
+    {% endif %}
+
     </ul>
     <div id="portalcontent" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
-{% endif %}
+
 {{ block('content') }}
-{% if tabname %}
+
     </div>
 </div>
-{% endif %}
 {% endblock %}
diff --git a/modules/core/templates/show_metadata.twig b/modules/core/templates/show_metadata.twig
index f39aee9fe3e69cd70e0263e1f42b9d5f92530c3a..b747563706e2325f11a72cf6d9c0029366561c23 100644
--- a/modules/core/templates/show_metadata.twig
+++ b/modules/core/templates/show_metadata.twig
@@ -4,7 +4,7 @@
     <div class="code-box">
         <div class="code-box-title">
             <h3>{{ 'Metadata'|trans }}</h3>
-            <button data-clipboard-target="#metadata" id="btn{{ loop.index }}" class="pure-button right clipboard-btn copy">
+            <button data-clipboard-target="#metadata" class="pure-button right clipboard-btn copy">
                 <span class="fa fa-copy"></span>
             </button>
         </div>
diff --git a/templates/auth_status.twig b/templates/auth_status.twig
index 9965dd37601b028f1294b9b560403c133ef1020a..8a80f413d388b0f6618b9d948758c4274f9ccf13 100644
--- a/templates/auth_status.twig
+++ b/templates/auth_status.twig
@@ -102,12 +102,12 @@
     </dl>
     <br>
 
-{% if logout %}
+{% if logout is defined %}
     <h2>{% trans %}Logout{% endtrans %}</h2>
     <p> {{ logout }}</p>
 {% endif %}
 
-{% if logouturl %}
+{% if logouturl is defined %}
     <div class="center">
         <a class="pure-button pure-button-red" href="{{ logouturl }}">{{ 'Logout'|trans }}</a>
     </div>
diff --git a/templates/includes/expander.twig b/templates/includes/expander.twig
index 0817520d3fca72894a5c740dba9c0108c57ca058..37deceb6bfbbd4711dc8d03b13029646ac855cf0 100644
--- a/templates/includes/expander.twig
+++ b/templates/includes/expander.twig
@@ -1,4 +1,4 @@
-        <div class="expandable{% if expanded %} expanded{% endif %}">
+        <div class="expandable{% if expanded is defined %} expanded{% endif %}">
           {% if block('general') is defined %}
           <div class="general">
             {{- block("general") }}
diff --git a/templates/metadata.twig b/templates/metadata.twig
index 4d3903533689fcb890d6d5cbd51077de9e285ba7..1857b4cbbffca850b2e6023662d4238a72f491d4 100644
--- a/templates/metadata.twig
+++ b/templates/metadata.twig
@@ -3,7 +3,7 @@
 {% block content %}
   <h2>{% trans %}Metadata{% endtrans %}</h2>
   <dl>
-    <dd>{{ metadata_intro }}</dd>
+    <dd>{{ '{admin:metadata_intro}'|trans }}</dd>
 
   {% if metaurl is defined %}
     <dd>{% trans %}You can get the metadata xml on a dedicated URL:{% endtrans %}</dd>
@@ -49,9 +49,9 @@
 
         <li>
           <a href="{{ cert.url }}"><i class="fa fa-download"></i>{{ cert.name }}
-          {#- #}{% if cert.signing %}-signing{% endif %}
-          {#- #}{% if cert.encryption %}-encryption{% endif %}.pem
-          {#- #}{% if cert.prefix %} ({% trans %}new{% endtrans %}){% endif %}</a> {{ cert.comment }}
+          {#- #}{% if cert.signing is defined %}-signing{% endif %}
+          {#- #}{% if cert.encryption is defined %}-encryption{% endif %}.pem
+          {#- #}{% if cert.prefix is defined %} ({% trans %}new{% endtrans %}){% endif %}</a> {{ cert.comment }}
         </li>
       {% endfor %}
 
@@ -59,4 +59,4 @@
   {% endif %}
 
   </dl>
-{% endblock content %}
\ No newline at end of file
+{% endblock content %}
diff --git a/templates/selectidp-links.twig b/templates/selectidp-links.twig
index a7bf1103fdfe0b210988767bc99f69736edf4c26..cebe6962a1418573e241c02336afbbe6c32ef8bf 100644
--- a/templates/selectidp-links.twig
+++ b/templates/selectidp-links.twig
@@ -17,11 +17,11 @@
         {% for idpentry in idplist %}
         {% if idpentry.entityid == preferredidp %}
                 <div class="preferredidp">
-                {% if idpentry.iconurl %}
+                {% if idpentry.iconurl is defined %}
                     <img class="float-l" src="{{ idpentry.iconurl }}">
                 {% endif %}
                 <h3><i class="fa fa-star"></i> {{ idpentry.name }}</h3>
-                {% if idpentry.description %}
+                {% if idpentry.description is defined %}
                     <p>{{ idpentry.description }}</p>
                 {% endif %}
                 <button type="submit" class="btn" name="idp_{{ idpentry.entityid }}">{{'Select'|trans}}</button>
@@ -31,11 +31,11 @@
 
         {% for idpentry in idplist %}
         {% if idpentry.entityid != preferredidp %}
-                {% if idpentry.iconurl %}
+                {% if idpentry.iconurl is defined %}
                     <img class="float-l" src="{{ idpentry.iconurl }}">
                 {% endif %}
                 <h3>{{ idpentry.name }}</h3>
-                {% if idpentry.description %}
+                {% if idpentry.description is defined %}
                     <p>{{ idpentry.description }}</p>
                 {% endif %}
                 <button type="submit" class="btn" name="idp_{{ idpentry.entityid }}">{{'Select'|trans}}</button>