简体   繁体   English

如何使用Twig获取JavaScript结果

[英]How to fetch javascript results w/ Twig

I am not sure how I can pass a variable from my JS folder to my Twig templates. 我不确定如何将变量从JS文件夹传递到Twig模板。 It is a variable that takes true/false on page load and needs to be used inside an If statement to execute responsive code. 它是一个在页面加载时为true / false的变量,需要在If语句内使用它来执行响应代码。

I tried calling the name of the variable itself but as I found out that doesn't work. 我尝试调用变量本身的名称,但发现不起作用。

JS Script JS脚本

var isMobile = (function() {....})();

Twig template 树枝模板

 {% if isMobile %}
  <div>Small div</div>
  {% else %}
  <div>Big div</div>
 {% endif %}

It will be better and more convenient doing with Javascript itself. 使用JavaScript本身会更好,更方便。

<div id="yourDIV">
  This is my DIV element.
</div

In Javascript do something like this. 用Javascript做这样的事情。

 var el = document.querySelector('#yourDIV');//to target multiple divs use class
 if (isMobile()) {
   el.style.display = "block";
  } else {
    el.style.display = "none";
 }

This is an idea, you can modify it as per your need. 这是一个主意,您可以根据需要进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM