Posts: 414
Threads: 62
Joined: Jul 2015
Reputation:
6
Hi guys,
I'm just currently deciding on whether I want to go the whole template thing, which would allow me to use a tool like html tidy on my views.
But at the moment there's a lot of php in the views and my view files are ugly/messy and terrible to read ( I must admit with really bad indentation )
What do you do to create tidy, well indented view files with php embedded? Do you use a template engine which allows you to use html tidy (this doesn't work when php is embedded).
I would love to know. I'm reasonably happy with the indentation in my controllers but not so much with my view files.
Practical guide to IgnitedCMS - Book coming soon
Posts: 414
Threads: 62
Joined: Jul 2015
Reputation:
6
View files are on github, but there's a lot.
Practical guide to IgnitedCMS - Book coming soon
Posts: 1,101
Threads: 4
Joined: Nov 2014
Reputation:
95
Honestly, the best method of getting clean view files when they contain a mix of HTML and PHP is to write clean files from the start. Further, just as with raw code, I use the indentation as a guide while I'm writing. Good auto-indent support in the editor is a start, and I find it useful to create my closing tags before filling them with content and have the editor prompt me with the closing tags so I can tell whether I forgot something. In fact, creating an outline of the view as a starting point is often very helpful in building a clean view, and, in some cases, I'll break up more complicated views into multiple files to keep it clean (for example, the content of a form might be in a separate file from the page on which the form is displayed.
In most cases, I've found tools built strictly for HTML will create more work than you would have had by just doing it by hand.
Also, as John_Betong mentioned, cleaner use of CSS and the removal of styles can help clean up the mess by just making it easier to find the individual elements. For example, you could easily add the clearfix class to the form and likely get the same result you achieved here with a clearfix div before and after your form. It should also be fairly easy to add the desired space around your form and/or panels with CSS rather than inserting an empty div with a margin-top style.
Learning to write clean HTML/CSS/JavaScript is important even if you think you're going to spend most of your professional life writing back-end code. At the very least, it helps you understand what the output should look like.
Finally, unless you have some real need for the comments in the HTML to be visible in your browser's "View Source" output, I would recommend using PHP comments, so they don't get transmitted to the client. It's a very minor thing, but the comments usually aren't aimed at the end users, but they have to put up with an extra couple of wasted bytes for something they'll never use.
Posts: 414
Threads: 62
Joined: Jul 2015
Reputation:
6
Thanks, I think I definitely made an elementary school boy error in rushing the prototype CMS system by writing very poor view files.
To tell the truth, the front end has never really been my strength, although that being said, my drag and drop page builder and menu is pretty damn advanced, for what it does. It's just my indentation that royally sucks, this was made worse by mixing codeigniter form helpers and vanilla html.
This will need a complete revisit, but first I need to move the BL into models.
Thanks for all your advice it is greatly appreciated, I've learnt a hell of a lot in a short space of time.
Practical guide to IgnitedCMS - Book coming soon