Change js variable via php

Hey :slight_smile:

I want to load the needed js files only when a page really is loaded.
Because i am using barba js it is not that easy with refreshing the scripts.

For that reason @pixelijn helped me to get the blocks from the used page.
Now i thought depending on the Block i call a javascript script wich switches a variable.

   <script>
         var slider;
    </script>
   <?php if ($doesBlockXExist->isNotEmpty()) : ?>
    True
    <script>
         slider = true;
    </script>
<?php else : ?>
    False
    <script>
         slider = false;
    </script>
<?php endif ?>
<script>
console.log(slider);
</script>

True and false are echoed correctly on the website, but the console.log always logs the first state.

Any idea?

Why are these variables in different script tags now? That doesn’t make sense. You can do it like this:

<script>
     let slider = <?= $doesBlockExist->isNotEmpty() ? true : false ?>;
     console.log(slider);
</script>

Yeah way better! But still: the slider variable doesnt change…

Looks like you have to wrap it in quotes

let slider = <?= $doesBlockXExist->isNotEmpty() ? 'true' : 'false' ?>;