If you are tired of copy/pasting bits of PHP in your layouts by trial and error, here is your chance to learn what those bits of PHP mean. This talk explains the PHP code found in layouts and templates: what it does, how you can change it, and what to watch out for. Intended for those who know HTML but don’t know what all those dollar signs and arrows are doing. And if you are a code guru, it's your chance to learn about layouts.
We’ll start by briefly going over the basics of PHP, then move on to looking at the actual code in various layouts to illustrate what is being done. We’ll end by making changes and additions to the coding and seeing how that changes what you see on the sceen.
If you want to learn more about PHP, see the book "PHP and MySQL, 24-Hour Trainer".
1. PHP for HTML Gurus
Andrea Tarr
Tarr Consulting
J and Beyond 2012
2. Outline
• PHP Basics
• Explanation of
actual code in
templates &
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
layouts
• Changing code
and seeing it in
action
• Book give away
2
3. PHP Basics
• Get in and out of PHP with <?php ?>
• End each statement with a semi-colon
<?php
$first_name = 'Andrea';
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
$last_name = 'Tarr';
?>
• Whitespace is irrelevant
<?php $first_name='Andrea';$last_name='Tarr';?>
• Omit any final ?> at the end of a file 3
4. Comments
• Single line comments
// This is a single line comment
$language = 'PHP'; // End of line comment
// Multiple lines of single comments
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
// can be done by repeating the slashes
• Multiple line comments
/* With this type of comment you can
start a comment on one line and end
it on an other line */
4
5. Variables
• Used to store information that can change
• Start with a $
• Assign a value with =
• Use single ('') or double ("") quotes for text
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
• You need to assign a value before you can use the
variable
• Use echo to display a value
• Use a dot (.) to join items together
$first_name = 'Andrea';
$last_name = 'Tarr'; 5
echo $first_name . ' ' . $last_name;
6. $Variable Types
• String Text: Andrea
• Numbers: 42
• Logical: True/False, 1/0
• Array: An array is a list, either indexed or named
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
• [0]=>Sun, [1]=>Mon, [2]=>Tue, [3]=>Wed
• Name=>Joomla, Version=>2.5,
• You can nest arrays
• Object: We'll come back to Object shortly
6
7. Functions()
• Functions perform an action
• You recognize a function by the ()
• You can pass information to the function in the ()
$full_name = ' Andrea Tarr ';
echo trim($full_name);
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
• This results in Andrea Tarr
• You can pass a function to a function
echo strtoupper(trim($full_name);
• This results in ANDREA TARR 7
8. CONSTANTS
• Constants are assigned once and don't change
• No $
• Usually in ALL CAPS
define('SITENAME', 'My Site');
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
echo SITENAME;
8
9. Objects
• Think of an object as a named array plus functions
• Properties are variables
• Functions are also called methods
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
Examples:
• $item->title contains the title
• $item->introtext contains the intro text
• $params->get('show_modify_date')
9
10. Classes
• Classes are the blueprints that are used to create
objects
• You can use the classes directly without creating an
object
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
Examples:
• JText::_('COM_CONTENT_READ_MORE');
• JHtml::_('string.truncate', ($this->item->title),
$params->get('readmore_limit'))
10
11. Making Decisions: if/else
• Example 1
if ($sum > 10) :
// do something when greater than 10
else :
// do something when 10 or less
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
endif;
• Example 2
if ($errors) :
// do something when there are errors
endif;
11
• You can also nest if statements
13. One Line If Statement
• Normal way
if ($gender == 'M') :
echo 'Man';
else :
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
echo 'Woman';
endif;
• One line version
echo ($gender == 'M') ? 'Man' : 'Woman';
13
14. Alternative Syntax
• Alternative Syntax used when mixing with HTML
if ($sum > 10) :
// do something when greater than 10
else :
// do something when 10 or less
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
endif;
• Normal syntax with curly braces
if ($sum > 10) {
// do something when greater than 10
} else {
// do something when 10 or less 14
}
15. Looping
• While
• Repeats while a condition is true
• Do/While
• Does it at least once then repeats while true
• For
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
• Loops a given number of times
• Foreach
• Repeats the code for each element in an array or
object
• Jumping out early
• Continue
• Jump to the next iteration
• Break 15
• Jump out of the loop
16. Template index.php file
• Template Parameters
• Conditional Stylesheets
• Conditional Positions
• One Line If Statement
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
16
17. Template Parameters
$color = $this->params->get('templatecolor');
<link rel="stylesheet" href="<?php echo
$this->baseurl ?>/templates/<?php echo
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
$this->template; ?>/css/<?php echo
htmlspecialchars($color); ?>.css"
type="text/css" />
<link rel="stylesheet"
href="/localhost/jc/templates/beez_20/css/personal.c
ss" type="text/css" /> 17
18. Conditional Stylesheets
<?php if ($this->direction == 'rtl') : ?>
<link rel="stylesheet" href="<?php echo
$this->baseurl ?>/templates/<?php echo
$this->template; ?>/css/template_rtl.css"
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
type="text/css" />
<?php endif; ?>
18
19. Conditional Positions
<?php if ($this->countModules('position-12')): ?>
<div id="top">
<jdoc:include type="modules" name="position-12" />
</div>
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
<?php endif; ?>
19
20. One Line If Statement
$showRightColumn =
($this->countModules('position-3') OR
$this->countModules('position-6') OR
$this->countModules('position-8'));
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
<div id="<?php echo
$showRightColumn ? 'contentarea2' : 'contentarea';
?>">
If there are right modules: <div id="contentarea2">
If there aren't: <div id="contentarea"> 20
21. Module Layout
• Latest Articles
• mod_articles_latest/tmpl/default.php
Beyond 2012 • Andrea Tarr
PHP for HTML Gurus • J and
21
22. Latest Articles
PHP for HTML Gurus • J and
22
Beyond 2012 • Andrea Tarr
24. var_dump()
PHP for HTML Gurus • J and
24
Beyond 2012 • Andrea Tarr
25. Object $item
PHP for HTML Gurus • J and
25
Beyond 2012 • Andrea Tarr
26. Object $item
PHP for HTML Gurus • J and
26
Beyond 2012 • Andrea Tarr
27. Object $item – Page 2
PHP for HTML Gurus • J and
27
Beyond 2012 • Andrea Tarr
28. Object $item – Page 3
PHP for HTML Gurus • J and
28
Beyond 2012 • Andrea Tarr
29. Object $item – Page 4
PHP for HTML Gurus • J and
29
Beyond 2012 • Andrea Tarr
30. Object $item – Page 5
PHP for HTML Gurus • J and
30
Beyond 2012 • Andrea Tarr
31. Object $item – Page 6
PHP for HTML Gurus • J and
31
Beyond 2012 • Andrea Tarr
32. Add information
PHP for HTML Gurus • J and
32
Beyond 2012 • Andrea Tarr
33. List with intro text
PHP for HTML Gurus • J and
33
Beyond 2012 • Andrea Tarr
34. Questions?
PHP for HTML Gurus • J and
34
Beyond 2012 • Andrea Tarr
35. Book Give Away
PHP for HTML Gurus • J and
35
Beyond 2012 • Andrea Tarr
Notas do Editor
Introduction to meIntro to the course. This isn't meant to teach you how to write PHP from scratch. It's meant to make you more comfortable around the php you come across in templates and layouts.Who can copy/paste PHP statements in templates and layouts? Who can make minor changes to PHP? Who can write PHP? Anyone here who is new to both HTML and PHP?
I'm going to start by going through the basics of how PHP works. This is going to go fairly fast and don't expect to remember all of it. It will make more sense when we start looking at the actual code, but it helps if you understand a bit of the basics first.So once we finish that, we will look at (what exactly are the files?)Then we'll try changing something in the code (adding a missing field, like the intro to something that is only title?) and see it working in actiion
The nouns $color css files
functions are the verb
$this = JDocumentHTML
This is the layout file that outputs the list of latest articles. This is the file that you would copy to your template to override with your changes
$list is aAdd a var_dump to see what's there that we want to use