|
{if $edit_flag}
<input type=checkbox name=edit value="y"> edit <br>
{/if}
|
It is up to the application programmer to assign the $edit_flag, a simple and easy-to-understand variable in the template. This way the template is no longer relying on your underlying data structure. If the format of the session data structure ever changes, nothing needs to be adjusted in the template.
Now lets look at a few things you can do with Smarty. One thing it can do is custom functions. These are tags in the template that execute a certain task. Example:
{html_image file="masthead.gif"}
|
Here we have a function called "html_image". This function takes the image given in the "file" attribute and does all the work necessary to come up with the following HTML code:
<img src="masthead.gif" border="0" height="48" width="256">
|
The image function did the chore of figuring out the height and width and supplying the default border flag. Of course you could just use the static HTML tag in the template instead, but this demonstrates how a custom function can be utilized to simplify a very common task. The designer can focus on design and less on the technical stuff. Furthermore, if the designer decides to drop in a different size masthead image, the template does not need adjusting.
html_image is a function that comes with Smarty. You can also make your own custom functions. Here's another example of what one might look like:
{html_link type="article" id="abc123" text="Fire takes out Hotel"}
|
This is using a custom function called "html_link". It comes up with the following HTML code:
<a href="/display_article.php?id=abc123">Fire takes out Hotel</a>
|
What does this accomplish? For one, the designer does not need to be concerned with the format of a URL to an article. With hard-coded URLs, what happens if one day the programmer decides to clean things up, and changes the URL syntax from /display_article.php?id=abc123 to /ART/abc123 ? We would have to edit every template with an article URL. This is just another example of how a template function can make templates easier to maintain.
Now for a bit on programmers and templates. Earlier it was mentioned that the programmer has no care for what the templates do with the content. At a conceptual level this is true, but in the real world you are not going to expect the template designer to have to construct all the templates out of thin air. After all, the business logic does determine what content is assigned to the templates. So, the programmer will typically setup skeleton templates for the designer to start with. This usually contains the raw elements such as content variables and section loops, and maybe a few simple markup tags so they don't start with the content in a big mess. Here is an example of a skeleton template that loops through a list of articles and displays them in a table:
(阅读次数:)
共3页: 上一页 [1] 2 [3] 下一页
|