简体   繁体   English

如何使用Twig获取html类名称?

[英]How can I get html class name using Twig?

I want to get the name of a class using tip and display a certain text based on what the class is. 我想使用提示获取类的名称,并根据类的内容显示某些文本。 Is there a way to get a class name and use it in an IF condition? 有没有一种方法可以获取类名并在IF条件下使用它?

<div class="zboruri_rute {{ route_type ? "departure_flight" : 
"arrival_flight" }}">

{% if class == "zboruri_rute departure_flight" %}

<p>Departure</p>

{% elseif if class == "zboruri_rute arrival_flight" %}

<p>Arrival</p>

{% endif %}

I want to display Departure if the class is zboruri_intoarcere and display arrival if the class is zboruri_plecare 如果班级是zboruri_intoarcere,我想显示出发;如果班级是zboruri_plecare,我想显示到达

Try it 试试吧

    {% set route_type = "departure_flight" %}
    <div class="zboruri_rute {{ route_type ? "departure_flight" :
    "arrival_flight" }}">

        {% if route_type == "departure_flight" %}

            <p>Departure</p>

        {% elseif  route_type == "arrival_flight" %}

            <p>Arrival</p>

        {% endif %}

or with jquery 或与jQuery

<p id="result"></p>
    <script>
         zboruri_rute = $(".zboruri_rute").attr('class');
         if (zboruri_rute.indexOf("departure_flight") >= 0)
         {$("#result").text("Departure")}
         if (zboruri_rute.indexOf("arrival_flight") >= 0)
         {$("#result").text("Arrival")}
    </script>

As said in the comments, by namth, u'd only need to test route_type again, eg 就像评论中所说的那样,您只需要再次测试route_type ,例如

<div class="zboruri_rute {{ route_type ? "departure_flight" : "arrival_flight" }}">
{% if route_type %}
    <p>Departure</p>
{% else %}
    <p>Arrival</p>
{% endif %}
</div>

demo 演示


Shorter version 短版

<div class="zboruri_rute {{ route_type ? "departure_flight" : "arrival_flight" }}">
    <p>{{ route_type ? 'Departure': 'Arrival' }}</p>
</div>

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

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