11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 19 • TEMPLATING WITH SMARTYwww.it-ebooks.info• The designer can work almost independently of the application developer becausethe presentational and logical aspects of the application are not inextricablyintertwined. Furthermore, because the presentational logic used by mosttemplating engines is typically more simplistic than the syntax of whateverprogramming language is being used for the application, the designer is notrequired to undergo a crash course in that language in order to perform his job.But how exactly does a templating engine accomplish this separation? Interestingly, mostimplementations use a custom language syntax for carrying out various tasks pertinent to the interface.This presentational language is embedded in a series of templates, each of which contains thepresentational aspects of the application and would be used to format and output the data provided bythe application’s logical component. A well-defined delimiter signals the location in which the provideddata and presentational logic is to be placed within the template. A generalized example of such atemplate is offered in Listing 19-1. This example is based on the templating engine Smarty’s syntax.However, all popular templating engines follow a similar structure, so if you’ve already chosen anothersolution, chances are you’ll still find this chapter useful.Listing 19-1. A Typical Template (index.tpl){$pagetitle}{if $name eq "Kirk"}Welcome back Captain!{else}Swab the decks, mate!{/if}There are some important items to note in this example. First, the delimiters, denoted by curlybrackets ({}), serve as a signal to the template engine that the data found between the delimiters shouldbe examined and some action potentially taken. Most commonly, this action involves inserting aparticular variable value. For example, the $pagetitle variable found within the HTML title tags denotesthe location where this value, passed in from the logical component, should be placed. Farther down thepage, the delimiters are again used to denote the start and conclusion of an if conditional to be parsedby the engine. If the $name variable is set to “Kirk”, a special message will appear; otherwise, a defaultmessage will be rendered.Because most templating engine solutions, Smarty included, offer capabilities that go far beyondthe simple insertion of variable values, a templating engine’s framework must be able to perform anumber of tasks that are otherwise ultimately hidden from both the designer and the developer. Notsurprisingly, this is best accomplished via object-oriented programming, in which such tasks can beencapsulated. (See Chapters 6 and 7 for an introduction to <strong>PHP</strong>’s object-oriented capabilities.) Listing19-2 provides an example of how Smarty is used in conjunction with the logical layer to prepare andrender the index.tpl template shown in Listing 19-1. For the moment, don‘t worry about where thisSmarty class resides; this is covered soon enough. Instead, pay particular attention to the fact that thelayers are completely separated, and try to understand how this is accomplished in the example.388

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!