Drupal - Changing the HTML produced by Panels: Is it possible?
I've looked into this issue before. Here's the issue you want to read. It seems nuts at first, but there are actually good reasons for it being like this.
Bottom line: think of Panels Flexible Layouts as a prototyping tool. Use it to try out and draft ideas in development, staging, user-testing etc, then when you've got the perfect layout signed off, implement it for production with clean, crisp, hand-optimised HTML as a custom Panels layout (more on those below).
Here's a quick summary of why it is the way it is, based on that issue I linked to:
- Panels' HTML output needs all that crazy markup to be able to accommodate the craziest flexible layouts anyone could want to create - think loads of nested fluid and fixed regions side by side.
- Nothing automatically generated will be good enough for HTML purists, so the Panels guys haven't tried to keep them happy within the flexible layouts feature. So there's no HTML optimiser algorithm that tries to pare down the HTML generated by your layout to the bare minimum, because no matter how good it was, it still wouldn't be best-standards HTML.
- Something they don't mention: if there was such an optimiser, certain changes to the layout would cause now-redundant divs and classes to disappear. If you hung CSS off these, you'd need to re-write your CSS most times you make a small change to a Panels flexible layout. This means that if they tried (and failed) to make Panels Flexible Layouts into a best-standards production tool, they would also have weakened it as a prototyping tool.
- You can get clean, lean Panels HTML output. The expected workflow, reading between the lines, is I believe something like this:
- You use Panels Flexible Layouts during early development, while trying out ideas.
- Once you have settled on a particular layout, if you want clean HTML, you implement it using a custom layout, using only the precise amount of HTML markup that layout needs with just the right classes, semantics etc for your site (how-to below).
While I didn't like it at first, on reflection I think it makes sense. Rather than trying to do the impossible and create one tool that is perfect for both prototyping and for best-standards in a production context - which both have very different needs and could only result in a comprimise that wasn't quite right for either - they've gone with one tool that is ideal for prototyping and another seperate tool (custom layouts) that is ideal for lean production.
This means you can get the best of both, at the cost of a little bit more work implimenting your final layout as a template based on best-standards HTML - but a HTML purist will want to do that little bit of extra work anyway to get their output perfect.
How to create custom panels layouts? There's a detailed guide in the documentation on how to do so. Here's a quick basic summary:
- Copy an existing layout from
/sites/all/modules/panels/plugins/layouts
to become/sites/all/themes/[your_theme_name]/layouts/[new_unique_layout_machine_name]
- Rename the files in the new copy to match their new folder name. Note that the
.tpl.php
file should have hyphens instead of underscores. - Add a line to the theme info file:
plugins[panels][layouts] = layouts
This tells Panels that you have custom layouts in this theme, and where they are (replace the stringlayouts
if it's not the path from your theme to your custom layouts folder) Edit the files with your perfect, sleek, minimal, semantic best-standards HTML and CSS.
- Don't forget to also update the
.inc
file with info about your layout like name, categories, filenames, etc. theme
in the .inc file should match the.tpl.php
file's filename, without the extension. Any underscores you enter here in thetheme
value will be converted to hyphens.- Also consider editing the
.png
icon to better reflect your actual layout.
- Don't forget to also update the
- Clear the cache. Your new layout appears as an option in Panels with the name, category, etc you specified in the
.inc
file. Use it.
You might also consider the Clean Markup Drupal 7 module for controlling markup output for Panels.
I searched this page in the browser and did not find a mention of it already, including in the really good comprehensive answer given by user568458.