How can I add extra info like lang="ar" dir="rtl" to the <html> tag like the following?

<html{{ html_attributes }} lang="ar" dir="rtl">

1 Answer 1


You can use the following code, even though for those specific values, I would find a different way (e.g. setting the language for the shown page).

{% html_attributes.setAttribute("lang", "ar") %}
{% html_attributes.setAttribute("dir", "rtl") %}
<html{{ html_attributes }}>

Inside hook_preprocess_html(), since $variables['html_attributes'] is an Attribute object, so you can use Attribute::setAttribute().

$variables['html_attributes']->setAttribute("lang", "ar");
$variables['html_attributes']->setAttribute("dir", "rtl");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.