Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
Custom Template Resources {#resources.custom}
2
=========================
3
 
4
You can retrieve templates using whatever possible source you can access
5
with PHP: databases, sockets, files, etc. You do this by writing
6
resource plugin functions and registering them with Smarty.
7
 
8
See [resource plugins](#plugins.resources) section for more information
9
on the functions you are supposed to provide.
10
 
11
> **Note**
12
>
13
> Note that you cannot override the built-in `file:` resource, but you
14
> can provide a resource that fetches templates from the file system in
15
> some other way by registering under another resource name.
16
 
17
 
18
    <?php
19
 
20
    /**
21
    * MySQL Resource
22
    *
23
    * Resource Implementation based on the Custom API to use
24
    * MySQL as the storage resource for Smarty's templates and configs.
25
    *
26
    * Table definition:
27
    * <pre>CREATE TABLE IF NOT EXISTS `templates` (
28
    *   `name` varchar(100) NOT NULL,
29
    *   `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
30
    *   `source` text,
31
    *   PRIMARY KEY (`name`)
32
    * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre>
33
    *
34
    * Demo data:
35
    * <pre>INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');</pre>
36
    *
37
    * @package Resource-examples
38
    * @author Rodney Rehm
39
    */
40
    class Smarty_Resource_Mysql extends Smarty_Resource_Custom {
41
     // PDO instance
42
     protected $db;
43
     // prepared fetch() statement
44
     protected $fetch;
45
     // prepared fetchTimestamp() statement
46
     protected $mtime;
47
 
48
     public function __construct() {
49
         try {
50
             $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty");
51
         } catch (PDOException $e) {
52
             throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
53
         }
54
         $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name');
55
         $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name');
56
     }
57
 
58
     /**
59
      * Fetch a template and its modification time from database
60
      *
61
      * @param string $name template name
62
      * @param string $source template source
63
      * @param integer $mtime template modification timestamp (epoch)
64
      * @return void
65
      */
66
     protected function fetch($name, &$source, &$mtime)
67
     {
68
         $this->fetch->execute(array('name' => $name));
69
         $row = $this->fetch->fetch();
70
         $this->fetch->closeCursor();
71
         if ($row) {
72
             $source = $row['source'];
73
             $mtime = strtotime($row['modified']);
74
         } else {
75
             $source = null;
76
             $mtime = null;
77
         }
78
     }
79
 
80
     /**
81
      * Fetch a template's modification time from database
82
      *
83
      * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source.
84
      * @param string $name template name
85
      * @return integer timestamp (epoch) the template was modified
86
      */
87
     protected function fetchTimestamp($name) {
88
         $this->mtime->execute(array('name' => $name));
89
         $mtime = $this->mtime->fetchColumn();
90
         $this->mtime->closeCursor();
91
         return strtotime($mtime);
92
     }
93
    }
94
 
95
 
96
    require_once 'libs/Smarty.class.php';
97
    $smarty = new Smarty();
98
    $smarty->registerResource('mysql', new Smarty_Resource_Mysql());
99
 
100
    // using resource from php script
101
    $smarty->display("mysql:index.tpl");
102
    ?>
103
 
104
 
105
 
106
And from within Smarty template:
107
 
108
 
109
    {include file='mysql:extras/navigation.tpl'}
110
 
111