How do I pass $variables['is_front'] from template_preprocess_page() to a jquery script in Drupal8?
1 Answer
send it:
function mytheme_preprocess_page(&$variables) {
$variables['#attached'] = [
'drupalSettings' => [
'myLibrary' => [
'is_front' => $variables['is_front'],
],
],
];
}
access it in jquery:
Drupal.behaviors.myBehaviour = {
attach: function (context, settings) {
console.log(settings.myLibrary.is_front);
}
};