Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Problem mit Datenbank als TPL Resource

 
Post new topic   Reply to topic    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
eazytrader
Smarty Rookie


Joined: 16 Jul 2010
Posts: 33

PostPosted: Thu Dec 27, 2012 9:51 pm    Post subject: Problem mit Datenbank als TPL Resource Reply with quote

Hallo,
ich habe mich nun daran versucht, die Datenbank als Resource für meine Templates zu verwenden.
Leider klappt das nicht so ganz wie ich will und ich möchte herzlichst darum bitten, das da mal jemand drüberschaut. Vielleicht findet Ihr den Fehler.
Zu Testzwecken habe ich das ganze mal 1zu1 aus der Smarty Doku entnommen.

smartyPlugin.class.php:
Code:

  Namespace _core\_system\models;

  use _core\_system\models\config as cfg;
  use _core\_system\models\container as cont;
  use _core\_system\models\owner as owner;
  use _core\_system\helpers\sessionHandler as session;
  use _core\_system\helpers\mysqlConnector as sql;

    class smartyPlugin extends \Smarty_Resource_Custom{

    // PDO instance
    protected $db;
    // prepared fetch() statement
    protected $fetch;
    // prepared fetchTimestamp() statement
    protected $mtime;

    public function __construct() {
        try {
            $this->db = new \PDO("mysql:dbname=test;host=localhost", "root", "");
        } catch (PDOException $e) {
            throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
        }

        $this->fetch = $this->db->prepare('SELECT timestamp, source FROM templates WHERE name = :name');
        $this->mtime = $this->db->prepare('SELECT timestamp FROM templates WHERE name = :name');

    }

    /**
     * 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(array('name' => $name));
        $row = $this->fetch->fetch();
        $this->fetch->closeCursor();
        if ($row) {
            $source = $row['source'];
            $mtime = strtotime($row['timestamp']);
        } 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(array('name' => $name));
        $mtime = $this->mtime->fetchColumn();
        $this->mtime->closeCursor();
        return strtotime($mtime);
    }

    }


templateController.php:
Code:

  Namespace _core\_system\controllers;

  use _core\_system\models\config as cfg;
  use _core\_system\models\container as cont;
  use _core\_system\models\owner as owner;
  use _core\_system\helpers\sessionHandler as session;
  use _core\_system\helpers\mysqlConnector as sql;
  use _core\_system\models\smartyPlugin as sm;

    class templateController extends \Smarty{

     public function __construct(){

            parent::__construct();

            $this->registerResource('mysql', new sm);


     }


    }


index.php
Code:

  namespace _core\containers\shop\sites;

  use _core\_system\controllers\templateController as tpl;

    class index{

        public static function init(){

            $tpl = new tpl;

            $tpl->display("mysql:index.tpl");
        }


    }


Fehlermeldung:

Quote:

Fatal error: Uncaught exception 'SmartyException' with message 'Unable to load template mysql 'index.tpl'' in D:\localServer\wwwData\xxxxxxx\_core\_system\thirdParty\Smarty\libs\sysplugins\smarty_internal_templatebase.php:127 Stack trace: #0 D:\localServer\wwwData\xxxxxxx\_core\_system\thirdParty\Smarty\libs\sysplugins\smarty_internal_templatebase.php(374): Smarty_Internal_TemplateBase->fetch('mysql:index.tpl', NULL, NULL, NULL, true) #1 D:\localServer\wwwData\xxxxxxx\_core\containers\shop\sites\index.php(23): Smarty_Internal_TemplateBase->display('mysql:index.tpl') #2 D:\localServer\wwwData\xxxxxxx\_core\_system\controllers\containerController.class.php(80): _core\containers\shop\sites\index::init() #3 D:\localServer\wwwData\xxxxxxx\_core\_system\controllers\containerController.class.php(69): _core\_system\controllers\containerController::DefaultSite() #4 D:\localServer\wwwData\xxxxxxx\_core\containers\shop\controller.class.php(33): _core\_system\controllers\containerContro in D:\localServer\wwwData\xxxxxxx\_core\_system\thirdParty\Smarty\libs\sysplugins\smarty_internal_templatebase.php on line 127


Wirklich vielen Dank! Gruß und an alle einen guten Rutsch und viel Erfolg im neuen Jahr!
Back to top
View user's profile Send private message
Shorty88
Smarty n00b


Joined: 04 Jan 2013
Posts: 4

PostPosted: Fri Jan 04, 2013 1:34 pm    Post subject: Reply with quote

Kann da auf die Schnelle nichts entdecken. Werde nochmal genau rüberschauen u Rückmeldung geben.
Back to top
View user's profile Send private message
eazytrader
Smarty Rookie


Joined: 16 Jul 2010
Posts: 33

PostPosted: Thu Jan 10, 2013 5:50 pm    Post subject: Reply with quote

Danke für die Mühe.
Ich habe es nun hinbekommen und es klappt auch alles wunderbar.
Nur eine Frage bleibt noch offen, wie binde ich jetzt CSS Dateien per Datenbank ein?

Gruss und Danke
Back to top
View user's profile Send private message
TomTo
Smarty Rookie


Joined: 30 May 2009
Posts: 18

PostPosted: Thu Jan 10, 2013 7:21 pm    Post subject: Reply with quote

Es wäre es schön, wenn Du Deine Lösung auch anderen mitteilen würdest, eventuell hat ja jemand mal das gleiche Problem.

Du kannst die Stylesheet in die Datenbank legen und sie dann über ein PHP Script dem jeweiligen Template zur Verfügung stellen.
Normalerweise macht man sowas bei grossen Projekten, oder man möchte mehrere Template Styles den Usern zur Verfügung stellen, wie es zb. VBulletin auch macht.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Language: German 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