1

How does Stackoverflow reuse their code, especially the header and footer?

So, basically when i click on 'Tags' only the content changes, not the header and footer. What are the best pratises? A link to a simple tutorial would be good.

I'm using HTML5, PHP, and MySQL. No javascript involved yet.

4
  • 1
    Are you talking about loading page content without refreshing the whole page (ajax) ? Commented Feb 8, 2012 at 9:52
  • 1
    yeah, but also code-reuse. i don't want to update header/footer on each page. Im not using Ajax at the moment. Creating my site entirely in css and html (and php). i'll definitely add some javascript later. my thought is that the site should work without javascript... Commented Feb 8, 2012 at 10:05
  • 2
    +1 for the progressive enhancement approach, @user1163859 Commented Feb 8, 2012 at 10:16
  • thanks! +1 for swift responses @vzwick Commented Feb 8, 2012 at 10:20

4 Answers 4

4

With no framework whatsoever involved, try something along these lines:

<?php

include('header.php');

// do something, render page specific content ...

include('footer.php');
Sign up to request clarification or add additional context in comments.

2 Comments

Hm, does that render the html locatedin header.php/footer.php? or just the php?
include() does exactly what it sounds like – it inserts the referenced php file and executes it. Documentation here. So, yes, it renders the HTML as well.
1

The way they do it is by separating all of the static content ( the one that wouldn't change between pages) into seperate files. Then they include them by demand as @vzwick pointed out.

It must be said, that they ARE loaded. SO is not using AJAX to reload only specific parts of the page for layout purposes. The header and footer parts are cached in between server calls, and seem to load almost instantaniously.

This is the principle that most of web-scripting languages work upon.

3 Comments

Ok, so the advantage is that its a cleaner way to code, since i would only need to update the header/footer once. But, it doesn't necessary make my site faster...right?
The speed gain is in the amount of coding you have to do and the maintainability of your code ;)
If you're not using some sort of PHP caching mechanism to cache the parsed PHP, then no, there will be no speed gain. What would make your site faster would be allowing the broswer to cache css and javascript. Also gzip'ing the page would help tremendously.
0

Ajax is a technology to reload only particular divs, not the whole page: Ajax

Comments

0

I dont know if its possible with HTML5, but I use AJAX and JQUERY for a dynamic site/content refresh

the Jquery Documentation is to find at http://docs.jquery.com/Main_Page and for the JqueryUI at http://jqueryui.com/demos/

here is a simple JQUERY/AJAX code snippet

$.ajax({
    beforeSend:function()   {
        $("#loader").show();
    },
    url:your/path/file.php,
    method:"get",
    data:{rights:usrRights,rights2:usrRights2},
    dataType:"html",
    success:function(output)    {
        $("#content").html(output);
    },
    complete: function() {
        $("#loader").fadeOut("slow");
    }
});

included a loader div with an overlay and by default its hidden in the div i put a .gif loader image

hope I could help you

regards

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.