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

Read Template from Variable as Resource
Goto page 1, 2  Next
 
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
mateusfig
Smarty Rookie


Joined: 27 Aug 2006
Posts: 24

PostPosted: Wed Sep 20, 2006 1:38 pm    Post subject: Read Template from Variable as Resource Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 20, 2006 1:44 pm    Post subject: Reply with quote

http://smarty.php.net/manual/en/language.function.eval.php
Back to top
View user's profile Send private message Visit poster's website
mateusfig
Smarty Rookie


Joined: 27 Aug 2006
Posts: 24

PostPosted: Wed Sep 20, 2006 1:58 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 20, 2006 2:19 pm    Post subject: Reply with quote

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:

Code:
TEST ten


Last edited by mohrt on Wed Feb 21, 2007 2:49 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
mateusfig
Smarty Rookie


Joined: 27 Aug 2006
Posts: 24

PostPosted: Wed Sep 20, 2006 2:51 pm    Post subject: Reply with quote

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:

Code:
TEST ten



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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 20, 2006 3:04 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 20, 2006 3:08 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mateusfig
Smarty Rookie


Joined: 27 Aug 2006
Posts: 24

PostPosted: Wed Sep 20, 2006 4:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 20, 2006 4:21 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mateusfig
Smarty Rookie


Joined: 27 Aug 2006
Posts: 24

PostPosted: Wed Sep 20, 2006 4:55 pm    Post subject: Reply with quote

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
View user's profile Send private message
mateusfig
Smarty Rookie


Joined: 27 Aug 2006
Posts: 24

PostPosted: Wed Sep 20, 2006 5:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 20, 2006 6:10 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
mateusfig
Smarty Rookie


Joined: 27 Aug 2006
Posts: 24

PostPosted: Wed Sep 20, 2006 6:20 pm    Post subject: Reply with quote

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 Laughing 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
View user's profile Send private message
mateusfig
Smarty Rookie


Joined: 27 Aug 2006
Posts: 24

PostPosted: Wed Sep 20, 2006 8:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Sep 20, 2006 8:39 pm    Post subject: Reply with quote

can't debug your app for you, sorry. see if $this is actually an object.
Back to top
View user's profile Send private message 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
Goto page 1, 2  Next
Page 1 of 2

 
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