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

Problem: Need to access templates in 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 -> Smarty Development
View previous topic :: View next topic  
Author Message
neil_rutherford
Smarty Rookie


Joined: 13 Jan 2004
Posts: 5

PostPosted: Tue Jan 13, 2004 9:32 am    Post subject: Problem: Need to access templates in a variable. Reply with quote

I have moved all the e-mail templates from disk and put them into a database table (all the site templates still remain on disk).

Prior to the introduction of templates in a database table, i would construct my e-mails like so:

// Assume assigns have been called
$mail_header = $smarty->fetch( "email_headerXX.tpl" );
$mail_body = $smarty->fetch( "email_bodyXX.tpl" );
$mail_footer = $smarty->fetch( "email_footerXX.tpl" );

$mail_message = $mail_header . $mail_body . $mail_footer;

then using the mailing function to send $mail_message to each user.


I've hit a limitation in smarty, the fetch and display functions only read
from template files and there is no alternative for reading from a variable
like $mail_message which is constructed from templates.

I don't want a plugin that can read from the tables directly as i need other information stored with the templates, i just need a more generic function that can read from variables containing template data.

like

// Assume assigns have been called
$mail_message = $smarty->eval( $mail_header . $mail_body . $mail_footer );

I know it goes against the caching rules, but this feature would help greatly.
Can anyone help on a workaround/solution?
Back to top
View user's profile Send private message
Gerald
Smarty Regular


Joined: 26 Nov 2003
Posts: 53
Location: Lyon [France]

PostPosted: Tue Jan 13, 2004 9:37 am    Post subject: Reply with quote

You could write the content of your variable into a file and use this file as the template.....

that should work ..... I guess.
Back to top
View user's profile Send private message Visit poster's website
neil_rutherford
Smarty Rookie


Joined: 13 Jan 2004
Posts: 5

PostPosted: Tue Jan 13, 2004 9:51 am    Post subject: Reply with quote

I had already considered that, but when you have mulitple users
on the system, all using the activation system and various other e-mails are flying around, that could cause a problem with concurrency.
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Jan 13, 2004 10:35 am    Post subject: Reply with quote

if you don't want to write a resource-plugin you can try to use {eval}.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
neil_rutherford
Smarty Rookie


Joined: 13 Jan 2004
Posts: 5

PostPosted: Tue Jan 13, 2004 4:05 pm    Post subject: Reply with quote

When i said i don't want a plugin, i need to get access to the data in the tables and meta data, i need direct access to the data.

Is there a way I can write a plugin which is similar to the fetch smarty method (overloaded version) which i can pass a variable containing the template instead of the name?
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Jan 13, 2004 4:26 pm    Post subject: Reply with quote

you can load the eval-plugin and use it from php:
[php:1:9c6a2dc242]
function eval($source) {
require_once $this->_get_plugin_filepath('function', 'eval');
return smarty_function_eval(array('var'=>$source), $this);
}
[/php:1:9c6a2dc242]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
neil_rutherford
Smarty Rookie


Joined: 13 Jan 2004
Posts: 5

PostPosted: Tue Jan 13, 2004 6:07 pm    Post subject: Reply with quote

Sorry about this, i haven't done any plugs before.

What do i call the file for the following code:

[php:1:22f3aef847]
function eval($source) {
require_once $this->_get_plugin_filepath('function', 'eval');
return smarty_function_eval(array('var'=>$source), $this);
}
[/php:1:22f3aef847]

I take it i need to drop this into the smarty plugins directory????

And to use from in my PHP code i call?????

[php:1:22f3aef847]
$tpl = new Smarty;
$tpl->assign( 'EMAIL_ADDRESS', $email_address );
$email_message = $tpl->eval( $email_template );
[/php:1:22f3aef847]
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Tue Jan 13, 2004 6:20 pm    Post subject: Reply with quote

it was meant as a method in an overloaded smarty:

[php:1:2d02793d72]
class MySmarty extends Smarty {

function eval($source) {
require_once $this->_get_plugin_filepath('function', 'eval');
return smarty_function_eval(array('var'=>$source), $this);
}

}

$tpl = new MySmarty;
$email_message = $tpl->eval( $email_template );
[/php:1:2d02793d72]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
neil_rutherford
Smarty Rookie


Joined: 13 Jan 2004
Posts: 5

PostPosted: Tue Jan 13, 2004 6:44 pm    Post subject: Reply with quote

Thanks... kind of thing i was looking for!

There is a eval function, but no method to be called directly.
Ideally this needs to be added to the next version of Smarty.

Who would i contact to recommend this to?
Back to top
View user's profile Send private message
messju
Administrator


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

PostPosted: Wed Jan 14, 2004 10:06 am    Post subject: Reply with quote

neil_rutherford wrote:
Who would i contact to recommend this to?


umm, me mabye Smile

but i wouldn't add this to the distribution. it's good if employing eval has a little hurdle. the method i posted above is really apparent if one read the chapter about plugins in the manual, BTW.

eval is really bad. you said yourself "I know it goes against the caching rules" in your first post in this thread. we shouldn't make it too easy for people to shoot themselves in the foot. having to extend smarty a little should make people think twice (or even more times if necessary) if they really want to use eval and throw most of smarty's strengths overboard.
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 -> Smarty Development 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