Undefined variable in snippet does not work on production


#1

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>
</svg>

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?


#2

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)).


#3

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


#4

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

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

#5

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


#6

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


#7

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.

http://php.net/manual/de/function.error-reporting.php


#8

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.