Toggle Fieldtype
A nice little toggle switch generally used to manage settings-type variables. It stores true or false and is delightfully uncomplicated, just like our relationship with yogurt.
Can I haz green?#
Some people like their toggles green. It's a personal preference, just like white or milk chocolate. Since the control panel theme is customizable, you can make your toggles green! Or whatever other color for that matter… but maybe not red? Look for the Theme section in config/statamic/cp.php and set the switch-bg your preferred color. Here's green:
'theme' => [
'switch-bg' => Color::Green[500],
'dark-switch-bg' => Color::Green[600],
],
Data Structure#
Flicking the toggle to the right sets to the value to true, left to false.
do_the_thing: true
Templating#
Toggles are usually used to control logic, so you can combine them with {{ if }} statements in your templates to handle all manner of show/hide wizardry.
{{ if do_the_thing }} It does it {{ /if }}
The following example uses the fetch helper function, which resolves Value instances for you and returns the underlying value. This way you always get the real "truthy" value, regardless of how you retrieved $do_the_thing.
<?php
use function Statamic\View\Blade\{fetch};
?>
@if (fetch($do_the_thing))
It does it
@endif