Separating code from layout in template

后端 未结 4 1401
渐次进展
渐次进展 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 08:59

    Separate the code to create the table into a ViewHelper. Configure the method call from the template. Have the ViewHelper render the actual table with all required settings. Your main template should then contain only a line like this:

    
    

    Basically, any logic required to build that table will be contained within the ViewHelper which is why you might want to make it a class. The complete implementation will vary depending on your needs.

    If you dont want to put all the logic into one large Table Helper, consider making helper functions for any parts of the table. For instance, something like

    
    

    This way, the sortLink helper would decide whether and how to create and insert any sort links, so you dont have to have that logic or any conditionals in the main template.

提交回复
热议问题