0

Normally, my situation is easily solved using views rewrite feature. However, in this case I am unable to use views rewrite because I have data attribute tags in the field wrappers that are stripped out by views. So I need to handle this at the template level. I have no problem with the field data itself, but with the field wrappers. I need to use php if statements in either views-view-field.tpl.php or views-view-field.tpl.php to enclose the wrapper html too along with the field. But i can't figure out the php syntax to use to accomplish this. I need to be able to show the field data plus wrappers if the field has content, but show some empty field without the wrappers if there is no data.

so far i have tried things like views-view-field.tpl.php

<?php if ($output): ?>
    <p class="tms-caption font-alt-1 text-medium weight-light color-white hide-on-mobile mb-30" data-animate-in="preset:slideInDownShort;duration:900ms;delay:500ms;" data-no-scale><?php print $output; ?></p>
<div class="clear"></div>
  <?php elseif ($empty($output)): ?>
    <div class="view-empty">
      <?php print $empty; ?>
    </div>
  <?php endif; ?>

How can i revise my if statements to it works in either fields or field.tpl.php?

0

1 Answer 1

0

First of all, there is no such thing as $empty in views-view-field.tpl.php

enter image description here

Second, if you're not printing anything when the output is empty, then there is no need to add an else statement. Thus, this should do the trick:

<?php if (!empty($output)) : ?>
    <p class="tms-caption font-alt-1 text-medium weight-light color-white hide-on-mobile mb-30" data-animate-in="preset:slideInDownShort;duration:900ms;delay:500ms;" data-no-scale><?php print $output; ?></p>
<?php endif; ?>

The first line means if NOT empty, in PHP ! = not.

4
  • thank you for the suggestion. i tried <?php if (!empty($output)) : ?> <h1 class="tms-caption title-xlarge text-uppercase weight-bold color-white mb-10" data-animate-in="preset:slideInDownShort;duration:900ms;delay:300ms;" data-no-scale><?php print $output; ?></h1> <div class="clear"></div> <?php endif; ?> and it does not work. however, <?php print $output; ?> outisde the php snippet does work. so i guess something still not correct with this syntax? Commented Sep 11, 2017 at 9:41
  • @drupalstrap did you flush the cache?
    – No Sssweat
    Commented Sep 11, 2017 at 9:50
  • yes, i always do Commented Sep 11, 2017 at 9:57
  • i know the template file is being read as hard coded text shows up. also ... using only <?php print $output; ?> both value and empty values display without a problem. however, once used inside the php statement it does not display anything. Commented Sep 11, 2017 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.