Can Twig output vary based on value of taxonomy

Support MB Views Can Twig output vary based on value of taxonomyResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #40147
    swestswest
    Participant

    In pseudo code
    Based on the value of the honor_type taxonomy, I would like different output:
    if Honor_Type = "Faculty"
    output <h4 class="class-of">Years of Service {{ post.vod_class_of_tax.name }}</h4>
    else
    output <h4 class="class-of">Years of Service {{ post.vod_class_of_tax.name }}</h4>
    end if

    
    Is this possible with Twig? If so, how?
    
    Here's the Twig code I have in the View:
    
    	<div class="card-heading">
    		<h4 class="honor-type">
    		{% for item in post.vod_honor_type_tax %} 
    			{{ item.name }}
    		{% endfor %}</h4>
    		<h1 class="honoree-name">{{ post.vod_honoree_names }}</h1>		
    		<h4 class="class-of">class of {{ post.vod_class_of_tax.name }}</h4>
         </div>
    

    It's the last line containing the h4 that I need to vary based on the value of vod_honor_type.

    Thanks for any help you can give.

    #40155
    PeterPeter
    Moderator

    Hello there,

    It's not a possibility of Twig, it's how you create your conditional code to output the value. I add the code below for example.

    {% for item in post.vod_honor_type_tax %} 
    	{% if item.name == 'somthing' %}
    		<h4 class="honor-type">{{ item.name }}</h4>
    		<h1 class="honoree-name">{{ post.vod_honoree_names }}</h1>		
    		<h4 class="class-of">class of {{ post.vod_class_of_tax.name }}</h4>
    	{% else %}
    		something if false
    	{% endif %}
    {% endfor %}

    To understand how to use Twig, please read more on the documentation https://docs.metabox.io/extensions/mb-views/#twig

    #40161
    swestswest
    Participant

    Thanks Peter.

    This code goes into a view for a Single item, so I don't think I can use a for loop as I'm seeing multiple items returned.

    <div class="vod-wrapper">
    	<div class="card-heading">
    	    <h4 class="honor-type">{{ post.vod_honor_type_tax.name }}</h4>
    		<h1 class="honoree-name">{{ post.vod_honoree_names }}</h1> 
    		{% for item in post.vod_honor_type_tax %} 
    			{% if item.name == 'Faculty' %}
                    <h4 class="years-of-service">Years of Service {{ post.years_of_service }}</h4>
            {% else %}
                    <h4 class="class-of">Class of {{ post.vod_class_of_tax.name }}</h4>
            {% endif %}
    		{% endfor %}
    
    </div>

    With the View code above, I'm seeing this for output
    https://share.cleanshot.com/W9vpCDL2
    and
    https://share.cleanshot.com/bhN2v6Xj

    Instead of this:
    https://share.cleanshot.com/pKqwJ0gQ
    and
    https://share.cleanshot.com/4vVBzhZ1

    #40170
    PeterPeter
    Moderator

    Hello,

    If this code output the honor type term name correctly

    <h4 class="honor-type">{{ post.vod_honor_type_tax.name }}</h4>

    then I think you don't need to use the loop to check the term name.

    #40172
    swestswest
    Participant

    Thank you Peter.
    This is what I finally got to work. I appreciate your help in finding a solution.

    The key I was looking for was:
    {% if post.vod_honor_type_tax.name == 'Faculty' %}

    	<div class="card-heading">
    	    <h4 class="honor-type">{{ post.vod_honor_type_tax.name }}</h4>
    		<h1 class="honoree-name">{{ post.vod_honoree_names }}</h1> 
    			{% if post.vod_honor_type_tax.name == 'Faculty' %}
    			   <h4 class="years-of-service">Years of Service {{ post.years_of_service }}</h4>
    			{% else %}
    			    <h4 class="class-of">class of {{ post.vod_class_of_tax.name }}</h4>
    			{% endif %}
    	</div>
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.