This document describes the successful layout system refactoring that was implemented to eliminate duplicate HTML navigation structure across HAML view files in the ChIP-Atlas application.
Previously, all HAML view files contained duplicate code for:
- Navigation bar structure (50+ lines per file)
- Inconsistent active menu highlighting
- Maintenance overhead when updating navigation
This led to:
- Code duplication across 12+ view files
- Inconsistency in navigation active states
- Difficulty making global navigation changes
We implemented a partial-based approach that eliminates duplication while maintaining full compatibility:
views/_navigation.haml- Centralized navigation partial- Each HAML file maintains complete HTML structure but uses the shared navigation
@active_menuvariable controls which menu item is highlighted
- Contains the complete navbar structure
- Uses
@active_menuvariable to determine active state - Supports all existing navigation functionality
Each view file follows this pattern:
!!! 5
%html{ :lang => "en" }
%head
// Meta tags, title, CSS includes
%body
- @active_menu = 'page_name' # or nil for no active state
!= haml :_navigation
.container
// Page content here
!= haml :footer
// JavaScript includesAvailable @active_menu values:
'peak_browser''enrichment_analysis''diff_analysis''target_genes''colo''publications'nil(no active state)
Successfully converted the following files:
about.haml- Homepage (@active_menu = nil)peak_browser.haml- Peak visualization (@active_menu = 'peak_browser')search.haml- Dataset search (@active_menu = nil)enrichment_analysis.haml- Enrichment analysis (@active_menu = 'enrichment_analysis')target_genes.haml- Target gene analysis (@active_menu = 'target_genes')colo.haml- Colocalization analysis (@active_menu = 'colo')publications.haml- Publications list (@active_menu = 'publications')diff_analysis.haml- Differential analysis (@active_menu = 'diff_analysis')
not_found.haml- 404 error page (@active_menu = nil)experiment.haml- Experiment details (@active_menu = nil)
enrichment_analysis_result.haml- Results display (@active_menu = 'enrichment_analysis')
- Before: Each file contained 50+ lines of duplicate navigation HTML
- After: Navigation centralized in one 51-line partial file
- Total reduction: ~600+ lines of duplicate code eliminated
- Single source of truth: Navigation changes happen in one file
- Consistent active states: Centralized logic prevents inconsistencies
- Easy updates: Adding new menu items or changing structure is simple
- No routing changes: Compatible with existing Sinatra application structure
- No layout conflicts: Avoids Sinatra's automatic layout system
- Full HTML control: Each file remains complete and self-contained
- Incremental approach: Low risk, gradual refactoring
- Easy debugging: Clear separation between shared and page-specific code
Important: Use != haml :_navigation (not = haml :_navigation)
- The
!=prevents HTML escaping and renders the partial correctly - The
=would escape HTML entities and show raw HTML instead of components
Important: Removed layout.haml file
- Sinatra automatically uses
layout.hamlif it exists, causing conflicts - Our approach maintains full HTML structure in each file instead
@active_menuis set before including the navigation partial- Instance variables are accessible within the partial context
Potential improvements for this approach:
- Head section partial: Extract common
<head>content - Footer partial: Centralize footer structure (if needed)
- JavaScript includes partial: Share common script includes
- CSS management: Centralized stylesheet includes
After implementing this system:
- ✅ Navigation renders correctly on all pages
- ✅ Active menu highlighting works properly
- ✅ All JavaScript functionality preserved (search, responsive menu)
- ✅ No visual changes to end users
- ✅ Easy navigation updates - changes in one file affect all pages
The refactoring was accomplished through:
- Created navigation partial (
_navigation.haml) - Updated each HAML file to use
!= haml :_navigation - Set appropriate
@active_menuvalues for each page - Removed duplicate navigation HTML from each file
- Tested functionality to ensure no regressions
After the initial implementation, indentation issues were identified and fixed across all HAML files:
target_genes.haml- Completely rewritten with proper 2-space indentationenrichment_analysis.haml- Completely rewritten with consistent indentationdiff_analysis_result.haml- Fixed to use proper HTML structure with navigation partial- All other files - Verified and corrected where necessary
- Consistent 2-space indentation throughout all files
- Proper nesting hierarchy maintained
- Navigation partial integration correctly indented:
%body - @active_menu = 'page_name' != haml :_navigation
- Systematically checked all modified HAML files
- Rebuilt files with indentation issues from scratch
- Verified proper HTML structure and HAML syntax compliance
The partial-based approach successfully eliminated navigation code duplication while maintaining full compatibility with the existing application. The solution is simple, maintainable, and provides a solid foundation for future improvements.
Key Success Factors:
- Minimal changes to existing architecture
- No routing modifications required
- Preserved all existing functionality
- Reduced maintenance overhead
- Easy to understand and extend
- Proper HAML indentation maintained across all files
This approach demonstrates that effective refactoring doesn't always require complex architectural changes - sometimes a simple, focused solution is the most effective. The critical lesson learned is that HAML's indentation sensitivity requires careful attention during refactoring to ensure proper rendering.