Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Sanity check Volt's "is not empty"

Hi there,

This Volt code:

  {% if package_id is not empty and booking_date_int is not empty %}
    {% for package in form.getEntity() %}
      {{ partial('booking/price_table', ['page' : package, 'id' : package_id, 'form' : form, 'booking_date_int' : booking_date_int]) }}
    {% endfor %}
  {% endif %}

Compiles to:

  <?php if ($package_id == !$empty && $booking_date_int == !$empty) { ?>
    <?php foreach ($form->getEntity() as $package) { ?>
      <?php echo $this->partial('booking/price_table', array('page' => $package, 'id' => $package_id, 'form' => $form, 'booking_date_int' => $booking_date_int)); ?>
    <?php } ?>
  <?php } ?>

When displaying errors in PHP, I get:

Notice: Undefined variable: empty in /Applications/MAMP/htdocs/escglam/public/cache/volt/%%applications%%mamp%%htdocs%%escglam%%app%%views%%booking%%index.volt.php on line 13

Is Volt deliberately creating an undefined variable to test for equality with another undefined variable?


This could very well be a bug. The code produced should be

<?php if (!empty($package_id) && !empty($booking_date_int)) { ?>

I don't know exactly the internals of that function but if a $empty variable is created automatically to do the same thing as the code above, then it is not properly defined and thus the notice you get.

In either case this is not the intended behavior. Could you please create an issue on Github so that we can address it?



Will do :)

edited May '14

{% if (not(items is empty)) ... %}
{% endif %}

This works.


Thanks Oleg!

It has already been reported, the is not operator does not exist :)


Hi Max, yep, I am now aware of that fact :)


Quick solution, not needed to surround everything with brackets. {% if not (yourVariable is empty) %}