Download No Frills Magento Layout PDF

TitleNo Frills Magento Layout
File Size1.4 MB
Total Pages164
Document Text Contents
Page 1

No Frills Magento Layout

Alan Storm

April 2011

Page 2

Contents

0 No Frills Magento Layout: Introduction 5
0.1 Who this Book is For . . . . . . . . . . . . . . . . . . . . . . . . 6
0.2 No Frills . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
0.3 Installing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . 7
0.4 Parting Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
0.5 Bugs in the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
0.6 About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . 9
0.7 Let’s Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1 Building Layouts Programmatically 10
1.1 Template Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.2 Template Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3 Back to our Template . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4 Nesting Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5 Advanced Block Functionality . . . . . . . . . . . . . . . . . . . . 18
1.6 Block Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.7 Enter the Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

1.7.1 What’s a Singleton!? . . . . . . . . . . . . . . . . . . . . . 23
1.8 Back to the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.9 Who’s the Leader . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.10 Method Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.11 A Full Page Layout . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.12 Initializing the Layout and Setting Content . . . . . . . . . . . . 27
1.13 Insert vs. Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.14 Getting a Reference and Text List . . . . . . . . . . . . . . . . . 28
1.15 A Recap and a Dilema . . . . . . . . . . . . . . . . . . . . . . . . 29

2 XML Page Layout Files 31
2.1 Hello World in XML . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.2 An Interesting use of the Word Simple . . . . . . . . . . . . . . . 33
2.3 Adding the XML, Generating the Blocks . . . . . . . . . . . . . . 34
2.4 Getting a Little More Complex . . . . . . . . . . . . . . . . . . . 35
2.5 Action Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

1

Page 82

CHAPTER 5. ADVANCED LAYOUT FEATURES

Block aliases are a feature you may never personally use, but recent versions
of Magento have made heavy use of them to overcome some earlier design
decisions. You’ll want to make sure you’re aware of the difference between an
alias and name, even if you never use an alias in your own updates.

5.9 Skipping a Child

We’ve already covered the getChildHtml method in a previous chapter. However,
it has a cousin method named getChildChildHtml. This method is also defined on
the Mage Core Block Abstract class

public function getChildChildHtml($name , $childName = ’’, $useCache = true ,

$sorted = false)

{

if (empty($name )) {

return ’’;

}

$child = $this ->getChild($name);

if (! $child) {

return ’’;

}

return $child ->getChildHtml($childName , $useCache , $sorted );

}

You use this method from a phtml template, and it might look something like

<?php echo $this ->getChildChildHtml(’my_child ’, ’foo’); ?>

The getChildHtmlmethod will render out the specified child. The getChildChildHtml
method obtains a reference to the first child block (my child above), and then
calls getChildHtml on it.

This method is most useful when you’re editing a phtml template and don’t want
to restructure your blocks. I personally haven’t found much use for it, but you
will see it used in the wild and in the core, so it’s worth knowing about. The most
typical use is to render a core/template block as though it was a core/text list
block.

Visit http://www.pulsestorm.net/nofrills-layout-chapter-five to join the discus-
sion online.

Prepared for Compaa Peruana de E-commerce S.A.; Copyright c
2011 Pulse
Storm LLC

81

Page 83

Chapter 6

CMS Pages

This chapter covers the CMS Page feature. For day-to-day Magento work most
of the knowledge here is unnecessary. However, if you ever need to debug a
Magento CMS page render, or are curious how CMS pages interact with the
layout, this is the chapter for you. We’ll also being laying the groundwork for
our final chapter on Widgets.

Back in 1996, if you wanted to put a piece of content online, you just uploaded
an HTML file. If you were really savvy, you’d upload an HTML include file
that contained your content, and the HTML page itself would use server side
includes.

It’s weird, that in 2011, if you asked a developer how to add some content to
a site or a web application, their process would be almost exactly the same.
Instead of adding an HTML file, they’d add a controller and a template, and
then put the HTML content in the template.

However, for non-developers, managing content on a website has gone com-
pletely GUI. Systems like Drupal, Concrete5, and Joomla rule the roost. Users
expect to manage their sites via a user interface, and not via code or adding
files. Magento’s often overlooked CMS features allows users the control they
want. Don’t worry though, there’s plenty in the CMS for a developer to sink
their teeth into, particularly a developer who knows the layout system inside
out.

6.1 Creating a Page

The CMS starts with a CMS Page entity. If you browse to

CMS -> Pages

82

Page 163

APPENDIX J. MAGENTO CONNECT

J.2 Installing Extensions: The GUI Way

There’s a GUI admin for Magento Connect. You can reach it from the Admin
Console by navigating to

System -> Magento Connect -> Magento Connect Manager

You’ll need to reauthorize your session as the admin user, (or any user with
Magento Connect ACL rights). The code that bootstraps the Magento Connect
Manager is separate from the source code of your Magento system proper.

Installing extensions that have been uploaded to Magento Inc’s central server is
as easy as entering the extension key into the installation field.

If you’ve downloaded a .tgz package file from the internet, Magento 1.5 also
offers a handy upload form, allows you to directly upload an extension

J.3 Installing Extensions: The Command Line
Way

Both the 1.4x and 1.5x branches of Magento offer the ability to install extension
from the command line. However, the tools used for each version differ slightly.

J.3.1 Magento Connect CLI install for Magento 1.42

In the root folder of Magento 1.4.2 there’s a shell script named pear. This shell
script in not the standard PEAR installer. It’s a customized installer you may
use to install Magento Connect extensions. To use it, you’ll need to tell your
operating system its allowed to execute it as a program

chmod +x pear

After that, you’ll need to run

./pear mage -setup

After setting a number of configuration variables and initializing two channels

connect.magentocommerce.com/core

connect.magentocommerce.com/community

the script will exit. You’re now ready to install and uninstall packages using
the command line installer

./pear install No_Frills_Magento_Layout_1_start -1.0.0. tgz

./pear uninstall \

channel:// connect . magentocommerce . com / community / No_Frills_Magento_Layout_1_sta ...

Prepared for Compaa Peruana de E-commerce S.A.; Copyright c
2011 Pulse
Storm LLC

162

Page 164

APPENDIX J. MAGENTO CONNECT

J.3.2 Magento Connect CLI install for Magento 1.5+

Magento 1.5 removed the pear installer, and introduced a new command line
script (mage) that offers a similar function. Again, you’ll need to give it exe-
cutable permissions

chmod +x mage

and then initialize it with

./mage mage -setup

After the setup script finishes running, you’ll be able to install extensions from
a file. NOTE: The command has changed to install-file, and the arguments
to uninstall have changed as well

./mage install -file No_Frills_Magento_Layout_3_start -1.0.0. tgz

./mage uninstall community No_Frills_Magento_Layout_3_start

Visit http://www.pulsestorm.net/nofrills-layout-appendix-j to join the discus-
sion online.

Prepared for Compaa Peruana de E-commerce S.A.; Copyright c
2011 Pulse
Storm LLC

163

Similer Documents