Output a field value inside a variable

Hi,

Currently I have something like this:

<section class="bg-color-<?= $background->color()->value() ?> padding">

But I’ve started to improve my workflow by working with variables when the logic is a little more complex, to later on echo the final variable into the file:

if ($layout && $layout->fullScreen()->bool()) {
  $primary_class = 'full-screen';
}

<section class="<?php echo $primary_class; ?>">

How can I output a value from a field inside a variable? Something like this :grimacing: :grimacing: :grimacing:

$primary_class = 'bg-color-'echo $background->color()->value()' padding';

You have to concatenate the strings and the field value with .. Don’t echo anything here.

$primary_class = 'bg-color-' .  $background->color()->value() . ' padding';

Make sure that there is a background color available.

What is $layout, btw. a page?

It might be useful to put the logic into a controller.

1 Like

You can also use Heredoc which makes it much more readable if its quite a bit of stuff.

1 Like

Awesome!

$layout is a structure field:
$layout = $page->layoutHero()->toStructure()->first();