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?
texnixe
December 28, 2018, 12:18pm
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)).
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 …
texnixe
December 28, 2018, 12:21pm
4
Instead of the if statement, I’d use a shorter ternary operator:
echo isset($class)? $class:'';
It is working dear @texnixe , as always
But why is it working locally but not on live server
texnixe
December 28, 2018, 12:24pm
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
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.