In today’s quality code tip, you will learn about removing the HTML from your business logic and putting it into a view file. In programming, you want to separate out your code by its intent and purpose. Let’s discuss the what, how, and why of view files. (Side note: I’m showing you the actual code on this website which is powered by WordPress and Genesis.)
Functions and class methods are for your business logic. Their intent is to do the work such as check authorizations, interact with the database, process data and forms, assemble the components, sanitize pieces of data, and then call the view file. That is what your business logic does. It is the specifics which are unique for your project and website.
View files are responsible for presenting the content and supplying its context and meaning, i.e. the HTML. This is the code that is rendered out to the browser. The processing and work are all done before the view file is called.
I'm getting the HTML out of my PHP functions & putting it into a View file #cleancode Share on XAs a side note, JavaScript does not go in the view or business logic. It goes in a separate script (.js) file and then is loaded separately. When using WordPress, you enqueue the file and let WordPress handle it.
Keep your code separated by intent.
Why?
It’s your job as the developer to create quality code that solves your client’s problems. Quality has many aspects to it, including readability and maintainability. It is much easier to read code that is lean and skinny, as well as serving a single purpose. When you look at the PHP code, you know it’s responsible for processing, sanitizing, gathering, and working. When you look at a CSS file, you know it’s for styling and giving your site personality and uniqueness. When you look at a JavaScript file, you know it handles the interactivity on the front-end for the person viewing the site. And the view file is responsible for the HTML.
Work to organize your code by intent and purpose. Improve the quality of your craft.
Lorenzo Rodriguez says
I jumped late on the php train. It is nice to learn about coding with a clean syntax and separating business logic from view markup
I wished I had learned this much earlier.