We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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!



38.8k
Accepted
answer

Will do :)



8.1k
edited May '14

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

This works.



38.8k

Thanks Oleg!

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

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



38.8k

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) %}