Separating code from layout in template

后端 未结 4 1412
渐次进展
渐次进展 2021-02-20 08:17

I have an app built around an MVC pattern. The view is php but mostly html with minimal php code embedded, stuff like this -

Welcome 

        
4条回答
  •  时光取名叫无心
    2021-02-20 09:18

    Excellent question, which is very uncommon for this site.

    First of all you HAVE to accept the fact that any real life template will never be simple. That's it. It's called presentation logic and it follows the complexity of the presentation itself.

    So, just make it the same way as before -- by using the same loop and conditional operator and -- possibly -- some home-brewed helper functions, which will spoil template purity but greatly helps.

    In fact, you have almost nailed it. you can use predefined variables, however they should contain no HTML but just values to be displayed.

    
     
      
       Name
       
       
      
     
    

    it is tempting to make a loop out of this but I would advise against that, because it will lead you to inventing some HTML builder which is another story and not that reliable as pure HTML itself.

    The only blame for you: a programmer, asking for the code assistance, should bring the code, not wordy blab. An example of the actual HTML would be much better, allowing answerers not to invent it out of the head.

提交回复
热议问题