Working with markups
use Drupal\Core\Render\RendererInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
// Open link in jQuery Ajax modal window
$renderArray['open-in-modal'] = [
'#type' => 'link',
'#title' => "Open in modal",
'#url' => Url::fromRoute('entity.node.canonical', ['node' => 1], ['absolute' => TRUE]),
'#attributes' => [
'class' => ['use-ajax'],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 400,
]),
],
];
// Turn off catching
$renderArray['do-not-cache'] = [
'#type' => 'markup',
'#markup' => "Without caching",
'#cache' => ['max-age' => 0],
];
//disable caching:
\!h \Drupal::service('page_cache_kill_switch')->trigger();
//Suffix, prefix
$renderArray['suffix-prefix'] = [
'#type' => 'markup',
'#prefix' => "<div class = 'some-class'>",
'#suffix' => "</div>",
'#markup' => "Text with suffix, prefix",
];
//Put markup inside theme wrapper container
$renderArray['theme-wrapper-container'] = [
'#type' => 'markup',
[
'#type' => 'item',
'#markup' => "My text with theme wrapper",
],
'#theme_wrappers' => [
'container' => [
'#attributes' => [
'class' => ['my-style'],
],
],
],
];
/** @var RendererInterface $renderService */
$renderService = Drupal::service('renderer');
$body = $renderService->renderRoot($renderArray);
//or
$body = \Drupal::service('renderer')->renderRoot($renderArray);