0

I have the following code:

<div class="other_left_array" >
      {% if followeds.followeds_posts_counts[key] > 0 %}
        <a id="2"  href="javascript:void(0);" class="left_array_trigger">
            la
        </a>
      {% else %}
        <a id="2" style="display: none" href="javascript:void(0);" class="left_array_trigger">
            la
        </a>
      {% endif %}
   </div>

I feel there is another more optimal way to write such a code, especially that the only difference between the two statements is : style="display: none". Your help is highly appreciated.

Victor Bocharsky
  • 11,930
  • 13
  • 58
  • 91
Adib Aroui
  • 4,981
  • 5
  • 42
  • 94

2 Answers2

1

Can you try this,

<div class="other_left_array" >          
 <a id="2" {%if followeds.followeds_posts_counts[key]>0 %} style="display: none;"  {%endif%}     href="javascript:void(0);" class="left_array_trigger">
       la
    </a>             
 </div>
Krish R
  • 22,583
  • 7
  • 50
  • 59
1

Just put the if statement in that place:

<div class="other_left_array" >
    <a id="2" {% if followeds.followeds_posts_counts[key] > 0 %}style="display: none"{% endif %}  href="javascript:void(0);" class="left_array_trigger">
        la
    </a>
</div>

To make it nicer, you can use the ternary operator:

<div class="other_left_array" >
    <a id="2" {{ followeds.followeds_posts_counts[key] > 0 ? 'style="display: none"' : '' }}  href="javascript:void(0);" class="left_array_trigger">
        la
    </a>
</div>

Or you can use the {% spaceless %} tag:

<div class="other_left_array" >
    {% spaceless %}
    <a
        id="2"
        {% if followeds.followeds_posts_counts[key] > 0 %}
            style="display: none"
        {% endif %}
        href="javascript:void(0);"
        class="left_array_trigger"
    >
    {% endspaceless %}
        la
    </a>
</div>
Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • Thank you Wouter, the second solution using ternary operator is, IMHO, the best one. – Adib Aroui Feb 14 '14 at 14:49
  • 1
    @whitelettersandblankspaces how is the ternary the best? You are outputting a string or an empty string at all times. Using the first solution you only attempt to output something only if needed. – kittycat Feb 14 '14 at 14:59
  • yeaaaah, you are right @crypticツ. I did a wrong calculation. Thank you for correcting me. – Adib Aroui Feb 14 '14 at 15:01