You are seeing an error because you are nesting Sawtooth Script like "[% [% %] %];" this is not allowed. Instead, you need to either restructure the code to remove the nesting, or replace the inner Sawtooth Script with their unverified Perl equivalents and string concatenation.
Option 1 example:
<img src="[% GraphicsPath() %]MDLayout[% Begin Unverified Perl
if (...) {
return 1;
}
else {
return 2;
}
End Unverified %].jpg"/>
Option 2 example:
return '<img src="' . GRAPHICSPATH() . 'MDLayout1.jpg"/>';