This question is purely out of curiosity. I am very picky over the neatness of my code and I hate using inline styles, however I have found a use case where I don't see any way to not implement them.
I am building a portion in my CMS which allows users to create multiple page sections with a lot of customisation, a page may look like
[ section A ] <--- Has an image, custom margins and padding set for content
[ section B ]
[ section C ] <--- custom margins and paddings set on this section
At the moment I have 3 database tables, one which supports basic information on the section such as a media link, title, subtitle etc. Another which supports toggle options such as bleeding the overflow of the container, aligning text horizontally/vertically and then a third table which stores information on specific values for that table, i.e. the padding or custom margins set on text, or the zoom %age of an image.
Right now, when my page loads I pull all the data back for each section and iterate through an array assigning the styles inline with my elements with PHP:
function build_left_section($section) {
// Echo the standard section piece
echo '<section class="section_wrapper section_overflow" style="background:'.$section['section_color'].'; height:'.$section['height'].'px">';
echo '<div class="content row">';
$this->get_borders($section);
echo '
<div id="left_col" class="image_column grid_6 ' . $this->get_width($section, 'left') . '">
<img id="image" src="'. $section['media_link'] .'" alt="'. $section['tags'] .'" style="margin-left:'. $section['image_x'] .'%; margin-top:'. $section['image_y'] .'%; width:'. $section['image_zoom'] .'%;">
</div>
<div id="right_col" class="text_column grid_6 ' . $this->get_width($section, 'right') . '">
<div class="text_move" style="margin-left:'.$section['text_x'].'%; margin-top:'.$section['text_y'].'%">
<div class="text_wrap '.$this->get_text_alignment($section).'" style="padding-left:'.$section['text_padding'].'px; padding-right:'.$section['text_padding'].'px;">
'.$this->get_text($section).'
</div>
</div>
</div>
';
}
I hate how messy this code looks, and I'm sure there are some considerable performance drawbacks due to it bulking the size of my HTML doc, I also read somewhere that it may incur cache problems.
Does anybody have any suggestions on how I could achieve the same result more effectively?
Thanks in advance.