Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
Resources {#plugins.resources}
2
=========
3
 
4
Resource plugins are meant as a generic way of providing template
5
sources or PHP script components to Smarty. Some examples of resources:
6
databases, LDAP, shared memory, sockets, and so on.
7
 
8
Custom Resources may be put in a file `resource.foobarxyz.php` within
9
your [`$plugins_dir`](#variable.plugins.dir), or registered on runtime
10
with [`registerResource()`](#api.register.resource). In either case you
11
will be able to access that resource by prepending its name to the
12
template you\'re addressing: `foobarxyz:yourtemplate.tpl`.
13
 
14
If a Resource\'s templates should not be run through the Smarty
15
compiler, the Custom Resource may extend `Smarty_Resource_Uncompiled`.
16
The Resource Handler must then implement the function
17
`renderUncompiled(Smarty_Internal_Template $_template)`. `$_template` is
18
a reference to the current template and contains all assigned variables
19
which the implementor can access via
20
`$_template->smarty->getTemplateVars()`. These Resources simply echo
21
their rendered content to the output stream. The rendered output will be
22
output-cached if the Smarty instance was configured accordingly. See
23
`libs/sysplugins/smarty_internal_resource_php.php` for an example.
24
 
25
If the Resource\'s compiled templates should not be cached on disk, the
26
Custom Resource may extend `Smarty_Resource_Recompiled`. These Resources
27
are compiled every time they are accessed. This may be an expensive
28
overhead. See `libs/sysplugins/smarty_internal_resource_eval.php` for an
29
example.
30
 
31
 
32
    <?php
33
 
34
    /**
35
     * MySQL Resource
36
     *
37
     * Resource Implementation based on the Custom API to use
38
     * MySQL as the storage resource for Smarty's templates and configs.
39
     *
40
     * Table definition:
41
     * <pre>CREATE TABLE IF NOT EXISTS `templates` (
42
     *   `name` varchar(100) NOT NULL,
43
     *   `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
44
     *   `source` text,
45
     *   PRIMARY KEY (`name`)
46
     * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre>
47
     *
48
     * Demo data:
49
     * <pre>INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');</pre>
50
     *
51
     * @package Resource-examples
52
     * @author Rodney Rehm
53
     */
54
    class Smarty_Resource_Mysql extends Smarty_Resource_Custom {
55
        // PDO instance
56
        protected $db;
57
        // prepared fetch() statement
58
        protected $fetch;
59
        // prepared fetchTimestamp() statement
60
        protected $mtime;
61
 
62
        public function __construct() {
63
            try {
64
                $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty");
65
            } catch (PDOException $e) {
66
                throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
67
            }
68
            $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name');
69
            $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name');
70
        }
71
 
72
        /**
73
         * Fetch a template and its modification time from database
74
         *
75
         * @param string $name template name
76
         * @param string $source template source
77
         * @param integer $mtime template modification timestamp (epoch)
78
         * @return void
79
         */
80
        protected function fetch($name, &$source, &$mtime)
81
        {
82
            $this->fetch->execute(array('name' => $name));
83
            $row = $this->fetch->fetch();
84
            $this->fetch->closeCursor();
85
            if ($row) {
86
                $source = $row['source'];
87
                $mtime = strtotime($row['modified']);
88
            } else {
89
                $source = null;
90
                $mtime = null;
91
            }
92
        }
93
 
94
        /**
95
         * Fetch a template's modification time from database
96
         *
97
         * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source.
98
         * @param string $name template name
99
         * @return integer timestamp (epoch) the template was modified
100
         */
101
        protected function fetchTimestamp($name) {
102
            $this->mtime->execute(array('name' => $name));
103
            $mtime = $this->mtime->fetchColumn();
104
            $this->mtime->closeCursor();
105
            return strtotime($mtime);
106
        }
107
    }
108
 
109
 
110
    require_once 'libs/Smarty.class.php';
111
    $smarty = new Smarty();
112
    $smarty->registerResource('mysql', new Smarty_Resource_Mysql());
113
 
114
    // using resource from php script
115
    $smarty->display("mysql:index.tpl");
116
    ?>
117
 
118
 
119
 
120
And from within Smarty template:
121
 
122
 
123
    {include file='mysql:extras/navigation.tpl'}
124
 
125
 
126
 
127
See also [`registerResource()`](#api.register.resource),
128
[`unregisterResource()`](#api.unregister.resource).