ajsie
ajsie

Reputation: 79686

how to organize all js, css, php and html code?

Now I know that you can use OOP and MVC to organize, but that is just for PHP.

Let's say, I add a new window that pops up when a user clicks on a link and it displays a form with JS validations and it is CSS styled.

Here we got 4 codes: JS, CSS, PHP and HTML (with some PHP snippets).

How would you organize all these codes? Because when I got 50 windows codes are spreaded everywhere and for me to change behaviour of delete a window, I have to play detective. I crunch everytime I have to add a new window with JS, CSS and so on.

I have thought about the structure. wouldn't it be better if you got a separate "module" for each one of the window. E.g. a folder for each one of the window. In that map you place one CSS-, one JS-, one PHP-, and one HTML-file? Then you got a very nice structure that aren't messy and you dont mix all windows with each other in one big JS and CSS file.

What do you think? And I would appreciate suggestions of how to organize these 4 kind of codes.

Upvotes: 12

Views: 9287

Answers (6)

jayism
jayism

Reputation: 13

If you are only dealing with the file types mentioned, my best experience is to have folders and files separate everything, and it also keeps it organized with everything Super Easy to Find as your Site Grows & Expands!

OK, so as for folders, you want all you're main HTML, PHP files in the:

/public_html/ (or whatever it's called on you're host. ie. The main area with the cgi-bin inside that is viewable as your main web folder).

eg-:

  • public_html/index.html
  • public_html/contact-us-by-email.php
  • public_html/main.html
  • public_html/archives.php

NEXT is the CSS and Javascript (JS), these will have their own folders inside your /public_html/ folder. and each file ending in .css or .js go in those particular folders obviously...

eg-:

  • public_html/css/style.css
  • public_html/css/dark.css
  • public_html/css/mobile.css
  • public_html/js/colorpicker.js
  • public_html/js/contact-form-tips.js
  • public_html/js/main.js

And that is That! As easy as it comes to remember, and once you are advanced enough at coding this is the best way to do things. The more advanced you get, the more file types, and also the more 'neat' you are going to want to make it look = organized, you may want to add an /includes/ folder before the /css/ /js/ /templates/ /images/ & so on it goes!

I hope this works out for you, or one of them!

THIS IS MY 1st QUESTION ANSWERED, SO I'M PROUD OF MYSELF & HOPE THE POSTER LIKES MY SOLUTION, GOOD LUCK!

Upvotes: 1

user237076
user237076

Reputation:

I like Django's way to organize folders. Let's try to imagine and adapt it to your php project:

Root folder is the Project folder, let's say the name of the website. It contains

  • common shared settings and values (ie. database access values, paths etc.), maybe helper functions (not Object Oriented), call it settings.php and/or utils.php or whatever
  • a media folder that also has its own structure:
    • css folder, for general css (ie. reset.css and common.css, for defining a general layout)
    • image folder, for common shared images
    • js folder, for common shared javascript code
  • a template folder containing static common pages not belonging to particular categories of the website

Each Root subfolder is an expression of an application of your project (ie. registration, news, announcements, faq, contacts, forum, ...) and contains:

  • a model folder in which you put your Models in PHP (MVC pattern)
  • a controller folder where you implement the Controllers in MVC pattern
  • a view folder in which you put the Views of your MVC pattern (i.e. quite static php pages responsible just to present the results passed by the Views)
  • a media folder structured exactly in the same way you structured the one for the root folder. In this folder you put elements only belonging to the particular part of the website you're developing.

For connecting the components, you could directly call / include them based on their paths, or you could implement a php file inside the root directory and each subfolder that is responsible for mapping urls and redirect the requests. Call it index.php or urls.php or connector.php, whatever.

It may seem redundant but it is not, and provides a high quality separation of concerns.

Upvotes: 7

Doug Neiner
Doug Neiner

Reputation: 66191

If you expect the same user to open a few of these different popup windows during a single visit, for their sake you need to consolidate your files so they are cached and the end users doesn't need to load all the CSS/JS again on each popup.

The folder approach is fine for the images and the JS. Unless every popup is drastically different, I would suggest a single CSS file for your own sanity. So your folder structure might look like this:

css/
* layout.css
popups/
* add_new
   - add_new.js
   - logo.png
   - add_new.php
* delete
   - delete.js
   - other_logo.png
   - delete.php

Now, before you deploy you can decide if it makes sense to compile your JS into a single file, or if the separate files would be best. (For instance, if the user opens 30 of the 50 windows every visit, use a single file)

Upvotes: 1

joatis
joatis

Reputation: 3435

If you can/need I'd create top level folders for JS, CSS, PHP, etc that would contain code that could be used across different windows. There's no need to have 50 copies of the same CSS file if they are all the same or even mostly the same code.

Then create a folder for each "window" that has seperate css, js, etc folders. Here you could put the files that are specific to that particular window. This way if you're only changing 1 css rule or js function, that is used by every window you could change it in 1 place.

If you ever need to change the rule for just 1 window, put the rule in the "local css" folder for that window and it will override your default. (That is if your HTML Links it after the "global css")

Upvotes: 1

erisco
erisco

Reputation: 14319

If you are looking for examples on how to design an application, having a look at the many frameworks is a good start. Even just their file structure will give you an idea. Typically, they organize their code into modules where both the PHP code and HTML templates also reside. None in particular are better to look at, but try: CakePHP, Symfony, CodeIgniter, or Agavi.

They will not do a great job on suggesting how to organize JavaScript and CSS files, however. When I make an application, I typically only have a handful of CSS files. I am surprised that you seem to need one per page but if you do: embed them. The advantage of external style sheets is completely lost if there is nothing reusable about their styling. JavaScript files, again, if they are not reusable you should simply embed them. Less HTTP requests per page load makes everyone happy.

When you find yourself scouring for a particular file that stubbornly will not surface itself, grep is an invaluable tool. Here is a random article that illustrates its usage.

Upvotes: 1

Rich Bradshaw
Rich Bradshaw

Reputation: 72975

I generally have my PHP pages in one folder, (maybe 10 files if it's a medium size site) then a subfolder in there called media, in which I put a css folder, a js folder an img folder, a swf folder etc.

I have 2 css files, one's a reset, the other has the style for the whole site written in chunks. I use a class on the body tag to target different page layouts.

The js folder has jquery, a file that's run on every page, then specific files for individual pages.

This keeps things pretty straight forward really.

Upvotes: 3

Related Questions