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?

Thanks.

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?

Thanks!



22.0k
Accepted
answer

Will do :)



7.1k
edited May '14
{% if (not(items is empty)) ... %}
....
{% endif %}

This works.



22.0k

Thanks Oleg!

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

https://github.com/phalcon/cphalcon/issues/2221



22.0k

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



79

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