Slide 1

Slide 1 text

Get Smart[y] An Interactive Presentation on the Smarty PHP Template Engine [email protected]

Slide 2

Slide 2 text

PHP Development Server Instructions • SSH to 192.168.69.8 • Login: smarty • Password: sm4rty • mkdir a folder for you to use • http://192.168.69.8/~smarty/folder/ • Edit files with nano and vim

Slide 3

Slide 3 text

Smarty is... • A presentation framework for PHP • Free and OSS • Very quick to learn and implement • Based on plug-ins -- make your own... • Going to make your life easier! • The only thing I felt like talking about...

Slide 4

Slide 4 text

So why should I use it? • The big rallying cry is “Let the programmers program, and the designers design” • Easily create templates (“themes”) • Designers can’t really ‘break’ your main programming code -- only manipulate the data you have given them. • It has a lot of functions built-in that you will want to program -- why waste the time? • Caching will help speed up that high traffic site

Slide 5

Slide 5 text

Sure, that sounds great... WHO USES IT? • Software http://www.drupal.org/ (Drupal CMS) http://www.xoops.org/ (XOOPS CMS) http://www.bblog.com/ (PHP Blogging) http://www.psychostats.com/ (Game Stats) http://tikiwiki.org/ (Popular Wiki) • Web sites Anyone using the above software (http://kerneltrap.org) http://www.enotes.com (Literature Notes) http://www.foundyouonline.com (Social Networking)

Slide 6

Slide 6 text

What we are covering in Smarty • Syntax • Variables • Templates • Variable modifiers • Functions • Creating a plugin • Caching

Slide 7

Slide 7 text

Smarty Syntax • Comments: {* Look like this *} • Variables: {$var} or {$array[0]} or {$User.Email} • Includes: {include file=template.tpl} • If-Then-Else: {if $yes} YES {else} NO {/if} • Literals: {literal}{/literal} in case you want to display code and not have it be evaluated

Slide 8

Slide 8 text

Folder Tree • Basically three main directories needed -templates/ (Templates) -templates_c/ (Templates cache) -configs/ (Template configuration files) • Configuration files are not mandatory in the context of templates, they are merely for you do define static values

Slide 9

Slide 9 text

Variables in Smarty • Your variables are the link to the PHP world. Variables can hold single values in Smarty or full arrays. • You are also able to create variables within Smarty if you need something temporary. {assign var=”hotel” value=”Marriot”}

Slide 10

Slide 10 text

Designating variable arrays assign('s_hotels', $p_hotels); //Call our template $smarty->display('example-one.tpl'); ?>

Slide 11

Slide 11 text

Reserved Smarty Variables • $smarty.* is a reserved variable tree $smarty.cookies.hotel = “hotel” Cookie $smarty.request.hotel = Get index.php?hotel= value $smarty.post.hotel = Get value of POST variable hotel $smarty.session.id = Retrieve PHP session ID $smarty.section.hotel.total = Total of items in array • Here is our first demo of smarty... http://127.0.0.1/example-one.php

Slide 12

Slide 12 text

Using {section} & array items • Call each array valuable the “boring way” Array value 0: {$s_hotels[0]} Array value 1: {$s_hotels[1]} • To loop through an array, you can use {section}{/section} {section name=hotel loop=$s_hotels} {$s_hotels[hotel]} {/section} • Other interesting uses of {section} are... {$smarty.section.hotels.total} {$smarty.section.hotels.rownum} {section} blah {sectionelse} No hotels! {/section} {$smarty.section.hotels.index}

Slide 13

Slide 13 text

Configuration Files • Configuration files will allow you to define variables for ease • Example from configs/hotels.conf: 1. [Hotels] 2. expensive = “very much so” • This will now allow you to call these variables when you include the config {config_load file=”hotels.conf” section=Hotels} Expensive?: {#expensive#} Expensive?: {$smarty.config.expensive} Expensive?: “very much so” (same result for both)

Slide 14

Slide 14 text

Mid-Presentation Security Note • Smarty does allow the embedding of PHP code within the template if the security features are turned off. You will, of course, want to try and never implement it and shouldn’t need to (that I can forsee).

Slide 15

Slide 15 text

Smarty Templates • .tpl is the standard file extension for templates in Smarty • The data assigned to your instance of Smarty is now able to be used constructively within the template • The template has built-in variable modifiers Truncate text Indent text Strip tags Word wrap String replacement Capitalize Date formatting Count characters

Slide 16

Slide 16 text

Variable Modifiers, EX. 1 • Truncate {$blog_entry|truncate:210} -This will truncate at 210th character but also end at the end of a word (so as to not cut off anythi {$blog_entry|truncate:10:”...”} -This will cause it to end with an ellipsis so you can entice your readers about your trip to Ohio... -If you add :true to the end of the modifier statement, you will truncate at the exact character.

Slide 17

Slide 17 text

Variable Modifiers, EX. 2 • strip_tags {$blog_content|strip_tags:false} -This will take your given content, remove any HTML tags and replace the stripped tags without including a space Before: Hi NOTACON After: Hi NOTACON -Smarty essentially removes anything with < >

Slide 18

Slide 18 text

Variable Modifiers, EX. 3 • wordwrap {$blog_content|wordwrap:10:"\n"} -This will wrap on the word nearest to the 10th character. It will wrap using a new-line. -If you add :true to the end of the statement, it will wrap at the exact character like our truncate variable modifier

Slide 19

Slide 19 text

Notes on Variable Modifiers • You can combine any number of them using pipes • You can combine them with Smarty functions also, ex (from Smarty Docs): {html_table loop=$myvar|truncate:40:"..."}

Slide 20

Slide 20 text

Smarty Functions • html_table {html_table loop=$data cols=4 table_attr='border="0"'} -Easily create tables with every attribute you want, consult documentation to see all the aspects. -In this example we loop through an array of data, and we loop after the fourth column to a new row. Obviously this assigns the border to a width of 0 -This is a lot cleaner then making your own loops to deal with tablizing (sure it’s a word) data

Slide 21

Slide 21 text

More on Smarty Functions • html_options -Imagine we have two arrays. One array has values [1-3] and the other has the hotel names we defined earlier. {html_options values=$hotels_ids output=$hotels} -Our output on the web site [one row] looks like.. Marriot

Slide 22

Slide 22 text

Quick Functions • counter {counter start=0 skip=2} • mailto {mailto address="[email protected]" subject="NOTACON"} • fetch {fetch file="http://www.goodstuff.com/news.txt"}

Slide 23

Slide 23 text

The really useful function • Cycle -Ever want to alternate between colors for rows of a table... easily? {section name=rows loop=$your_data} {/section} {$your_data[rows]}

Slide 24

Slide 24 text

Random “Advanced” Stuff • Pre-Filters: Remove those unwanted comments from templates before they are compiled. • Post-Filters: Add those unwanted (but cooler) comments to templates following compilation. • Output-Filters: Replace your user-friendly tags with HTML that actually does something.

Slide 25

Slide 25 text

Creating a Q&D Plugin ", $source); $source = str_replace("[/code]", "", $source); return $source; } ?> Example of a Smarty Plugin: How to call plugin... $smarty->load_filter('output','tags'); $smarty->display('your_page.tpl');

Slide 26

Slide 26 text

Template Caching • You are able to cache the entire page or just a single section • Example: You query your news database each time someone visits the site. Instead of this, cache the news page and show that static page to the people. Set the refresh for whatever you wish • Basic aspects to caching are - $smarty->caching = {0,1,2} - $smarty->cache_lifetime = # of seconds; - $smarty->clear_cache('file.tpl');

Slide 27

Slide 27 text

Obligatory Thank You Slide... • Apple for making my Powerbook and Keynote 2 • http://smarty.php.net for actually posting that I was speaking here and creating Smarty • Joe Stump (www.joestump.net) for enlightening me • My fellow Core staff (and our brave volunteers) • Kathleen Sulewski, Kim Dubicki, Jeff England, Matt Fanto, Jeff Peckham, Ken West, Citadel, Corey Houston, James Turner and anyone else who sat through this.

Slide 28

Slide 28 text

References • http://smarty.php.net/manual/en/ • http://zend.com/zend/tut/tutorial-stump.php • Random Googling...