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

template from database

 
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 3
View previous topic :: View next topic  
Author Message
jovenshadow11
Smarty Rookie


Joined: 28 Jun 2011
Posts: 14

PostPosted: Wed Jul 13, 2011 8:59 am    Post subject: template from database Reply with quote

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


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Jul 13, 2011 12:28 pm    Post subject: Reply with quote

During the early days of Smarty3 there was some API change.

See http://www.smarty.net/docs/en/api.register.resource.tpl
Back to top
View user's profile Send private message
jovenshadow11
Smarty Rookie


Joined: 28 Jun 2011
Posts: 14

PostPosted: Wed Jul 13, 2011 12:32 pm    Post subject: Reply with quote

U.Tews wrote:
During the early days of Smarty3 there was some API change.

See http://www.smarty.net/docs/en/api.register.resource.tpl


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


Joined: 28 Jun 2011
Posts: 14

PostPosted: Wed Jul 13, 2011 12:38 pm    Post subject: Reply with quote

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


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Jul 13, 2011 12:39 pm    Post subject: Reply with quote

You did not create the instance of the Smarty class.

$smarty = new Smarty();
Back to top
View user's profile Send private message
jovenshadow11
Smarty Rookie


Joined: 28 Jun 2011
Posts: 14

PostPosted: Wed Jul 13, 2011 12:48 pm    Post subject: Reply with quote

U.Tews wrote:
You did not create the instance of the Smarty class.

$smarty = new Smarty();


now its working! Smile 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
View user's profile Send private message
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 3 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