|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
jovenshadow11 Smarty Rookie
Joined: 28 Jun 2011 Posts: 14
|
Posted: Wed Jul 13, 2011 8:59 am Post subject: template from database |
|
|
Hello, i just start using smarty 3 and i found this sample but its not working, what i want to do is save my template in the database so that i can easily edit in my CMS.
i download the latest update in the SVN also.
require('plugins/Smarty-3.0.8/libs/Smarty.class.php');
// put these function somewhere in your application
function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
{
// do database call here to fetch your template,
// populating $tpl_source with actual template contents
$tpl_source = "This is the template text";
// return true on success, false to generate failure notification
return true;
}
function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
{
// do database call here to populate $tpl_timestamp
// with unix epoch time value of last template modification.
// This is used to determine if recompile is necessary.
$tpl_timestamp = time(); // this example will always recompile!
// return true on success, false to generate failure notification
return true;
}
function db_get_secure($tpl_name, &$smarty_obj)
{
// assume all templates are secure
return true;
}
function db_get_trusted($tpl_name, &$smarty_obj)
{
// not used for templates
}
// register the resource name "db"
$smarty->register->resource("db", array("db_get_template",
"db_get_timestamp",
"db_get_secure",
"db_get_trusted"));
// using resource from php script
$smarty->display("db:index.tpl");
this is the error im getting:
Fatal error: Call to a member function resource() on a non-object in D:\joven\website\atic\website\development\website\temp.php on line 37 |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
|
Back to top |
|
jovenshadow11 Smarty Rookie
Joined: 28 Jun 2011 Posts: 14
|
Posted: Wed Jul 13, 2011 12:32 pm Post subject: |
|
|
Thanks for the reply!
this is what i did...
from this
$smarty->register->resource("db", array("db_get_template",
"db_get_timestamp",
"db_get_secure",
"db_get_trusted"));
i change to
$smarty->registerResource("db", array("db_get_template",
"db_get_timestamp",
"db_get_secure",
"db_get_trusted"));
the result
Fatal error: Call to a member function registerResource() on a non-object in D:\joven\website\atic\website\development\website\temp.php on line 37
did i missed something? or do i need to set on smarty class? pls help... |
|
Back to top |
|
jovenshadow11 Smarty Rookie
Joined: 28 Jun 2011 Posts: 14
|
Posted: Wed Jul 13, 2011 12:38 pm Post subject: |
|
|
FYI, im using smarty 3.
what i did now is copy the sample i found on this page http://www.smarty.net/docs/en/template.resources.tpl
this is the sample i found
<?php
//i add this to locate the smarty class on my local drive.
require('plugins/Smarty-3.0.8/libs/Smarty.class.php');
// put these function somewhere in your application
function db_get_template ($tpl_name, &$tpl_source, $smarty_obj)
{
// do database call here to fetch your template,
// populating $tpl_source with actual template contents
$tpl_source = "This is the template text";
// return true on success, false to generate failure notification
return true;
}
function db_get_timestamp($tpl_name, &$tpl_timestamp, $smarty_obj)
{
// do database call here to populate $tpl_timestamp
// with unix epoch time value of last template modification.
// This is used to determine if recompile is necessary.
$tpl_timestamp = time(); // this example will always recompile!
// return true on success, false to generate failure notification
return true;
}
function db_get_secure($tpl_name, $smarty_obj)
{
// assume all templates are secure
return true;
}
function db_get_trusted($tpl_name, $smarty_obj)
{
// not used for templates
}
// register the resource name "db"
$smarty->registerResource("db", array("db_get_template",
"db_get_timestamp",
"db_get_secure",
"db_get_trusted"));
// using resource from php script
$smarty->display("db:index.tpl");
?>
again still not working, is there something missing? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Jul 13, 2011 12:39 pm Post subject: |
|
|
You did not create the instance of the Smarty class.
$smarty = new Smarty(); |
|
Back to top |
|
jovenshadow11 Smarty Rookie
Joined: 28 Jun 2011 Posts: 14
|
Posted: Wed Jul 13, 2011 12:48 pm Post subject: |
|
|
U.Tews wrote: | You did not create the instance of the Smarty class.
$smarty = new Smarty(); |
now its working! thanks a lot!
can you help me also how can i solve my other problem?
i want to put {block name=b1} and {block name=b2} on my template.
but on the program how will i do it that my program will know how many block i have on the template and assign the right data on each block?
can you help me again please? |
|
Back to top |
|
|
|
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
|
|