Adding variable to get_template_part

<?php get_template_part( 'template-parts/component-page/component-$variable); ?>

What is the correct way to add a variable to where I currently have $variable?

Solutions Collecting From Web of "Adding variable to get_template_part"

It looks like you’re just trying to concatenate a string, so it can be done like this:

<?php get_template_part( 'template-parts/component-page/component-' . $variable ); ?>

Or you could use string interpolation (note the double quotes used here):

<?php get_template_part( "template-parts/component-page/component-{$variable}" ); ?>

Alternatively, get_template_part() can take two parameters, $slug and $name:

<?php get_template_part( 'template-parts/component-page/component', $variable ); ?>