3

How do I pass $variables['is_front'] from template_preprocess_page() to a jquery script in Drupal8?

0

1 Answer 1

4

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);

    }
  };

Not the answer you're looking for? Browse other questions tagged or ask your own question.