Inline Template with Render Array in #context I'm using the https://api.drupal.org/InlineTemplate to render a string with a variable, and that variable is itself another theme function.  

My reading of the documentation is that this is supported; based on the fact that the #context variable can be a string or render array.


  #context: (array) The variables to substitute into the Twig template. Each variable may be a string or a render array.


However, my implementation is not working:

    $my_subtemplate = [
      '#type' =&gt; 'inline_template',
      '#template' =&gt; '{% trans %}my {{ foo }} here{% endtrans %}',
      '#context' =&gt; ['foo' =&gt; 'bar'],
    ];

    $my_string_with_embedded_themable = [
      '#type' =&gt; 'inline_template',
      '#template' =&gt; '{% trans %}Print this string and this subtemplate: {{ my_subtemplate }}.{% endtrans %}',
      '#context' =&gt; [
        'my_subtemplate' =&gt; $my_subtemplate,
      ],
    ];


The generated output is:


  Print this string and this subtemplate: .


I've confirmed that my_subtemplate generates the correct output on its own:


  my bar here


But when its placed into a variable of another inline template, it does not render.
The issue not in the inline_template but the issue comes from {% trans %} if you place a render array variable into {% trans %}{% endtrans %} you will get the following error and it will not rendered:


  Warning: htmlspecialchars() expects parameter 1 to be string, array given in Drupal\Component\Utility\Html::escape()


So you have two options:

1) get the variable out of {% trans %}{% endtrans %} like the following:

    $my_subtemplate = [
      '#type'     =&gt; 'inline_template',
      '#template' =&gt; '{% trans %} my {{ foo }} here {% endtrans %}',
      '#context'  =&gt; [
        'foo' =&gt; 'bar'
      ],
    ];
    $my_string_with_embedded_themable = [
      '#type'     =&gt; 'inline_template',
      '#template' =&gt; '{% trans %} Print this string and this subtemplate: {% endtrans %} {{ my_subtemplate }} .',
      '#context'  =&gt; [
        'my_subtemplate' =&gt; $my_subtemplate,
      ],
    ];


2) Pre-render the renderable array variable to a string first.

    $my_subtemplate = [
      '#type'     =&gt; 'inline_template',
      '#template' =&gt; '{% trans %} my {{ foo }} here {% endtrans %}',
      '#context'  =&gt; [
        'foo' =&gt; 'bar'
      ],
    ];

    $renderer = \Drupal::service('renderer'); 
    $my_subtemplate = $renderer-&gt;renderRoot($my_subtemplate); // this fixes it

    $my_string_with_embedded_themable = [
      '#type'     =&gt; 'inline_template',
      '#template' =&gt; '{% trans %} Print this string and this subtemplate: {{ my_subtemplate }}.{% endtrans %}',
      '#context'  =&gt; [
        'my_subtemplate' =&gt; $my_subtemplate,
      ],
    ];