How to render a Twig template in the element-api plugin (v2.5) I want to render a Twig template in an API endpoint. Specifically I need HTML for the JSON Feed field "content_html".

My attempts until now (inside the transformer function):

$view = Craft::$app-&gt;getView();
$template = $view-&gt;resolveTemplate('_blog-entry');

// first attempt:
$html = $view-&gt;render($template, ['entry' =&gt; $blogpost]);

// second attempt:
$controller = Craft::$app-&gt;createController('templates/render')[0];

$html = $controller-&gt;render($template, ['entry' =&gt; $blogpost])


But in both ways it says "Template not found" even if the path after the "resolveTemplate" is correct.

Has anybody an idea how to do it? Thanks!
You didn’t tell where that Twig file is located, but I assume your issue has to do with being in the wrong “Template Mode”.

Here’s how you can switch mode:

Craft::$app-&gt;view-&gt;setTemplateMode()


Documentation: https://docs.craftcms.com/v3/extend/updating-plugins.html#rendering-plugin-templates-on-front-end-requests