Search code examples
fat-free-framework

Fat Free Framework 3 render arbitrary HTML (not from files)


I was wondering if it would be somehow possibile to render a HTML code portion if this is not lying inside an actual .html file.

You usually do:

$template = new Template;
echo $template->render('whatever.html');

But I'd really like to do:

$template = new Template;
$rendered_content = $template->render($my_html_code);

The reason is: I have created several email templates and it would be great if I could use F3 template engine to replace placeholder variables I've put inside these, and have the final processed HTML code ready to be sent via email.

I know I could probably dump the template to a temp .html file, but this sounds a bit ugly to me, and I'd rather not go through this way unless no other options are available.


Solution

  • You're looking for the resolve() method:

    $tpl = new Template;
    $rendered_content = $tpl->resolve($my_html_code);
    

    This method is enough for replacing "placeholder variables", such as My name is {{ @name }}.

    If you also intend to use template tags (<repeat>, <check>, etc..), you need first to parse() the string, and then pass the result to resolve(). E.g:

    $tpl = new Template;
    $rendered_content = $tpl->resolve($tpl->parse($my_html_code));