簡體   English   中英

是否可以在樹枝模板中添加php數字計數器變量?

[英]Is it possible to add a php number counter variable to a twig template?

我在Drupal 8中為段落模塊設置了自定義模板。因此,用戶可以根據需要將手風琴添加到正文中。 我有一個可訪問的代碼段,我已經編寫並在需要手風琴時使用。 除非我正常使用它,否則它工作正常,它位於自定義編碼頁面上,因此我可以更改每個ID值。 由於這將是一個段落模塊,用戶可以多次添加到頁面上,因此我需要以某種方式將ID號動態地應用於代碼段-每次用戶添加另一個手風琴時,都將ID號添加到該編號中。

所以下面是我使用的代碼:

<div class="accord">
   <h2 class="toggleAccord">
      <button aria-controls="accord-1" aria-expanded="false">{{content.field_accordion_title}}</button>
   </h2>
   <div class="accordion-text" aria-hidden="true" id="accord-1">
      {{content.field_accordion_body}}
   </div>
</div>

<script>
$(document).ready(function() {

  openAccordion();

  $(".toggleAccord button").click(function() {

    var btn = $(this);
    var toggle = btn.parent();
    var content = btn.parent().next();

    if (btn.attr('aria-expanded') === 'false') {
      // open
      $(content).slideDown();
      toggle.addClass('closeAccord');
      btn.attr('aria-expanded', 'true');
      content.attr('aria-hidden', 'false');

    } else {
      // close
      $(content).slideUp();
      toggle.removeClass('closeAccord');
      btn.attr('aria-expanded', 'false');
      content.attr('aria-hidden', 'true');
    }
  });
});
</script>

因此,每次添加手風琴時,我都需要使用“ accord-1”來動態計數。 雅閣2,雅閣3等。

有沒有一種方法可以用php或jQuery變量替換數字,以便它自動計數?

是的,您可以先計算DOM元素。 然后,在將該值計數並存儲在變量中之后,我將使用ajax或xhr請求將此信息發送到我的drupal端的php變量。 您是否認為需要代碼建議?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM