If you follow the inheritance chain of the Template Filter far enough back, you
eventually reach
Varien Filter TemplateM a g e _ W i d g e t _ M o d e l _ T e m p l a t e _ F i l t e r e x t e n d s M a g e _ C m s _ M o d e l _ T e m p l a t e _ F i l t e r e x t e n d s M a g e _ C o r e _ M o d e l _ E m a i l _ T e m p l a t e _ F i l t e r e x t e n d s M a g e _ C o r e _ M o d e l _ E m a i l _ T e m p l a t e _ F i l t e r e x t e n d s V a r i e n _ F i l t e r _ T e m p l a t e
1. Look for any
{{foo path="trans email/ident general/email"}}style strings
2. Parse the token for the directive name (
fooabove)
3. Create a method name based on the directive name. In the above exam-
ple, the directive name is
foo, which means the method name would be
fooDirective4. Use
call user functo call this method on itself, passing in the an array
containing a tokenized version of the directive string.
It’s beyond the scope of this book to cover the implementation details of each
specific directive. We mention mainly to let you know that, if you’re trying to
debug a particular template directive, say
{{ m e d i a url = " / w o r k f o r f r e e . jpg " }}
you can find its implementation method by taking the directive name (
media),
and adding the word
Directive. A quick search through the code base should
turn up the implementation
# F i l e : a p p / c o d e / c o r e / M a g e / C o r e / M o d e l / E m a i l / T e m p l a t e / F i l t e r . p h p p u b l i c f u n c t i o n m e d i a D i r e c t i v e ( $ c o n s t r u c t i o n ) { $ p a r a m s = $this - > _ g e t I n c l u d e P a r a m e t e r s ( $ c o n s t r u c t i o n [ 2 ] ) ; r e t u r n M a g e :: g e t B a s e U r l ( ’ m e d i a ’ ) . $ p a r a m s [ ’ url ’ ]; }
In this specific case we can see that the
{{media ...}directive simply grabs the
base media URL using
Mage::getBaseUrl(’media’), and appends the
urlparameter
to it.
Visit http://www.pulsestorm.net/nofrills-layout-chapter-six to join the discus-
sion online.
Widgets
Consder the following situation. You’re a developer. You have a deep knowledge
of the Magento system. The corporate VP in charge of giving you things to do
runs into your work area and says
I want to add a YouTube video to the sidebar?!
You start explaining layouts, and blocks, and pages, and how they render, and
which XML file he’ll need to edit, or maybe you could add it as a page update
o...
Your boss then gives you that steely, bossy look and says again
I wantto add a YouTube video to the sidebar
Most people don’t work on their own cars. Most people don’t harvest or hunt
their own food. And most people don’t want to code their own websites. That’s
the problem widgets set out to solve. In this chapter we’ll give you a full overfull
of the Magento widget system. From using the widgets that ship with Magento,
to creating your own widgets, to understanding how widgets are inserted into
the flow of the Layout.
7.1
Widgets Overview
So, what are widgets?
1. Widgets are Magento Template Blocks
2. Widgets Contain Structured Data
3. Widgets Contain Rules for Building User Interfaces
5. Widgets contain rules that say which blocks in the layout system are
allowed to contain them
Let’s start by building ourselves a minimum viable widget, and inserting it into
a CMS page. We’ll be building our widget in the
Nofrills Booklayoutmodule.
You, of course, are free to add widgets toanymodule you create.
To start with, we need to create a configuration file that will let Magento know
about our widget. Being a newer subsystem of Magento, widgets have their
own custom XML config file which will be merged with the Magento config as
needed. Widget config file are named
widget.xml, and should be placed in your
module’s
etcfolder
<! - - # F i l e : a p p / c o d e / l o c a l / N o f r i l l s / B o o k l a y o u t / e t c / w i d g e t . x m l - - >
< widgets > </ widgets >
There are times where Magento will load the widget config from cache, and
there’s other times where the config will always be loaded from disk. Because
of that, it’s best to always clear the cache when making changes to this file.
We now have an empty widget config. Next, let’s add a node to hold our widget
definition
<! - - # F i l e : a p p / c o d e / l o c a l / N o f r i l l s / B o o k l a y o u t / e t c / w i d g e t . x m l - - >
< widgets >
< n o f r i l l s _ l a y o u t b o o k _ y o u t u b e t y p e = " n o f r i l l s _ b o o k l a y o u t / y o u t u b e " > < name > Y o u T u b e E x a m p l e Widget </ name >
< d e s c r i p t i o n t y p e = " d e s c " >
T h i s w i g e t d i s p l a y s a Y o u T u b e v i d e o . </ d e s c r i p t i o n >
</ n o f r i l l s _ l a y o u t b o o k _ y o u t u b e > </ widgets >
Each second level node in this file tells Magento about a single widget that’s
available to the system. You should take steps to ensure this node’s name is
unique to avoid possible collisions with other widgets that are loaded in the
system from other modules. In this case, the name
nofrills layoutbook youtubeshould suffice.
It’s the
type="nofrills booklayout/youtube"attribute we’re interested in. This de-
fines a block class alias for our widget. We’re telling Magento that the block
class
N o f r i l l s _ B o o k l a y o u t _ B l o c k _ Y o u t u b e
should be used for rendering this widget. The
<name/>and
<description/>tags
are used for text display in the Magento Admin Console.
Let’s create that class. Add the following file
# F i l e : a p p / c o d e / l o c a l / N o f r i l l s / B o o k l a y o u t / B l o c k / Y o u t u b e . p h p
<? php
i m p l e m e n t s M a g e _ W i d g e t _ B l o c k _ I n t e r f a c e { p r o t e c t e d f u n c t i o n _ t o H t m l () { r e t u r n ’ < o b j e c t w i d t h = " 6 4 0 " h e i g h t ="505" > < p a r a m n a m e =" m o v i e "
v a l u e =" h t t p :// www . y o u t u b e . com / v / d Q w 4 w 9 W g X c Q ? fs =1& amp ; hl = e n _ U S " > </ param >
< p a r a m n a m e =" a l l o w F u l l S c r e e n " v a l u e =" t r u e " > </ param > < p a r a m n a m e =" a l l o w s c r i p t a c c e s s " v a l u e =" a l w a y s " > </ param >
< e m b e d src =" h t t p :// www . y o u t u b e . com / v / d Q w 4 w 9 W g X c Q ? fs =1& amp ; hl = e n _ U S " t y p e =" a p p l i c a t i o n / x - s h o c k w a v e - f l a s h "
a l l o w s c r i p t a c c e s s =" a l w a y s " a l l o w f u l l s c r e e n =" t r u e " w i d t h = " 6 4 0 " h e i g h t ="505" > </ embed > </ object > ’ ; }
}
This class ismostlya standard block class. It extends from the
Mage Core Block Abstractclass, and we’ve overridden the base
toHtmlmethod to have this block return
the embed code for a specific YouTube video. The one difference you’ll notice
is the class definition also has this
i m p l e m e n t s M a g e _ W i d g e t _ B l o c k _ I n t e r f a c e