Jump to content

A Temporal Medley of Coding Principle: CSS, Javascript, and PHP


iwato

Recommended Posts

GREETING:  I hope that everyone who celebrated the American holiday of Thanksgiving this past week spent a wonderful holiday full of cheer, good food and drink, and restored family ties and friendships.  My celebration was short, alone, but very well spent.  And, in the quiet of the office, abandoned by nearly everyone for the holiday break, I was able to finish nearly three weeks of study, investigation, and experimentation with the grammar of the MySQL database.  You may discover the results by clicking on any direct reference to a specific podcast on the Grammar Captive Weekly Podcast webpage that is itself not a referenced podcast.  This includes requests for a specific podcast coming from remote third parties.  Not only do the referenced podcasts appear, where before they did not, but only that number of podcasts actually referenced fills the page.

Important in your regard is that I have W3Schools to thank for helping me to realize what for me has been a major feat.  Many thanks!

BACKGROUND:  In the same spirit of good coding practice I have another question related to coding strategy.

All of the Click and Listen panels are created from a subsection of the same HTML template that is included into a PHP template generator before being dumped into the host HTML webpage as a dynamically filled <div> of the Grammar Captive Weekly Podcast page.

QUESTION:  Where, how, and, of course, when is the best place and time to enter the CSS styling?  On the HTML template, the final hosting page, or dynamically via PHP before the page is dumped into the host page.  In answering these question please explain your logic for recommending the strategy that you do.

Roddy

 

 

Link to comment
Share on other sites

The only advice I'd give is to not generate the stylesheet with PHP since you want the stylesheet to be cached by the user's browser. Outside of that, it really doesn't matter.

It's common to have PHP compile a list of necessary stylesheet URLs and then have the template create <link> tags for them in the <head> section. With this approach, different sections of your program can append stylesheets to the list if they need one of their own.

Link to comment
Share on other sites

As a simultaneous learner and doer who has been subject to large interruptions (the largest lasting for as much as six years) I make an effort to document everything that I study and create.  To this end my HTML template is merely a <div> tag within an explanatory HTML page.  As a result, the only very useful <head> tag is that of the dynamically filled host page.  In the past, I have used the CSS scoped attribute to style specific sections or divisions that I have included into the hostage.  

What is your opinion of this practice?.

Roddy

Link to comment
Share on other sites

I would recommend against it. Since not all browsers support the scoped attribute you'll find that in some browsers your CSS is applying to elements outside of your scoped area. Aside from that, doing it this way means your CSS is scattered all over the place which is harder to maintain.

If you don't have a <head> section then where is all the metadata for your page?

Link to comment
Share on other sites

Quote

If you don't have a <head> section then where is all the metadata for your page?

It is in the host page into which the <div> tag from the HTML template is displayed.

Roddy

Link to comment
Share on other sites

Alright, please allow me to return to your original suggestion.

Quote

It's common to have PHP compile a list of necessary stylesheet URLs and then have the template create <link> tags for them in the <head> section. With this approach, different sections of your program can append stylesheets to the list if they need one of their own.

The procedure that i use for creating the Click and listen panels inserted into the podcast host page, is somewhat complex.  

Step One:  I create an HTML file that forms the basic structure of the page.  It is minimally formatted, and all styling is either inline or uses a scoped <style> tag.

Step Two:  I then set the starting and ending points of that portion of the HTML file  that I would like to include in my template, read the file using the get_file_contents() method, and extract that portion of the file defined by the starting and ending points.  What is read contains the inline CSS and the scoped <style> tag.

Step Three:  The result is then combined with data obtained from a MySQL data base and inserted into the host page via an AJAX call of HTML data type.

As you have intimated, I would prefer to use a CSS style sheet that would allow better manageability of insert, but I do not know where to place it.  

There are only two <head> elements in the entire procedure.  The <head> element of the host page into which the Click and Listen panel is inserted and the <head> element of the HTML page from which only a portion is used in the insert.  This latter <head> tag is useless in the page that hosts the insert.  It is in the <head> that I am accustomed to placing the <link> tags that contain CSS stylesheets.

THE DIFFICULTY:  I am troubled by the fact that the insert is introduced into the host page only after the <head> element of the host page has been read by the browser.

QUESTION:  Will a style sheet already loaded into the host page be applied to new HTML entered via AJAX?

Roddy

 

Link to comment
Share on other sites

I'm don't know how your entire website is structured. Are you using a content management system? Whichever system you're using, it should provide a way to modify content in the head of the page.

The browser should only see one <head> element per document, otherwise your HTML is invalid.

If you want to add a stylesheet after an AJAX call, you can generate it using Javascript. Have the server return the URL of the stylesheet and have Javascript use the URL to generate a <link> tag.

// Assuming AJAX returned a JSON object in the data variable
if(data.stylesheet) {
  var link = document.createElement("link");
  link.rel = "stylesheet";
  link.href = data.stylesheet;
  document.getElementsByTagName("head")[0].appendChild(link);
}

 

  • Thanks 1
Link to comment
Share on other sites

No CMS.  So, likely many bad, but innovative things.  This said, I think I get the idea.  I need to experiment a little.

Roddy

Edited by iwato
Link to comment
Share on other sites

It appears from the condition of the above if statement that stylesheet is a built-in property of the returned data object.  Is this true?

Roddy

Link to comment
Share on other sites

If you decide to go with that approach, it is. The server would return a JSON structure a bit like this:

{
  "html" : "<div class=\"something\">Some content</div>",
  "stylesheet" : "/css/style.css"
}

There are hundreds of potential solutions, that's just one of them. Programming is about identifying and solving problems. I'm not saying this is the best solution for your project, it is just one of them.

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...