Undefined variable in snippet does not work on production

Im using this snippet on my site:

<svg class="icon-<?= $icon ?><?php if($class): ?> <?= $class ?><?php endif ?>">
	<use xlink:href="<?php echo url('assets/svg/symbol-defs.svg#icon-'. $icon .''); ?>"></use>

and call it like:

<?php snippet('svg', array(
	'icon' => 'trip'
)) ?>

On my development environment everything works, but on production (DigitalOcean Ubuntu 18.04.1 LTS) I got the following error

Undefined variable: class

How to get the code to work on production?

The problem is not the icon, but the $classvariable. Where is that defined? You don’t pass it to the snippet.

To check if the variable is set, it is not enough to do if($class), but if(isset($class)).

1 Like

Here is a snippet with the variable $class:

<?php snippet('svg', array(
	'icon' => 'arrow_left',
	'class' => 'navigation__icon navigation__icon--prev'
)) ?>

I will try your suggestion @texnixe

Instead of the if statement, I’d use a shorter ternary operator:

echo isset($class)? $class:'';
1 Like

It is working dear @texnixe, as always :slight_smile:

But why is it working locally but not on live server :roll_eyes:

It would probably make sense to do the same for icon, in case you use the snippet somewhere else and forget to pass the icon…

The reason why it works in one place but not in the other might be different error settings.


1 Like

Case sensitivity can also cause it, but looking at your code, I don’t think thats a problem here. I’ve had things work fine on Ubuntu but not on CentOS, despite both being case sensitive operating systems. Something to keep in mind though.

1 Like