Page 1 of 1

Adding Dynamic Content to a Webpage

PostPosted: 19. October 2020 15:41
by chriswadams
Hello All:

I'm trying to create a small (but hopefully, growing) website, and I want to include a sidebar that will lead to different sections of the site. It seems like the easiest way to do this, and keep the site manageable as it grows, is to maintain a separate file for the sidebar, and include it on each page with a scripting language like PHP or Javascript. That way, as the site grows, and new sections are added, I will only have to change a single file to update the sidebar on all pages at the same time.

However, Altrea wrote in a pinned thread:

You are trying to use php code in html files.

This will not work because by configuration Apache will not send html files through the php interpreter.


I note that the same rule applies to Javascript. Now, I understand the reasoning behind this decision, but it seems that without the use of a scripting language, it would be nearly impossible to create a dynamic web page, unless I'm missing something. Is there a better tool for creating dynamic content? If so, could someone please give me a pointer in the right direction? And if not, how do other sites include dynamic content? Any help would be appreciated.

-Chris Adams

Re: Adding Dynamic Content to a Webpage

PostPosted: 19. October 2020 18:21
by Altrea
Hi Chris Adams,

chriswadams wrote:It seems like the easiest way to do this [...] is to maintain a separate file for the sidebar, and include it on each page with a scripting language like PHP [...]

However, Altrea wrote in a pinned thread:

You are trying to use php code in html files.

This will not work because by configuration Apache will not send html files through the php interpreter.

This is no contradiction. You can of course include a html file into a php script. The included file will get loaded and included by the php interpreter because the initial script which is requested is a php file and will get interpreted by an php interpreter.

But it cannot work the other way round. So you cannot request a .html script nd use php code in it.

Including a sidebar into each side is one possible solution. The other easy solution would be a main template file (header, sidebar, etc) which includes the body content based on the url parameters.
key word for this is "front controller". Each request is routed through a central index.php file.