| 148 |
lars |
1 |
Features
|
|
|
2 |
=======
|
|
|
3 |
|
|
|
4 |
Some of Smarty's features:
|
|
|
5 |
- It is extremely fast.
|
|
|
6 |
- It is efficient since the PHP parser does the dirty work.
|
|
|
7 |
- No template parsing overhead, only compiles once.
|
|
|
8 |
- It is smart about [recompiling](#variable.compile.check) only the
|
|
|
9 |
template files that have changed.
|
|
|
10 |
- You can easily create your own custom
|
|
|
11 |
[functions](#language.custom.functions) and [variable
|
|
|
12 |
modifiers](#language.modifiers), so the template language is
|
|
|
13 |
extremely extensible.
|
|
|
14 |
- Configurable template [{delimiter}](#variable.left.delimiter) tag
|
|
|
15 |
syntax, so you can use `{$foo}`, `{{$foo}}`, `<!--{$foo}-->`, etc.
|
|
|
16 |
- The [`{if}..{elseif}..{else}..{/if}`](#language.function.if)
|
|
|
17 |
constructs are passed to the PHP parser, so the `{if...}` expression
|
|
|
18 |
syntax can be as simple or as complex an evaluation as you like.
|
|
|
19 |
- Allows unlimited nesting of
|
|
|
20 |
[`sections`](#language.function.section), `if's` etc.
|
|
|
21 |
- Built-in [caching](#caching) support
|
|
|
22 |
- Arbitrary [template](#resources) sources
|
|
|
23 |
- [Template Inheritance](#advanced.features.template.inheritance) for
|
|
|
24 |
easy management of template content.
|
|
|
25 |
- [Plugin](#plugins) architecture
|
|
|
26 |
|
|
|
27 |
## Separation of presentation from application code
|
|
|
28 |
- This means templates can certainly contain logic under the condition
|
|
|
29 |
that it is for presentation only. Things such as
|
|
|
30 |
[including](./designers/language-builtin-functions/language-function-include.md) other templates,
|
|
|
31 |
[alternating](./designers/language-custom-functions/language-function-cycle.md) table row colors,
|
|
|
32 |
[upper-casing](./designers/language-modifiers/language-modifier-upper.md) a variable,
|
|
|
33 |
[looping](./designers/language-builtin-functions/language-function-foreach.md) over an array of data and
|
|
|
34 |
rendering it are examples of presentation logic.
|
|
|
35 |
- This does not mean however that Smarty forces a separation of
|
|
|
36 |
business and presentation logic. Smarty has no knowledge of which is
|
|
|
37 |
which, so placing business logic in the template is your own doing.
|
|
|
38 |
- Also, if you desire *no* logic in your templates you certainly can
|
|
|
39 |
do so by boiling the content down to text and variables only.
|
|
|
40 |
|
|
|
41 |
## How does it work?
|
|
|
42 |
|
|
|
43 |
Under the hood, Smarty "compiles" (basically copies and converts) the
|
|
|
44 |
templates into PHP scripts. This happens once when each template is
|
|
|
45 |
first invoked, and then the compiled versions are used from that point
|
|
|
46 |
forward. Smarty takes care of this for you, so the template designer
|
|
|
47 |
just edits the Smarty templates and never has to manage the compiled
|
|
|
48 |
versions. This approach keeps the templates easy to maintain, and yet
|
|
|
49 |
keeps execution times extremely fast since the compiled code is just
|
|
|
50 |
PHP. And of course, all PHP scripts take advantage of PHP op-code caches
|
|
|
51 |
such as APC.
|
|
|
52 |
|
|
|
53 |
## Template Inheritance
|
|
|
54 |
|
|
|
55 |
Template inheritance was introduced in Smarty 3. Before template
|
|
|
56 |
inheritance, we managed our templates in
|
|
|
57 |
pieces such as header and footer templates. This organization lends
|
|
|
58 |
itself to many problems that require some hoop-jumping, such as managing
|
|
|
59 |
content within the header/footer on a per-page basis. With template
|
|
|
60 |
inheritance, instead of including other templates we maintain our
|
|
|
61 |
templates as single pages. We can then manipulate blocks of content
|
|
|
62 |
within by inheriting them. This makes templates intuitive, efficient and
|
|
|
63 |
easy to manage. See
|
|
|
64 |
[Template Inheritance](./programmers/advanced-features/advanced-features-template-inheritance.md)
|
|
|
65 |
for more info.
|
|
|
66 |
|
|
|
67 |
## Why not use XML/XSLT syntax?
|
|
|
68 |
There are a couple of good reasons. First, Smarty can be used for more
|
|
|
69 |
than just XML/HTML based templates, such as generating emails,
|
|
|
70 |
javascript, CSV, and PDF documents. Second, XML/XSLT syntax is even more
|
|
|
71 |
verbose and fragile than PHP code! It is perfect for computers, but
|
|
|
72 |
horrible for humans. Smarty is about being easy to read, understand and
|
|
|
73 |
maintain.
|
|
|
74 |
|
|
|
75 |
## Template Security
|
|
|
76 |
Although Smarty insulates you from PHP, you still have the option to use
|
|
|
77 |
it in certain ways if you wish. Template security forces the restriction
|
|
|
78 |
of PHP (and select Smarty functions.) This is useful if you have third
|
|
|
79 |
parties editing templates, and you don't want to unleash the full power
|
|
|
80 |
of PHP or Smarty to them.
|
|
|
81 |
|
|
|
82 |
## Integration
|
|
|
83 |
Sometimes Smarty gets compared to Model-View-Controller (MVC)
|
|
|
84 |
frameworks. Smarty is not an MVC, it is just the presentation layer,
|
|
|
85 |
much like the View (V) part of an MVC. As a matter of fact, Smarty can
|
|
|
86 |
easily be integrated as the view layer of an MVC. Many of the more
|
|
|
87 |
popular ones have integration instructions for Smarty, or you may find
|
|
|
88 |
some help here in the forums and documentation.
|
|
|
89 |
|
|
|
90 |
## Other Template Engines
|
|
|
91 |
Smarty is not the only engine following the *"Separate Programming Code
|
|
|
92 |
from Presentation"* philosophy. For instance, Python has template
|
|
|
93 |
engines built around the same principles such as Django Templates and
|
|
|
94 |
CheetahTemplate. *Note: Languages such as Python do not mix with HTML
|
|
|
95 |
natively, which give them the advantage of proper programming code
|
|
|
96 |
separation from the outset. There are libraries available to mix Python
|
|
|
97 |
with HTML, but they are typically avoided.*
|
|
|
98 |
|
|
|
99 |
## What Smarty is Not
|
|
|
100 |
|
|
|
101 |
Smarty is not an application development framework. Smarty is not an
|
|
|
102 |
MVC. Smarty is not an alternative to Laravel, Symfony, CodeIgniter,
|
|
|
103 |
or any of the other application development frameworks for PHP.
|
|
|
104 |
|
|
|
105 |
Smarty is a template engine, and works as the (V)iew component of your
|
|
|
106 |
application. Smarty can easily be coupled to any of the engines listed
|
|
|
107 |
above as the view component. No different than any other software,
|
|
|
108 |
Smarty has a learning curve. Smarty does not guarantee good application
|
|
|
109 |
design or proper separation of presentation, this still needs to be
|
|
|
110 |
addressed by a competent developer and web designer.
|
|
|
111 |
|
|
|
112 |
## Is Smarty Right for Me?
|
|
|
113 |
|
|
|
114 |
Smarty is not meant to be a tool for every job. The important thing is
|
|
|
115 |
to identify if Smarty fits your needs. There are some important
|
|
|
116 |
questions to ask yourself:
|
|
|
117 |
|
|
|
118 |
### Template Syntax
|
|
|
119 |
Are you content with PHP tags mixed with HTML? Are your
|
|
|
120 |
web designers comfortable with PHP? Would your web designers prefer a
|
|
|
121 |
tag-based syntax designed for presentation? Some experience working with
|
|
|
122 |
both Smarty and PHP helps answer these questions.
|
|
|
123 |
|
|
|
124 |
### The Business Case
|
|
|
125 |
Is there a requirement to insulate the templates from
|
|
|
126 |
PHP? Do you have untrusted parties editing templates that you do not
|
|
|
127 |
wish to unleash the power of PHP to? Do you need to programmatically
|
|
|
128 |
control what is and is not available within the templates? Smarty
|
|
|
129 |
supplies these capabilities by design.
|
|
|
130 |
|
|
|
131 |
## Feature set
|
|
|
132 |
Does Smarty's features such as caching, template
|
|
|
133 |
inheritance and plugin architecture save development cycles writing code
|
|
|
134 |
that would be needed otherwise? Does the codebase or framework you plan
|
|
|
135 |
on using have the features you need for the presentation component?
|
|
|
136 |
|
|
|
137 |
## Sites using Smarty
|
|
|
138 |
Many well-known PHP projects make use of Smarty such as XOOPS CMS, CMS Made Simple, Tiki
|
|
|
139 |
CMS/Groupware and X-Cart to name a few.
|
|
|
140 |
|
|
|
141 |
## Summary
|
|
|
142 |
Whether you are using Smarty for a small website or massive enterprise
|
|
|
143 |
solution, it can accommodate your needs. There are numerous features
|
|
|
144 |
that make Smarty a great choice:
|
|
|
145 |
|
|
|
146 |
- separation of PHP from HTML/CSS just makes sense
|
|
|
147 |
- readability for organization and management
|
|
|
148 |
- security for 3rd party template access
|
|
|
149 |
- feature completeness, and easily extendable to your own needs
|
|
|
150 |
- massive user base, Smarty is here to stay
|
|
|
151 |
- LGPL license for commercial use
|
|
|
152 |
- 100% free to use, open source project
|