Welcome Guest, Not a member yet? Register   Sign In
Better view files
#1

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, www.ignitedcms.com
Reply
#2

(This post was last modified: 08-01-2015, 12:22 AM by John_Betong. Edit Reason: Spelling: not my fortay )

(07-31-2015, 03:46 PM)iamthwee Wrote: 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.

Maybe some posters could assist in formatting if you could supply a link to your "really bad indentation" view file.
Reply
#3

View files are on github, but there's a lot.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#4

Some editors can help you with automatic formatting. Sublime text with a plugin or phpstorm for example. Not quite shure how they handle mixed (php in html) but i'm shure they can make it better without alot of work. But the best practise is off ource to write readable code from the start Wink
Reply
#5

(This post was last modified: 08-02-2015, 05:04 AM by ignitedcms.)

Quote:Not quite shure how they handle mixed (php in html)

They don't Wink

Which is the problem, html tidy garbles any codeigniter form helpers especially with arrays, that is why I was looking to pass variables to my views using the template parser class and remove CI form helpers altogether - they're pretty useless for heavily customized frontend UI's anyway.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#6

(08-02-2015, 03:28 AM)iamthwee Wrote: View files are on github, but there's a lot.

Can you give the link to one of them.
Reply
#7

https://github.com/ignitedcms/ignitedcms...d-post.php
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#8

(08-02-2015, 06:36 AM)iamthwee Wrote: https://github.com/ignitedcms/ignitedcms...d-post.php

I prefer to manually indent the view files because it is so much easier to isolate sections for testing purposes. It may seem an arduous task but well worth the effort and helps debugging.

I am intrigued as to how you created the relevant form. Was it hand-coded or PHP generated? It appears at first glance that there is a lot of CSS duplication and the styles should be replaced with classes.

Reply
#9

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.
Reply
#10

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, www.ignitedcms.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB