Theming for Magento — Part 03 — Theming Basics

Digg Muti Delicious

Right. We’re in part 03 of our “Theming for Magento” tutorial col­lec­tion. Time to get down to some theming.

A quick word on conventions

Magento, like many other con­tent man­age­ment sys­tems, likes to fol­low con­ven­tions. Adopting these con­ven­tions when design­ing your theme, as well as dur­ing the them­ing pro­cess, can greatly min­im­ize the amount of code required by your theme. As men­tioned on Part 02, Magento has a “fall back” fea­ture where it looks for required files, when not in your theme, within all other themes in the cur­rent inter­face until it finds the file it requires. This essen­tially allows users to keep a copy of the default theme in the inter­face in which they are cod­ing their theme (either the “default” or a cus­tom inter­face) and to only modify the files that require customisation.

An example of the above-​​mentioned con­ven­tions can be seen in the way that boxes are styled within Magento. Boxes are not unlike “wid­gets” in WordPress. Each box is assigned a unique class for spe­cific styl­ing to that box, as well as a gen­eric class for global styling.

A CSS block for global Magento box styl­ing looks like this (no attrib­utes cur­rently set for each class):

.box {}
.box .head {}
.box .head h4 {}
.box .content {}

By styl­ing the above tags in a CSS file within your theme, you should be able to style the major­ity of side­bar blocks within your Magento theme.

Setting up your theme’s stylesheets

As men­tioned in Part 02, Magento themes are made up of tem­plates and XML files, as well as a folder to hold all images, CSS files and other assets. For starters, I’d recom­mend mak­ing cop­ies of all CSS files within the default theme and pla­cing them in the same rel­at­ive loc­a­tion within your cus­tom theme (most likely the “CSS” folder within your theme’s “skin” folder). Once you’ve got the hang of work­ing with these CSS files, or to add your own so your own CSS file con­ven­tions can be used, the calls to the CSS files are loc­ated in page.xml near the top of the file. Once you get the hang of the con­ven­tions used within Magento, adjust­ing the theme’s CSS becomes a lot easier.

Great, so what other con­ven­tions are there?

If you’re work­ing in Firefox, I’d recom­mend installing the Firebug exten­sion, or some­thing sim­ilar. In Safari, use the “inspect Element” com­mand. This will allow you to inspect the code of your Magento install­a­tion and to identify con­ven­tions within the code. As there are an almost end­less amount of tem­plate files, when styl­ing your theme I find it best to keep a copy of the default theme within the inter­face in which I’m cod­ing and to make as many changes as pos­sible via the CSS and images without modi­fy­ing the tem­plate *.phtml files or *.xml files. This way, when your theme is done, you may end up with only sev­eral mod­i­fied *.phtml and *.xml files. Also, if you want to then pack­age your theme as a stan­dalone theme (ie: without requir­ing the need of the default theme), you can simply copy the unmod­i­fied files from the default theme over to yours and voila!

A Very Important Final Note

Well, folks, that’s it for this round of the “Theming For Magento” tutorial col­lec­tion. In Part 04, we’ll be open­ing up one or two *.xml and *.phtml files, check­ing out how they work and look­ing into ways to cre­ate smal­ler code snip­pets where possible.

A very import­ant last note: when cre­at­ing your Magento theme, make a note of all the files you’ve mod­i­fied along the way. For example, if you only modify the CSS files, make a note of that. It will help greatly later in when identi­fy­ing unmod­i­fied files for copy­ing over from the default theme and can, in gen­eral, be a real time-​​saver.

Related Posts

8 Comments

  1. Nel's GravatarNel
    Posted 25th January, 2010 at 11:54 pm (224 days ago)

    Oh dear. You are my life­line right now on this Magento pro­ject I’m work­ing on. Are you going to add a part 4 soon?

  2. Posted 26th January, 2010 at 11:43 am (223 days ago)

    Hi Nel,
    Part 04 is on it’s way.

    Are there any areas in par­tic­u­lar which you would like me to dis­cuss in Part 04?

    Kind regards,
    Matty.

  3. Deb's GravatarDeb
    Posted 2nd February, 2010 at 5:10 am (217 days ago)

    Thanks — great post!

    Do you start with the blank, default or from scratch when devel­op­ing a new site?

    Patiently wait­ing for part 04

    • Posted 7th February, 2010 at 1:42 pm (211 days ago) in reply to Deb

      Hi Deb,
      Thanks very much. :)

      I usu­ally start as I would when build­ing a web­site for any other CMS, which is by build­ing the static XHTML/​CSS first. From there, I setup my code within either a cus­tom inter­face (with a copy of the default theme in it) or in the default inter­face and I begin slowly migrat­ing my code over to Magento. From there, because I have the default theme in place, all tem­plates and lay­out XML files can be eas­ily found from the default theme. From there, I slowly cus­tom­ise any tem­plates for any mod­ules that require spe­cific cus­tom­isa­tion. Once I’m done with that, I copy and paste the unmod­i­fied tem­plates and lay­out XML files from the default theme into my cus­tom theme.

      At this point it’s prob­ably a good idea that I men­tion that its a very good idea to keep a note of all files you modify in the tem­plate and lay­out folders… so you don’t over­write your cus­tom­isa­tions by acci­dent. :)

      I hope this helps.

      Cheers,
      Matt.

  4. Amber's GravatarAmber
    Posted 19th March, 2010 at 5:44 pm (171 days ago)

    I am work­ing on a Magento theme for the first time and I would love to read part 4!

    • Posted 19th March, 2010 at 6:49 pm (171 days ago) in reply to Amber

      Hey Amber,
      Thanks so much for your com­ment. :)

      I’ll hope­fully be work­ing on part 4 over the next few days and will post it as soon as possible.

      Are there any top­ics in par­tic­u­lar which you would like covered?

  5. Amber's GravatarAmber
    Posted 24th March, 2010 at 10:01 pm (166 days ago)

    I am cus­tom­iz­ing a store for a cli­ent who already has the main “inform­a­tional” web­site up. They want the store to fit seam­lessly with the web­site as far as look & feel. So the main site already has a stylesheet, etc. Any tips would be great.

    • Posted 25th March, 2010 at 8:02 pm (165 days ago) in reply to Amber

      Hey Amber,
      The main points I’ll be aim­ing to get across in part 4 will be the concept of a) how the vari­ous pieces are attached via the XML files and b) the concept of cre­at­ing “areas”. This concept is sim­ilar to cre­at­ing wid­get­ised areas in WordPress, if you’re famil­iar with that.

      Essentially, I’d recom­mend doing two things.

      1. Modify as little of the exist­ing Magento XHTML as pos­sible (copy the files from the ‘default’ theme as a start­ing point).
      2. Work with the concept of cre­at­ing “areas” (left, right, con­tent, etc) and style the vari­ous boxes (wid­gets, referred to in the XHTML/​CSS as boxes) without modi­fy­ing the XHTML. This will save you time in the future and also ensure that you don’t have to dive into end­less *.phtml files to change code.

      I hope this helps. Let me know if any­thing is unclear or if there’s any­thing else I can assist with. :)

      Cheers,
      Matty.

Leave your comment

Your email is never shared. Required fields are marked *

*
*