Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Template from a variable

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Marc
Smarty n00b


Joined: 30 Jun 2003
Posts: 4

PostPosted: Mon Jun 30, 2003 11:51 am    Post subject: Template from a variable Reply with quote

Hi there,

I got a little problem with smarty (or I don't find it in the manual?!): I try to do a "personalized newsletter" using Smarty. The problem is, that I have a variable containing the template, I want to process and afterward send per email. Example:

Code:
$mail_body = "Hello {$Username},\n\n  this is your personal newsletter."
$smarty->assign("Username", "Hans");
$mail = $smarty->fetch($mail_body);


This one doesn't work, I know, but is there any switch I can trigger to make it work?

Regards,
Marc
Back to top
View user's profile Send private message
bkatsma
Smarty n00b


Joined: 30 Jun 2003
Posts: 3

PostPosted: Mon Jun 30, 2003 2:25 pm    Post subject: Reply with quote

First it has to be assined in php

Code:

<?php

$Username = Hans
$mail_body = "Hello {$Username},\n\n  this is your personal newsletter."
$smarty->assign("Username", "$Username");
$mail = $smarty->fetch($mail_body);

?>
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jun 30, 2003 2:45 pm    Post subject: Reply with quote

no, fetch doesn't take a template's content, but a template's resource-name (mostly a filename in the templates-directory).

you may want to use eval here:

Code:

$smarty->assign('mail_body', $mail_body);
$result = $smarty->fetch('mail.tpl');
(now send $result via mail())


'mail.tpl' would be
Code:
{eval var=$mail_body}


see: http://smarty.php.net/manual/en/language.function.eval.php for reference
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Marc
Smarty n00b


Joined: 30 Jun 2003
Posts: 4

PostPosted: Mon Jun 30, 2003 2:48 pm    Post subject: Reply with quote

Sorry, but it's not about the Smarty command assign (whether correct or not in this example), it's about the fetch, that works with files and not with streams, variables or whatever you would like to call it.

The Problem: $smarty->fetch wants to load a file, but I don't have a file, because my template is an input from another webpage.

Yours,
Marc
Back to top
View user's profile Send private message
Marc
Smarty n00b


Joined: 30 Jun 2003
Posts: 4

PostPosted: Mon Jun 30, 2003 2:50 pm    Post subject: Reply with quote

Sorry messju,

I already typed when your message arrived. I will try that, sound good for me.

Thx,

Marc
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Jun 30, 2003 3:30 pm    Post subject: Reply with quote

no prob Smile.

on another note: if your template comes from a different source (via url-fopen-wrapper or from a database) take a look a resources:
http://smarty.php.net/manual/en/templates.from.elsewhere.php

i don't know if this is overkill in your mail_body-case, but it outperforms eval almost always.

greetings
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Marc
Smarty n00b


Joined: 30 Jun 2003
Posts: 4

PostPosted: Tue Jul 01, 2003 1:05 pm    Post subject: Reply with quote

Would that not be a cute little extension of smarty for the next release? Or any future release?
Code:

$template = "Hello {$Name}!";
$smarty->assign("Name", "Mr. Foo Bar");
$my_output = $smarty->fetch($template, "variable"); # or something like that ;)

Would like to know if someone else already had this problem ...
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Jul 02, 2003 6:11 pm    Post subject: Reply with quote

Quite some time ago I posted a resource plugin at the wiki that retrieves a template from a variable. It is trivial to write, but there are issues. hint: look at the compiled dir after using that technique.

Anyhow, your answer, as messju rightly pointed out, will be a resource. You may decide to implement it in another way, though (eg. write the incoming data to a temp file)
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Jul 02, 2003 6:54 pm    Post subject: Reply with quote

another lean approach may be a simple php-function that loads the eval-plugin and calls smarty_function_eval(array('var'=>$code), $smarty).

but overall: it's intentionally made uneasy for the user to use smarty file-less. smarty shines with written compiled files to disk (accomplished by using templates or resources) and an accelerator. it can get 10 times slower if you use it differently. as a rule of thumb: think over your application-design twice before passing your smarty-code in strings and not in files.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP