NO BUG: Divide by function in Metabox Views created Critical Error when Editing Bricks

Browser: Chrome 110
OS: Windows
Video: mbv-divide-by-crashes-bricks-editor

Hi, I created a Metabox View and used Bricks to display the shortcode for that View in the Rich Text Editor.

But I’m running into a strange issue where the frontend page with the Metabox View renders just fine. The output of the Metabox View is also correct.

However, when I use a Twig template statement with a divider in it, such as:

{% set xxx_pct = (xyz / abc * 100) %}

or

{{ xxx / yyy }}

The Bricks Editor on the backend suddenly throws a Critical Error message.

If I remove the divide by math statement, the Bricks Editor on the backend reverts back normally and I can edit the template again.

I am confused at why the frontend would render the Metabox View with no issues, but the backend would throw a critical error and what I would need to do to fix this?

Thank you.

Hi @saint ,

I think the template already set a populate content am I right?
I suspect the star_cnt_ttl or five_start_cnt variable value in your video could be zero or undefine due to certain reason.

Can you write some logic to handle these possibility?

Alternatively, please turn on Error log and check what is the actual error.

Regards,
Jenn

Hi Jenn,

You are totally right. Turns out, it was the can’t “Divide by Zero” that was causing it. I’ve added in some additional logic to handle this, and it looks like it is working.

For those that may run into the same issue in the future, I wrapped it in an if statement to check if it is greater than zero first.

{% if ( five_star_cnt > 0 ) and ( star_cnt_ttl > 0 ) %}
	{{ five_star_cnt / star_cnt_ttl }}
{% endif %}

Bricks will throw a critical error in the backend when you go and edit it if there is a “Divide by Zero” issue, but the calculation will show up fine on the frontend, so something to check or be aware of when doing calcs.