View previous topic :: View next topic |
Author |
Message |
Marc Smarty n00b
Joined: 30 Jun 2003 Posts: 4
|
Posted: Mon Jun 30, 2003 11:51 am Post subject: Template from a variable |
|
|
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 |
|
bkatsma Smarty n00b
Joined: 30 Jun 2003 Posts: 3
|
Posted: Mon Jun 30, 2003 2:25 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Jun 30, 2003 2:45 pm Post subject: |
|
|
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 |
|
Marc Smarty n00b
Joined: 30 Jun 2003 Posts: 4
|
Posted: Mon Jun 30, 2003 2:48 pm Post subject: |
|
|
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 |
|
Marc Smarty n00b
Joined: 30 Jun 2003 Posts: 4
|
Posted: Mon Jun 30, 2003 2:50 pm Post subject: |
|
|
Sorry messju,
I already typed when your message arrived. I will try that, sound good for me.
Thx,
Marc |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Jun 30, 2003 3:30 pm Post subject: |
|
|
no prob .
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 |
|
Marc Smarty n00b
Joined: 30 Jun 2003 Posts: 4
|
Posted: Tue Jul 01, 2003 1:05 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Jul 02, 2003 6:11 pm Post subject: |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Jul 02, 2003 6:54 pm Post subject: |
|
|
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 |
|
|