| 148 |
lars |
1 |
Philosophy
|
|
|
2 |
=======
|
|
|
3 |
|
|
|
4 |
## What is Smarty?
|
|
|
5 |
|
|
|
6 |
Smarty is a template engine for PHP. More specifically, it facilitates a
|
|
|
7 |
manageable way to separate application logic and content from its
|
|
|
8 |
presentation. This is best described in a situation where the
|
|
|
9 |
application programmer and the template designer play different roles,
|
|
|
10 |
or in most cases are not the same person.
|
|
|
11 |
|
|
|
12 |
For example, let\'s say you are creating a web page that is displaying a
|
|
|
13 |
newspaper article.
|
|
|
14 |
|
|
|
15 |
- The article `$headline`, `$tagline`, `$author` and `$body` are
|
|
|
16 |
content elements, they contain no information about how they will be
|
|
|
17 |
presented. They are [passed](#api.assign) into Smarty by the
|
|
|
18 |
application.
|
|
|
19 |
|
|
|
20 |
- Then the template designer edits the templates and uses a
|
|
|
21 |
combination of HTML tags and [template tags](#language.basic.syntax)
|
|
|
22 |
to format the presentation of these
|
|
|
23 |
[variables](#language.syntax.variables) with elements such as
|
|
|
24 |
tables, div\'s, background colors, font sizes, style sheets, svg
|
|
|
25 |
etc.
|
|
|
26 |
|
|
|
27 |
- One day the programmer needs to change the way the article content
|
|
|
28 |
is retrieved, ie a change in application logic. This change does not
|
|
|
29 |
affect the template designer, the content will still arrive in the
|
|
|
30 |
template exactly the same.
|
|
|
31 |
|
|
|
32 |
- Likewise, if the template designer wants to completely redesign the
|
|
|
33 |
templates, this would require no change to the application logic.
|
|
|
34 |
|
|
|
35 |
- Therefore, the programmer can make changes to the application logic
|
|
|
36 |
without the need to restructure templates, and the template designer
|
|
|
37 |
can make changes to templates without breaking application logic.
|
|
|
38 |
|
|
|
39 |
## Goals
|
|
|
40 |
|
|
|
41 |
The Smarty design was largely driven by these goals:
|
|
|
42 |
- clean separation of presentation from application code
|
|
|
43 |
- PHP backend, Smarty template frontend
|
|
|
44 |
- complement PHP, not replace it
|
|
|
45 |
- fast development/deployment for programmers and designers
|
|
|
46 |
- quick and easy to maintain
|
|
|
47 |
- syntax easy to understand, no PHP knowledge necessary
|
|
|
48 |
- flexibility for custom development
|
|
|
49 |
- security: insulation from PHP
|
|
|
50 |
- free, open source
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
|
54 |
## Two camps of thought
|
|
|
55 |
|
|
|
56 |
When it comes to templating in PHP, there are basically two camps of
|
|
|
57 |
thought. The first camp exclaims that \"PHP is a template engine\". This
|
|
|
58 |
approach simply mixes PHP code with HTML. Although this approach is
|
|
|
59 |
fastest from a pure script-execution point of view, many would argue
|
|
|
60 |
that the PHP syntax is messy and complicated when mixed with tagged
|
|
|
61 |
markup such as HTML.
|
|
|
62 |
|
|
|
63 |
The second camp exclaims that presentation should be void of all
|
|
|
64 |
programming code, and instead use simple tags to indicate where
|
|
|
65 |
application content is revealed. This approach is common with other
|
|
|
66 |
template engines (even in other programming languages), and is also the
|
|
|
67 |
approach that Smarty takes. The idea is to keep the templates focused
|
|
|
68 |
squarely on presentation, void of application code, and with as little
|
|
|
69 |
overhead as possible.
|
|
|
70 |
|
|
|
71 |
## Why is separating PHP from templates important?
|
|
|
72 |
|
|
|
73 |
Two major benefits:
|
|
|
74 |
|
|
|
75 |
- SYNTAX: Templates typically consist of semantic markup such as HTML.
|
|
|
76 |
PHP syntax works well for application code, but quickly degenerates
|
|
|
77 |
when mixed with HTML. Smarty\'s simple {tag} syntax is designed
|
|
|
78 |
specifically to express presentation. Smarty focuses your templates
|
|
|
79 |
on presentation and less on \"code\". This lends to quicker template
|
|
|
80 |
deployment and easier maintenance. Smarty syntax requires no working
|
|
|
81 |
knowledge of PHP, and is intuitive for programmers and
|
|
|
82 |
non-programmers alike.
|
|
|
83 |
|
|
|
84 |
- INSULATION: When PHP is mixed with templates, there are no
|
|
|
85 |
restrictions on what type of logic can be injected into a template.
|
|
|
86 |
Smarty insulates the templates from PHP, creating a controlled
|
|
|
87 |
separation of presentation from business logic. Smarty also has
|
|
|
88 |
security features that can further enforce restrictions on
|
|
|
89 |
templates.
|
|
|
90 |
|
|
|
91 |
## Web designers and PHP
|
|
|
92 |
|
|
|
93 |
A common question: "Web designers have to learn a syntax anyway, why
|
|
|
94 |
not PHP?" Of course web designers can learn PHP, and they may already
|
|
|
95 |
be familiar with it. The issue isn't their ability to learn PHP, it is
|
|
|
96 |
about the consequences of mixing PHP with HTML. If designers use PHP, it
|
|
|
97 |
is too easy to add code into templates that doesn't belong there (you
|
|
|
98 |
just handed them a swiss-army knife when they just needed a knife.) You
|
|
|
99 |
can teach them the rules of application design, but this is probably
|
|
|
100 |
something they don't really need to learn (now they are developers!)
|
|
|
101 |
The PHP manual is also an overwhelming pile of information to sift
|
|
|
102 |
through. It is like handing the owner of a car the factory assembly
|
|
|
103 |
manual when all they need is the owners manual. Smarty gives web
|
|
|
104 |
designers exactly the tools they need, and gives developers fine-grained
|
|
|
105 |
control over those tools. The simplicity of the tag-based syntax is also
|
|
|
106 |
a huge welcome for designers, it helps them streamline the organization
|
|
|
107 |
and management of templates.
|
|
|
108 |
|