View part for a PHP application - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

View part for a PHP application

Description:

The last week's improvement to our PHP application was on the model/database part. The View part still lacked an architectural solution to give it structure ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 14
Provided by: jyrk9
Category:
Tags: php | application | lacked | part | view

less

Transcript and Presenter's Notes

Title: View part for a PHP application


1
View part for a PHP application
  • The last weeks improvement to our PHP
    application was on the model/database part.
  • The View part still lacked an architectural
    solution to give it structure
  • How to try to separate PHP and HTML?
  • In this course, we use a template library called
    Smarty.
  • It is a php-based library, which offers the
    possibility to model the output pages using what
    they call templates, which are in a sense
    formatting templates.
  • The templates use variables. The program using
    the template instantiates the variables and
    includes the template in the output.

2
Smarty
  • http//smarty.php.net
  • Documentation
  • Downloads
  • Installation instructions
  • Example application
  • The example application has been used as a model
    for implementing an example for the course.
  • The example this far is meant for the
    associations public pages.
  • Next week we add the maintenance facility.

3
Smarty - principles
  • Presentation is separated into template files.
  • Template files may be arranged into a
    hierarchical manner
  • They may include other template files.
  • The application computes all that is required and
    then passes the variables to be displayed to
    smarty
  • Smarty includes facilities for various things,
    e.g.
  • iteration (like sections)
  • formatting (substrings, capitalisation, known
    date and time formats, etc)
  • The templates may contain html (and, in fact, may
    only be html, if e.g. for some template there are
    no variables or such).
  • The examples will clarify this.

4
Smarty application directories
  • Main file (index.php) just calls php programs in
    library directories.
  • Other things are put outside of the directories
    from where the web server reads the files (could
    be chosen differently)
  • /lib/php/Smarty/ - this is the Smarty library
    installation
  • /smarty/association/libs/ - application php
    libraries/functions
  • /smarty/association/templates/ Smarty
    templates
  • /smarty/association/configs/ - configuration
    files
  • /smarty/association/cache/ - internal use
  • /smarty/association/templates_c/ - internal use
    (compiled templates)

5
The application html library
  • index.php minimal, initiates Smarty and calls
    its services
  • association.css the style file
  • images subdirectory that contains the image of
    the Finnish flag and the image of the UK flag
  • Javascript can be positioned in many ways, e.g.
  • In the Smarty templates to be included in the
    html files that are generated
  • As separate files, for instance in a subdirectory
    of the application directory

6
The Smarty installation
  • The installation just means that the Smarty
    software needs to be downloaded and put in a
    place, where the php programs access it.
  • For security, it is a good principle to put all
    that can be put in a place, from where the web
    server does not serve pages.
  • In our case, this means installation outside of
    the public_html subdirectory
  • I have installed my Smarty in /lib/php/Smarty
  • You will need to do the same for the courseworks.
  • Therefore, it is advisable to do the same already
    now.

7
/smarty/assocation/libs/
  • The libs subdirectory contains PHP code necessary
    for the application.
  • setup.php contains code for setting up the
    application in fact it just takes connection to
    the database and initializes the paths for other
    smarty subdirectories than the libs (which has
    already been found, as the setup.php is executed)
  • sql.lib.php contains the code for database
    operations. This is more or less the same thing
    as the DBlayer.php last week.
  • association.lib.php contains the application php
    functionalities. Notice that I have made a
    separate file of the maintenance functions this
    is not needed this week.

8
association.lib.php
  • Initiates the application class, which has a
    database object and a Smarty object.
  • Initiation also reads the configuration file,
    another slide on this.
  • In the main() function it chooses the action to
    be executed plus its parameters, and calls the
    respective functions.
  • As we only display data, a typical action
  • Accesses the database.
  • Assigns the results (an array) to a Smarty
    variable.
  • Calls the Smarty template to generate the output
    page.

9
Configuration files
  • These are like the Java property files.
  • The lines contain keywords and the respective
    strings.
  • When the configuration files are loaded, a
    variable can be specified to load just parts of
    the document.
  • This can be used e.g. to load texts for one
    language in an internationalised application. See
    the example
  • Respectively, the configuration file has a
    general section, from which everything is read,
    and keyword-separated sections starting
    withkeyword
  • Also comments can be used
  • See the example file

10
Template files
  • The whole templating facility is too large to be
    covered in this lecture.
  • We will focus on the key ideas, which are also
    exemplified in our example application
    implementation.
  • On the most basic level, the template file can be
    just a html file, which is included in the output
    as such.
  • The pages can be constructed in a hierarchical
    manner, using include statements, which, like
    other Smarty commands, are in between and
  • E.g. main.tpl contains three include statements
    to include a header, the main content part
    (details on this later) and a footer.

11
Variables
  • The variables can be used e.g. for conditionality
    and for output.
  • Examples
  • header.tpl chooses by language how it forms the
    menu, ie. which of the left side menu items
    contain a link.
  • header.tpl also outputs the variables, e.g. when
    forming the links it outputs the current action
    action
  • The configuration file contents can be accessed
    with a different notation. The basic notation is
    used e.g. to output the menu item events, etc,
    e.g. events
  • Another form, used in main.tpl (the other fails
    here, might have been due to concatenation)
    smarty.config.htmlDir

12
Conditionality and iteration
  • The if statements in header.tpl give an idea on
    how conditionality can be managed in an easy
    case.
  • Iteration is primarily needed to output sets of
    items.
  • These are typically arranged as arrays.
  • events.tpl gives an idea on how this can be
    managed.
  • The section statement is given an index name
    and it loops over a structure, in this case the
    array of events found from the database.
  • It is possible to test for the first and last
    item, and there is also an else for the case that
    the structure is empty.
  • Notice that the DBs getAll function retrieves an
    array in format suitable for Smartys section
    structure.

13
Output formatting
  • There are various modifiers, with which the
    output variables can be formatted as is needed.
  • As example, see how the seconds are removed from
    the SQL time datatype times in events.tpl
  • There particular formatters for things like
    dates, see an example of this as well from
    events.tpl
Write a Comment
User Comments (0)
About PowerShow.com