XF 2.2 Render template html output to a variable

ivanr

Member
Hey all,

Is it possible to render template or macro and get html output into a variable? I tried doing something like this:
$latest_posts = $this->app->templater()->renderMacro('public:modules_macros','latest_posts', $viewParams);
or
$latest_posts = $this->app->templater()->renderTemplate('latest_posts', $viewParams);

But I only get empty string, no erros of any kind.

Thanks in advance :)
 
Although the preferred way is to create a controller action instead, and return a view by providing the template name and view parameters, to get this code working, you need to include the template type - in type:templatename format.

If it is a public template, then:
PHP:
$latest_posts = $this->app->templater()->renderTemplate('public:latest_posts', $viewParams);
 
Thank you for the reply, indeed template type was missing from variable (would give error). Problem was somewhere in my dev server which i yet have to determine, on local this code works perfectly.

Btw, im calling this from controller action method and reason for this is that I want to render several macros for different things ( fe. latests posts, latest stats etc,) and pack the all in single xen json response. Addon is called from joomla and provides data for several joomla modules.
 
The templater hasn't been fully initialized in the controller yet, so a lot of global variables will be missing when you try to render templates there. The correct place to render templates is a view, or as includes in the final template.
 
Back
Top Bottom