I create a lot of views in my work as part of the common MVC pattern applied in web frameworks like Code Igniter and Cake.
I also adhere to the following maxim as best I can: fat models and library objects doing the logic; skinny controllers responding to user actions server side; and views presenting request results and providing UI.
However, in some cases views need to be more complex. We might need to do different things based on the state of each looped item for example and sometimes this gets quite complex.
The problem with this is that the identing applied to both html and php soon gets out of sync.
A very simple example:
<ul> <li> ... </li> <li> ... </li> <?foreach> <li> ... </li> <?endforeach> </ul>
We now have li elements that are at the same depth in the output document but not at the same level in the code. If we start using other control structures this can get worse:
<ul> <li> ... </li> <li> ... </li> <?foreach> <li> ... </li> <?if> <li> ... </li> <?endif> <?endforeach> </ul>
And so on ...
Is there a well used pattern I can follow that minimises this problem even with complex views ?