Using the FatFree Template->render on a string instead of a file

727 views
Skip to first unread message

Bradley Slavik

unread,
Sep 11, 2013, 11:20:06 PM9/11/13
to f3-fra...@googlegroups.com
I see how to use a file for sending email from your examples:

mail(
   $f3->get('to'),
   $f3->get('subject'),
   Template::instance()->render('email.txt','text/html')
);


We are storing our emails as text fields in a database table.
I load body of email into string and expand macros embedded in it.

How do I get this Template object to "render" my string, which is
not in a file?

How do I do something like this:

$body = '<p>This is for {{ @userName }}</p><p>The end</p>';
echo Template::instance()->render($body,'text/html');

Of course, we cannot use the render function just like this, because it is
supposed to take a file name, but I hope there is a way to use the power of
fatfree to render a string instead of a file.


Bradley Slavik

bcosca

unread,
Sep 12, 2013, 2:12:40 AM9/12/13
to f3-fra...@googlegroups.com
Not possible to render templates on the fly without any intervening file I/O due to the fact that templates are compiled/cached prior to output.

ikkez

unread,
Sep 12, 2013, 4:59:24 AM9/12/13
to f3-fra...@googlegroups.com
I tried it once by myself to modify the template class to force it eating my template string, but it's indeed not possible that easy and could have a lot side effects.
But i tested another solution / work-a-round for you.

Put your string into a virtual file, which the template class can work with. To achieve that you need my little File Abstraction Layer Plugin

This is a little example how to do it:


$f3->route('GET /template-from-string', function($f3){
   
    $f3->set('userName', 'John');

    $body = '<p>This is for {{ @userName }}</p><p>The end</p>';

    /** @var FAL $fal */
    $fal = FAL::instance();
    $fal->load('template.html');
    $fal->setContent($body);
    $fileStream = $fal->getFileStream(); // fal://template.html
    // file_exists($fileStream); is now TRUE

    // workaround templates "file must be in UI dir" limitation
    $f3->set('UI',$f3->get('UI').'| ');
    // render file
    echo \Template::instance()->render($fileStream);
});

Notice: this solution is experimental.

Bradley Slavik

unread,
Sep 12, 2013, 3:09:41 PM9/12/13
to f3-fra...@googlegroups.com
I don't mind if it is cached, as long as it creates a unique name since there may be more than one process running at once sending email. As long as FatFree does not get confused and read the wrong file into the wrong session everything is fine. So is ikkez solution what you would recommend?

Nanawel

unread,
Apr 11, 2017, 10:13:21 AM4/11/17
to Fat-Free Framework
I'm also interested in such a feature. My situation is that I have templates located not on the filesystem but in the database (because basically they're CMS and so they can be edited by backend users).

Still no good solution for this case?

ved

unread,
Apr 11, 2017, 10:34:49 AM4/11/17
to Fat-Free Framework
Maybe this:

- write the template from the database to a temporary file
- change the UI variable to the folder where the temp file was created
- call Template::render on the temp file

Not tested but I guess it should work, although not ideal. 

Nanawel

unread,
Apr 11, 2017, 11:35:59 AM4/11/17
to Fat-Free Framework
Yes sure that would work, but it's a pity to need to write a file just to process a template. My goal here is to avoid unnecessary I/O.

Thanks anyway.

ikkez

unread,
Apr 11, 2017, 11:45:34 AM4/11/17
to f3-fra...@googlegroups.com
there is a solution nowadays.

$content = $template->resolve( $template->parse( $template ) );

(should work on 3.6+, is bugged in 3.6.1, but already fixed in 3.6.2-dev)

ved

unread,
Apr 11, 2017, 12:45:36 PM4/11/17
to Fat-Free Framework
Cool, I actually didn't know about that. Thumbs up @ikkez :)

Nanawel

unread,
Apr 11, 2017, 12:51:16 PM4/11/17
to Fat-Free Framework
Ohh... How did I miss that? That's perfect :D

Thanks Ikkez!

Nuwanda

unread,
Oct 18, 2017, 10:03:09 PM10/18/17
to Fat-Free Framework
I've looked on the View and Templates page and on the Templates API page, and there's no mention of resolve.

Where is the doc for this?

ikkez

unread,
Oct 19, 2017, 5:46:58 AM10/19/17
to Fat-Free Framework
resolve is inherited from the Preview class... sure not the best way to display the docs but the class hierachy is View->Preview->Template
https://fatfreeframework.com/3.6/preview#resolve

Ross Elliot

unread,
Oct 20, 2017, 7:12:05 PM10/20/17
to ikkez via Fat-Free Framework
Thanks.
--
-- You've received this message because you are subscribed to the Google Groups group. To post to this group, send an email to f3-fra...@googlegroups.com. To unsubscribe from this group, send an email to f3-framework...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/f3-framework?hl=en
---
You received this message because you are subscribed to the Google Groups "Fat-Free Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to f3-framework...@googlegroups.com.
To post to this group, send email to f3-fra...@googlegroups.com.
Visit this group at https://groups.google.com/group/f3-framework.
To view this discussion on the web visit https://groups.google.com/d/msgid/f3-framework/ac7be4bf-4346-4560-861b-9b4d6cd9eb45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages