View previous topic :: View next topic |
Author |
Message |
mateusfig Smarty Rookie
Joined: 27 Aug 2006 Posts: 24
|
Posted: Wed Sep 20, 2006 1:38 pm Post subject: Read Template from Variable as Resource |
|
|
I need compile my templates from a VARIABLE as resource and not a file file.tpl or something like this! Then I don't want pass a path, just get the codig from a variable.
I look for this in the entire Manual but no sucess. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
|
Back to top |
|
mateusfig Smarty Rookie
Joined: 27 Aug 2006 Posts: 24
|
Posted: Wed Sep 20, 2006 1:58 pm Post subject: |
|
|
mohrt wrote: | http://smarty.php.net/manual/en/language.function.eval.php |
Thanks but I could not understand still how to use this. All I need is like this: this->display($variableTemplate);
I need this to show a error page in case of important folders of website are missing or renamed.
________
Rm250
Last edited by mateusfig on Sun Feb 13, 2011 2:59 am; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 20, 2006 2:19 pm Post subject: |
|
|
You'll need to create a custum resource to do this. Here is a complete working example:
Code: |
<?php
require('Smarty.class.php');
$smarty =& new Smarty();
// register the resource name "var"
$smarty->register_resource("var", array("var_template",
"var_timestamp",
"var_secure",
"var_trusted"));
// template variable
$smarty->assign('mytpl',"TEST {\$mynum}\n");
// just a test variable to use in the template
$smarty->assign('mynum','ten');
// the timestamp for the template variable
$smarty->assign('mytpl_time',strtotime('Sep 20 2006 00:00:00'));
// display it
$smarty->display('var:mytpl');
// the resource functions
function var_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
$tpl_source = $smarty_obj->get_template_vars($tpl_name);
return empty($tpl_source) ? false : true;
}
function var_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
// if var has a timestamp value, use it
$time = $smarty_obj->get_template_vars($tpl_name.'_time');
return !empty($time) ? $time : time();
}
function var_secure($tpl_name, &$smarty_obj)
{
// assume all templates are secure
return true;
}
function var_trusted($tpl_name, &$smarty_obj)
{
// not used for templates
}
?> |
This will evaluate the template var "mytpl" as a template resource. I also threw in the ability to set a timestamp for the variable. Otherwise it just gets compiled every time. An empty template var will throw an error. Adjust as you see fit.
This will generate the output:
Last edited by mohrt on Wed Feb 21, 2007 2:49 pm; edited 1 time in total |
|
Back to top |
|
mateusfig Smarty Rookie
Joined: 27 Aug 2006 Posts: 24
|
Posted: Wed Sep 20, 2006 2:51 pm Post subject: |
|
|
mohrt wrote: | You'll need to create a custum resource to do this. Here is a complete working example:
[php]
<?php
require('Smarty.class.php');
$smarty =& new Smarty();
// register the resource name "var"
$smarty->register_resource("var", array("var_template",
"var_timestamp",
"var_secure",
"var_trusted"));
$smarty->assign('mynum','ten');
$smarty->assign('mytpl',"TEST {\$mynum}\n");
$smarty->assign('mytpl_time',strtotime('Sep 20 2006 00:00:00'));
$smarty->display('var:mytpl');
// the resource functions
function var_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
$tpl_source = $smarty_obj->get_template_vars($tpl_name);
return true;
}
function var_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
// if var has a timestamp value, use it
$time = $smarty_obj->get_template_vars($tpl_name.'_time');
return !empty($time) ? $time : time();
}
function var_secure($tpl_name, &$smarty_obj)
{
// assume all templates are secure
return true;
}
function var_trusted($tpl_name, &$smarty_obj)
{
// not used for templates
}
?>[/php]
This will evaluate the template var "mytpl" as a template resource. I also threw in the ability to set a timestamp for the variable. Otherwise it just gets compiled every time. You can go a step further and return false if the tpl var is not assigned, etc. Adjust as you see fit.
This will generate the output:
|
Yes, the example works pretty good. Now I'm trying to aply it to the my project since i use a class and this must to be done into the class. Instead display('index.tpl') the method load() of my class (that build the website) should show display("$this->compiledErros"). I need do it for 2 reasons:
1? the folder w/ error.tpl could be missing (or template folder)
2? i need use the smartyML to translate the errors, and it just does it when display("template.tpl") and this template.tpl is from file resource like themes/core/tpl/
Thank you so much
________
Easy vape
Last edited by mateusfig on Sun Feb 13, 2011 2:59 am; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 20, 2006 3:04 pm Post subject: |
|
|
There is no good way to pass the actual template source as an argument to the $smarty->display() call. You will have to do one step:
[php:1:76dad7d8d0]$smarty->assign('errors',$this->compiledErros);
$smarty->display('var:errors');[/php:1:76dad7d8d0] |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 20, 2006 3:08 pm Post subject: |
|
|
Another option, you can extend the Smarty class and make display_var() functionality (UNTESTED):
[php:1:a0326ddf34]class MySmarty extends Smarty {
function Smarty() {
// do custom resource register here
}
function display_var($tplsource) {
$this->assign('source',$tplsource);
$this->display('var:source');
}
}
// put custom resource functions here[/php:1:a0326ddf34]
Then you can call like you want:
[php:1:a0326ddf34]$smarty = new MySmarty();
$smarty->display_var($source);[/php:1:a0326ddf34]
EDIT: changed the function name to display_var() |
|
Back to top |
|
mateusfig Smarty Rookie
Joined: 27 Aug 2006 Posts: 24
|
Posted: Wed Sep 20, 2006 4:04 pm Post subject: |
|
|
mohrt wrote: | Another option, you can extend the Smarty class and make display_var() functionality (UNTESTED):
[php]class MySmarty extends Smarty {
function MySmarty() {
// do custom resource register here
}
function display_var($tplsource) {
$this->assign('source',$tplsource);
$this->display('var:source');
}
}
// put custom resource functions here[/php]
Then you can call like you want:
[php]$smarty = new MySmarty();
$smarty->display_var($source);[/php]
EDIT: changed the function name to display_var() |
this is the error i got when i try use your last example:
Warning: call_user_func_array(): First argumented is expected to be a valid callback, 'var_template' was given in
Then, i guess a simple setting will do this work....But I don't know what to do.
________
Washington medical marijuana dispensaries
Last edited by mateusfig on Sun Feb 13, 2011 2:59 am; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 20, 2006 4:21 pm Post subject: |
|
|
It seems that your resource functions are not found. Did you include them? Put the registration call and resource functions in the places that I put the comments.
[php:1:7de2becc6e]class MySmarty extends Smarty {
function MySmarty() {
$this->register_resource("var", array("var_template",
"var_timestamp",
"var_secure",
"var_trusted"));
}
function display_var($tplsource) {
$this->assign('source',$tplsource);
$this->display('var:source');
}
}
// the resource functions
function var_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
$tpl_source = $smarty_obj->get_template_vars($tpl_name);
return empty($tpl_source) ? false : true;
}
function var_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
// if var has a timestamp value, use it
$time = $smarty_obj->get_template_vars($tpl_name.'_time');
return !empty($time) ? $time : time();
}
function var_secure($tpl_name, &$smarty_obj)
{
// assume all templates are secure
return true;
}
function var_trusted($tpl_name, &$smarty_obj)
{
// not used for templates
}
[/php:1:7de2becc6e]
Last edited by mohrt on Wed Sep 20, 2006 5:02 pm; edited 1 time in total |
|
Back to top |
|
mateusfig Smarty Rookie
Joined: 27 Aug 2006 Posts: 24
|
Posted: Wed Sep 20, 2006 4:55 pm Post subject: |
|
|
hey, i got to make it work!! wow, great!
i fixed my fault w/ the "resources functions", placing them in the right place. Afer it i removed the Website extends SmartyML to Website extends SmartyMyCfg directly (before i was using the SmartyML (multi-language suport class) extended from SmartyMyCfg and Website extended from SmartyML).
Then the SmartyML class was messing everything up, although i dont know why.
I need find the issue out because i need use SmartyML for site's multi-language suport
thank u so much!!
________
[URL=http://www.bmw-tech.org/wiki/Airhead_(motorcycle)]Airhead (Motorcycle)[/URL]
Last edited by mateusfig on Sun Feb 13, 2011 2:59 am; edited 1 time in total |
|
Back to top |
|
mateusfig Smarty Rookie
Joined: 27 Aug 2006 Posts: 24
|
Posted: Wed Sep 20, 2006 5:45 pm Post subject: |
|
|
I got new problems. The stuff works but when the value of $variable (the one w/ the source code) changes, the first sis printed still. I deleted the compiled files from compile directory myself, then the new value comes up. What is this? I think something the compile control.
ps.: caching is off at the moment. and it was everything ok before to use the new feature described by thead i created
________
Extreme q vaporizer
Last edited by mateusfig on Sun Feb 13, 2011 2:59 am; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 20, 2006 6:10 pm Post subject: |
|
|
sounds like the timestamp isn't working right or something, and the new data isn't getting recompiled.
Use md5($source) as the compile_id, see how that works. |
|
Back to top |
|
mateusfig Smarty Rookie
Joined: 27 Aug 2006 Posts: 24
|
Posted: Wed Sep 20, 2006 6:20 pm Post subject: |
|
|
mohrt wrote: | sounds like the timestamp isn't working right or something, and the new data isn't getting recompiled.
Use md5($source) as the compile_id, see how that works. |
Yes, now its working after use md5.
________
Yamaha Mt-100
Last edited by mateusfig on Sun Feb 13, 2011 2:59 am; edited 1 time in total |
|
Back to top |
|
mateusfig Smarty Rookie
Joined: 27 Aug 2006 Posts: 24
|
Posted: Wed Sep 20, 2006 8:23 pm Post subject: |
|
|
Why the SmartyML class dont works after use this trick? I tested SmartyML separated and it works normal. Any hint/idea?
Fatal error: Call to a member function on a non-object in .......\smartyML.class.php on line 57
line 57: $_smarty_compile_id = $this->language->getCurrentLanguage().'-'.$_smarty_compile_id;
if I uncomment this line comes follow error: Error loading Multilanguage Support
This last error cames from smarty_prefilter_i18n() function that is before the class SmartyML
________
Volcano vaporizer
Last edited by mateusfig on Wed Mar 16, 2011 8:49 pm; edited 1 time in total |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed Sep 20, 2006 8:39 pm Post subject: |
|
|
can't debug your app for you, sorry. see if $this is actually an object. |
|
Back to top |
|
|