Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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