Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web programming - PHP Part III.

Web programming - PHP Part III.

University of Stavanger, DAT310

Krisztian Balog

March 09, 2016
Tweet

More Decks by Krisztian Balog

Other Decks in Programming

Transcript

  1. So far: mixing HTML and PHP <!DOCTYPE html>
 <html> […]

    <?php
 echo "First name: " . $first_name . "<br>"; ?>
 <input type="text" name="first_name" value="<?php echo $first_name;?>" /> 
 […]
  2. Templating <!DOCTYPE html>
 <html> […] First name: {$first_name} <br> 


    […] <input type="text" name="first_name" 
 value="{$first_name}" /> <!DOCTYPE html>
 <html> […] First name: John <br> 
 <input type="text" name="first_name" 
 value="John" /> 
 […] Template file Final HTML - Placeholders are substituted with actual values
  3. Smarty - Templating language - Clean separation of presentation from

    logic - Particularly important for projects where the roles of designers and developers are separated - Easier maintenance of large projects - HTML extended with Smarty variables and control statements in between {…}
  4. Smarty syntax & controls - {$variable} - single value -

    {foreach} - loop, e.g., for displaying arrays - {if} - {include} - including another smarty file - {literal} - JavaScrips or CSS content (that contains { or } chars) needs to be in between {literal}..{/literal}
  5. {foreach} 
 iterating values • apple • orange • banana

    PHP Smarty Output <ul> {foreach $fruits as $f} <li>{$f}</li> {/foreach} </ul> $fruits = array("apple", "orange", "banana"); $smarty->assign("fruits", $fruits);
  6. {foreach}
 iterating keys & values • [red] #ff0000 • [green]

    #00ff00 • [blue] #0000ff PHP Smarty Output $colors = array( "red" => "#ff0000", "green" => "#00ff00", "blue" => "#0000ff", ); $smarty->assign("colors", $colors); <ul> {foreach $colors as $key => $val} <li>[{$key}]: {$val}</li> {/foreach} </ul>
  7. {if} and {include} Smarty {if $page == "list"} {include file="list.tpl"}

    {elseif $page == "table"} {include file="table.tpl"} {else} Here are some choices: […] {/if}
  8. PHP methods 1. Create new Smarty object
 
 2. Set

    Smarty variables
 
 3. Display page $smarty = new Smarty(); $fruits = array("apple", "orange", "banana");
 $smarty->assign("fruits", $fruits); $smarty->display("sample.tpl");
  9. // edit these two lines according to your local settings


    define("SMARTY_DIR", "YOUR-WWW-ROOT/Smarty-3.1.28/libs/");
 define("PROJECT_DIR", "");
 
 // init Smarty
 require(SMARTY_DIR . "Smarty.class.php");
 $smarty = new Smarty();
 $smarty->setTemplateDir(PROJECT_DIR . "smarty/templates");
 $smarty->setCompileDir(PROJECT_DIR . "smarty/templates_c");
 $smarty->setCacheDir(PROJECT_DIR . "smarty/cache");
 $smarty->setConfigDir(PROJECT_DIR . "smarty/config");
 //$smarty->testInstall();
 
 // passing some data to Smarty
 $smarty->assign("name", "John");
 
 // ...
 
 // display HTML
 $smarty->display("sample.tpl"); Example examples/php/smarty/smarty.php
  10. - Download latest version from 
 https://github.com/smarty-php/smarty/releases - Unzip it

    under www root - Set up required folder structure under PHP project dir Smarty setup examples/php/smarty/README.md need to be writable by the web server
  11. Custom Smarty functions - {html_select_date} — creates date dropdowns -

    {html_radios} — creates a radio button group - {html_checkboxes} — creates a checkbox group - {html_table} — dumps an array of data into a table - …
  12. Setup - Use an FTP client, e.g., FileZilla - Server:

    badne5.ux.uis.no - User and pass in email from Theodor Ivesdal - www root is public_html - URL is http://www.ux.uis.no/~yourusername