• No results found

Layout Files

In document No Frills Magento Layout. Alan Storm (Page 149-154)

Layout files follow the same cascading loading rules. First the current custom

design is checked, then the default theme, and the default theme in the base

design package. Layout files were included in this grand design of the base

theme system, as many developers ignore, or are unaware of,

local.xml

for layout

updates.

This led many system owners to edit the default Package Layout

files to achieve their final design goals, resulting in the same upgrade problems

mentioned above.

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

sion online.

The Hows and Whys of

Clearing Magento’s Cache

In layman’s terms, caching in computer science/software engineering refers to

the practice of doing something that’s resource intensive once, storing the results

somewhere else, and then the next time someone wants it you hand them the

stored result. The most common example of this for web developers is browser

caching. Asset files (images, CSS, Javascript) will be downloaded once, and then

stored locally for a period of time. This results in better network performance,

and hair pulling by web developers at 2am wondering why their CSS files aren’t

being updated.

Magento, like most modern web frameworks, heavily utilizes a caching system

to improve performance. For example, certain configuration files are loaded

once from disk once, combined, and then the combined config is stored on disk

for later use. This includes the Layout XML files. This means if you have

caching turned on (CE ships with caching on out of the box), you’ll need to

clear your cache after making any changes to the layout files. Otherwise, the

old, cached version of the Package Layout will be loaded and Magento won’t see

your changes, and you’ll be left wondering why your new block isn’t showing

up.

You can control cache settings by navigating to

S y s t e m - > C a c h e M a n a g m e n t

in the Admin Console, (seeFigure F.1)

From this page you can clear out the cache, or turn it off entirely.

Occasionally, a cached configuration may prevent you from getting to the Ma-

gento Admin Console. For example, consider an event observer with an invalid

class name. If this happens, you’ll want to look in the

var/cache

folder

Figure F.1

ls - l var / c a c h e t o t a l 0 d r w x r w x r w x 10 _ w w w s t a f f 340 Mar 15 1 6 : 1 0 mage - -0 d r w x r w x r w x 10 _ w w w s t a f f 340 Mar 15 1 6 : 1 0 mage - -1 d r w x r w x r w x 6 _ w w w s t a f f 204 Mar 15 1 6 : 0 2 mage - -2 d r w x r w x r w x 8 _ w w w s t a f f 272 Mar 15 1 6 : 0 2 mage - -3 d r w x r w x r w x 8 _ w w w s t a f f 272 Mar 15 1 6 : 1 0 mage - -4 d r w x r w x r w x 12 _ w w w s t a f f 408 Mar 15 1 6 : 0 2 mage - -5 d r w x r w x r w x 10 _ w w w s t a f f 340 Mar 15 1 6 : 0 2 mage - -6 d r w x r w x r w x 8 _ w w w s t a f f 272 Mar 15 1 6 : 0 2 mage - -7 d r w x r w x r w x 12 _ w w w s t a f f 408 Mar 15 1 6 : 0 2 mage - -8 d r w x r w x r w x 10 _ w w w s t a f f 340 Mar 15 1 6 : 0 2 mage - -9 d r w x r w x r w x 6 _ w w w s t a f f 204 Mar 15 1 6 : 0 2 mage - - a d r w x r w x r w x 10 _ w w w s t a f f 340 Mar 15 1 6 : 0 2 mage - - b d r w x r w x r w x 164 _ w w w s t a f f 5 5 7 6 Mar 15 1 6 : 0 2 mage - - c d r w x r w x r w x 172 _ w w w s t a f f 5 8 4 8 Mar 15 1 6 : 0 2 mage - - d d r w x r w x r w x 4 _ w w w s t a f f 136 Mar 15 1 6 : 1 0 mage - - e d r w x r w x r w x 12 _ w w w s t a f f 408 Mar 15 1 6 : 1 0 mage - - f

This folder is where Magento stores its cached data. Delete everything in this

folder to manually clear the Magento cache and restore you store’s functionality.

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

sion online.

Magento Setters and

Getters

In most MVC model systems a common pattern develops. Developers find they

need to store two general types of data

1. ”Business Logic” data (ex. a product’s SKU)

2. Data that allows the model to function (ex. the database table name)

It’s very common to see developers use a single

array

(or similar, hash table like

structure) property to store the business logic data, allowing the other class/ob-

ject properties to be used for system functionality. Magento is no different. Any

object that comes from a class that inherits from

Varien Object

(which includes

both models and blocks) has a protected

$ data

property

/* * * O b j e c t a t t r i b u t e s * * @ v a r a r r a y */ p r o t e c t e d $ _ d a t a = a r r a y ();

This array holds all the object’s business logic data. You can get an array of

key/value pairs for this data with the

getData

method.

v a r _ d u m p ( $object - > g e t D a t a ( ) ) ;

If you want toseta specific data field, use

$object - > s e t D a t a ( ’ t h e _ k e y ’ , ’ v a l u e ’ );

similarly, if you want a specific field back from an object, you can use

and you can set multiple keys at once by using

setData

with an array.

$ v a l u e = $object - > s e t D a t a ( a r r a y (

’ t h e _ k e y ’ = > ’ v a l u e ’ ’ t h e _ t h i n g ’ = > $thing , ));

You’ve probably noticed we’re naming our keys using an all lower-case, underscore-

for-spaces convention. While nothing enforces this, itis

the standard Magento

convention. Beyond consistency, this also helps when it comes to Magento’s

magic getter and setter methods.

G.1

Getter and Setter

In addition to the data getting and setting methods mentioned above, there’s

also a more ”magic” syntax.

$ k e y = $object - > g e t T h e K e y (); $object - > s e t T h e K e y ( ’ v a l u e ’ );

Using PHP’s

call

method, Magento has implemented their own

get

and

set

methods. If you call a method on an object (with

Varien Object

in the inheritance

chain) whose name begins with

get

or

set

,

and

there isn’t an existing method

already with the same name, Magento will use the remainder of the method

name to create a data property key, and either get or set the value. That means

this

$object - > s e t T h e K e y ( ’ v a l u e ’ );

is equivalent to this

$object - > s e t D a t a ( ’ t h e _ k e y ’ , ’ v a l u e ’ );

That’s why it’s important to keep with the lowercase/underscore key convention.

Magento will convert the leading-camel-case

T h e K e y

into a key named

t h e _ k e y

Another neat feature here is that the

set

method will always return an instance

of the object being set, which enables method chaining

$object - > s e t F o o ( ’ bar ’ ) - > s e t B a z ( ’ h o l a ’ ) - > s a v e ();

After using this style interface for a few weeks you’ll be loath to return to typing

out array brackets.

In document No Frills Magento Layout. Alan Storm (Page 149-154)