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

How to get the templates from database and fetch it?

 
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
dpol
Smarty n00b


Joined: 24 Oct 2018
Posts: 1

PostPosted: Wed Oct 24, 2018 11:33 am    Post subject: How to get the templates from database and fetch it? Reply with quote

Hi all... cannot repeat example https://www.smarty.net/docs/en/plugins.resources.tpl
smarty 3.1.33
php 7.2.10

Index view:
Code:
class Index extends Page {
...
public function render(\stdClass $data)
    {
        return $this->dom->fetch('pgsql:test.tpl');
...

Page view:
Code:
abstract class Page
{
    abstract public function render(\stdClass $data);

    protected function __construct(\PDO $db) {
        ...
        require_once 'smarty/Autoloader.php';
        \Smarty_Autoloader::register();
        $this->dom = new \Smarty;
       
        $this->dom->setTemplateDir(sprintf('%s/%s/', $this->settings->path->tpl, 'templates'));
        $this->dom->setCompileDir(sprintf('%s/%s/', $this->settings->path->tpl, 'templates_c'));
        $this->dom->setConfigDir(sprintf('%s/%s/', $this->settings->path->tpl, 'configs'));
        $this->dom->setCacheDir(sprintf('%s/%s/', $this->settings->path->tpl, 'cache'));
        $this->dom->addPluginsDir(sprintf('%s/%s/', $this->settings->path->tpl, 'plugins'));
       
        require_once sprintf('%s/%s/%s', $this->settings->path->tpl, 'plugins', 'resource.pgsql.php');
       
        $this->dom->registerResource('pgsql', new \Smarty_Resource_Pgsql($db));
    }
...

Custom resource:
Code:
class Smarty_Resource_Pgsql extends \Smarty_Resource_Custom
{
    public function __construct(\PDO $db)
    {
        $this->db = $db;
       
        $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name');
        $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name');
    }

    /**
     * @var \PDO instance
     */
    protected $db = null;
   
    /**
     *
     * @var \PDOStatement prepared fetch() statement
     */
    protected $fetch = null;
   
    /**
     *
     * @var \PDOStatement prepared fetchTimestamp() statement
     */
    protected $mtime = null;

    /**
     * Fetch a template and its modification time from database
     *
     * @param string $name template name
     * @param string $source template source
     * @param integer $mtime template modification timestamp (epoch)
     * @return void
     */
    protected function fetch($name, &$source, &$mtime)
    {
        $this->fetch->execute(['name' => $name]);
        $row = $this->fetch->fetch();
        $this->fetch->closeCursor();
        if ($row) {
            $source = $row['source'];
            $mtime = strtotime($row['modified']);
        } else {
            $source = null;
            $mtime = null;
        }
    }

    /**
     * Fetch a template's modification time from database
     *
     * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source.
     * @param string $name template name
     * @return integer timestamp (epoch) the template was modified
     */
    protected function fetchTimestamp($name)
    {
        $this->mtime->execute(['name' => $name]);
        $mtime = $this->mtime->fetchColumn();
        $this->mtime->closeCursor();
        return strtotime($mtime);
    }

}

Template in db:
Code:
sci=> select * from templates;
 id |   name   |          modified          |         source         
----+----------+----------------------------+------------------------
  1 | test.tpl | 2018-10-24 14:23:30.932706 | {$x="hello world"}{$x}
(1 строка)

sci=>

But get error:
Code:
#0 /usr/share/php/smarty/sysplugins/smarty_internal_templatebase.php(232): Smarty_Internal_Template->render(false, 0)
#1 /usr/share/php/smarty/sysplugins/smarty_internal_templatebase.php(116): Smarty_Internal_TemplateBase->_execute(Object(Smarty_Internal_Template), NULL, NULL, NULL, 0)
#2 phar://smarty3.phar/com/sci/view/Index.php(25): Smarty_Internal_TemplateBase->fetch('pgsql:test.tpl')
#3 phar://smarty3.phar/com/sci/controller/Index.php(27): com\sci\view\Index->render(Object(stdClass))
#4 phar://smarty3.phar/com/sci/controller/Server.php(32): com\sci\controller\Index->run()
#5 htdocs/index.php(26): com\sci\controller\Server->run()
#6 {main}

Anybody help ?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Oct 25, 2018 11:22 am    Post subject: Reply with quote

1. Don't abuse "Smarty_*" space, or somebody may think it's not your own code.
[s]2. Where did you do the "$smarty->registerResource(…);" ?[/s] Scratch that, I see now.

Ok, what is the actual ERROR MESSAGE? Backtrace is only a backtrace.

P.S.
There's a russian support forum, if that's going to be easier for you.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Oct 25, 2018 11:25 am    Post subject: Reply with quote

P.P.S.
If I were you, I'd pass Smarty object to Page constructor, not PDO.
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 -> General 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