That is the sort of code you would use in a template or snippet. Adding that into the index.php of your plugin doesn’t make sense as the current page is not known to the plugin.
Could you try and tell us what you are trying to achieve with this plugin?
Well I created a snippet with a simple if / else if / else statement that checked for the presence of a field, but the snippet didn’t seem to work?
If I put the exact if / then / else statement inline in an individual template, it worked fine, but for some reason if I included it as a snippet like I’m doing with various other elements on my site it failed.
Simply put, I’m trying to accomplish this: “If a field is present on the page you’re currently viewing, set a variable to the value in that field. If that field is not present, use this default value instead.”
Then I’m echoing that variable in certain areas of the page.
If the field is empty, it falls back to whatever you pass to the or() field method. Instead of a hardcoded string, you can also fall back to another field:
It’s cleanly printing out the setColor variable like a champ. But when I try to echo that setColor variable somewhere else, it blows up. Is this an issue with the way I’m trying to use PHP in general, or am I doing something Kirby doesn’t like?
But where are you trying to echo that? Of course you can’t just echo a variable anywhere but only where it is defined. Could you please post the complete context?
In any case there is no need to define the variable just to echo it immediately. afterwards. And I have my doubt regarding the usefulness of putting this bit of code into a snippet at all, anyway.
I’m sure you’ll find this appalling, but I love having a cleaner appearance in my template. So using a snippet is just nicer to look at. Let alone the fact that I’m referencing it in a million places.