简体   繁体   English

显示选定元素树枝模板

[英]Display selected elements twig template

data= [{id:1, text:'abc'},{id:2, text:'cde'}]

In my template I want to do this:在我的模板中,我想这样做:

<ul>
  {{% for article in data %}}
  <li><a href={{article.id}}>{{article.id}}</a></li>
</ul>

In click on li I would display only the property "text" of related id.在单击 li 时,我将仅显示相关 id 的属性“文本”。

<div>{{selected article.text}}</div>

If clicked 1 I want to see "abc".如果点击 1 我想看到“abc”。

Is there a way to do it with twig?有没有办法用树枝做到这一点?

If I understand correctly, you cant to show dynamically the text related to an id on click on the li containing the related id.如果我理解正确,您无法在单击包含相关 id 的 li 时动态显示与 id 相关的文本。

This cannot be achieved with only twig.这不能仅用树枝来实现。 You need some javascript here.你需要一些 javascript 在这里。 Basically what you can do is the following:基本上你可以做的是以下几点:

<ul>
  {% for article in data %}
    <li>
      <a href="#" data-content="{{ article.text|e('html_attr') }}" onclick="document.getElementById('dynamic-content').innerHTML = this.dataset.content; return false">
        {{ article.id }}
      </a>
    </li>
  {% endfor %}
</ul>
<div id="dynamic-content"></div>
  • |e('html') is some escaping for keeping it inside an html attribute, learn more here |e('html')是将其保留在 html 属性中的一些转义,在此处了解更多信息
  • onclick="" this contains pure javascript, it work like this but I recommand to do it another way (checkout this JSFiddle ) onclick=""这包含纯 javascript,它像这样工作,但我建议用另一种方式来做(检查这个 JSFiddle

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

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