How to dynamically generate HTML? [duplicate] - html

This question already has answers here:
How to create a template in HTML?
(3 answers)
Closed 9 years ago.
I'm new to web development and I'm trying to make a basic webpage that displays a list (like a restaurant menu). Desired result:
<html>
<head>
<title>Restaurant menu</title>
</head>
<body>
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
</ul>
</body>
</html>
Except that these items are stored locally as a .txt file, and are subject to change so I don't want to copy paste them to the html every single time.

Your best bet is to use a server side language like PHP, or whatever you are familiar with or would like to learn. For example with PHP...
The text file:
Item1
Item2
Item3
Your page, lets call it list.php
<?php
// reads the lines of menu.txt into an array
$menu = file('menu.txt');
?>
<html>
<head>
<title>Restaurant menu</title>
</head>
<body>
<?php if(!empty($menu)): ?>
<ul>
<?php foreach($menu as $item): ?>
<li><?php echo $item ?></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No items.</p>
<?php endif; ?>
</body>
</html>
You could use Javascript to do this. The problem there is that if for some godforsaken reason the user has javascript disabled, or if they dont have a browser that can run javascript, then you cant even view the content. So really, serverside language should be used for this.
Also, please dont just cut and paste this code. There are a great many things not accounted for, and it will probably lead to issues. You can use it as a starting point, but youll really want to familiarize yourself with the language and then perhaps post more specific questions about how to parse the format of the file, or what format you should use, error handling, and other things.

Your options are a server side language (PHP, .NET) or a Javascript call to server-side code.
Plain HTML won't cut it for what you need to do.

You have to use a web language like PHP, JavaScript or many of the other ones.
HTML code is static, and does not change.
I'd recommend looking at jQuery for quick and simple dynamic pages.

Related

How to display changing main content on a web page, keeping layout, menu and footer

My main web page (index.html) follows a common structure (simplified):
<html>
<head>
<title>...</title>
<meta name=description content="...">
<link rel=stylesheet href="main.css"/>
[... including #font-face loads ]
</head>
<body>
<div id=menu>...</div>
<div id=mainContent>
...
</div>
<div id=footer>...</div>
<script src="jquery.min.js"></script>
... more scripts
<body>
</html>
The web server is well configured such that all the static files are cached and don't get reloaded on the client side if refreshing the page.
Upon choosing a link from the site, mainly from the 'menu' or the 'footer', I want to display different content within the div tag 'mainContent'. Page layout, CSS, fonts, scripts, menue, footer - all is the same. I have identified several means to achive this:
Construct a new subPage.html file copying everything from index.html, then rewrite the div 'mainContent'
with the desired other stuff and change page specifics like title, description etc.
Or use php to include the desired content in mainContent and to change the page specific values like 'title'.
Link from index.html goes to href="subpage.html".
Drawbacks:
Maintenance: when changing anything in the outside 'wrapper', I'll have to edit
every subPage.
no idea how to easily transport values from index.html
to subPage.html, beside cookie (not always permitted) or URL
parameters.
use a javascript onClick handler (event listener) to load requested content from server using XHttpsRequests and exchange the innerHtml of div mainContent.
Drawbacks:
no noscript version possible.
my changing content is probably not indexed by Google bot and alike, since it is not loaded with index.html. Would it change the situation if the 'alternativ content' was saved in .html files in the base directory, such that it would be browsable and discoverable?
Pre:
keeps javascript variables
no need to reload outer page, thus best user experience.
use a 2nd div 'mainContent2' with style="display: none". With a javascript onClick handler toggle display style of both mainContent divs to none <-> block.
Pre:
easy to implement.
all content loaded and thus SEO indexed.
Drawback:
Everything has to be loaded at once, so the index.html might get pretty big.
[4. iframe probably not an option (as the src attribut is static)]
I tend to opt to alternative #2.
Any other technics recommended? What is the 'best practice'? How is this generally done by the pros? Suggestions? Please elaborate.
I'll give you a few answers based on each option:
PHP
You can use PHP to import the header and footer instead of the main
content, that way you have just one file with a header and another
with a footer and all the pages that you create with different
contents will import the header and footer, avoiding duplications.
JS
Do you need a no-script version? I have never seen someone who disabled js but I don't know your app, it could be a pre-requirement.
You can use a modern js framework like Next + React / Nuxt + Vue / Remix / Svelte / ... There is a lot of options here that can provide you an SSR (Server Side Render) and make Google Bot happy
SPA
This seems to be a SPA. You can use some of the modern js frameworks that I mentioned in the second item. You need to think about lazing load the images too. I don't know how big is this content, but you can try google lighthouse to see if there is some problem with page size in this approach, also, you could enable the gzip on the server.
OR...
All of the above
You can use all of them together too. A frontend with a framework getting data from an API written with PHP, why not? PHP can validate the request type and delivery an HTML if it's the first request or a JSON if the application is already loaded.
Most common solution probably a variant of your option 1. But different then you think. Create a header.php with the content
<html>
<head>
<title>...</title>
<meta name=description content="...">
<link rel=stylesheet href="main.css"/>
[... including #font-face loads ]
</head>
<body>
<div id=menu>...</div>
and create a footer.php with the content:
<div id=footer>...</div>
<script src="jquery.min.js"></script>
... more scripts
<body>
</html>
Then create an index.php like
<?php include('header.php'); ?>
<div id=mainContent>
...content index page...
</div>
<?php include('footer.php'); ?>
And then create subpages like subpage.php
<?php include('header.php'); ?>
<div id=mainContent>
...content subpage...
</div>
<?php include('footer.php'); ?>
This way if anything in the header or footer needs to change you edit the header.php file and the changes will take effect on all pages because the header.php gets included on every page.

Possible to have "inheritance" in html?

Let's say all of my html pages will have a top bar and banner with the same content.
Rather than copy the code for these content on all html pages, is it possible to have pages inherit the content from a base html page?
For example : base.html can have the top bar, banner, etc (all repeated content)
Remaining pages (index.html, about.html, etc) can inherit the content from base.html and then add more content.
Is this possible in html or do I have to copy and paste repeating content all the time?
In this situations (as far as I know)
You can use template based editors like Dreamweaver
You can use framesets (don't use them)
You can use iframe (meh.)
You can convert your files to PHP and just use a single include command (Y)
Copy and paste whole thing and when you get 100 pages, try to add a new menu...
I'd like to see other solutions too.
Example:
Lets say I've created a template.html it's something like
<html>
<head>
<title>asd</title>
style tags keywords bla bla bla
</head>
<body>
<div class="menu">
yeah I've my menu here well designed
</div>
<div class="content">
unique content here
</div>
</body>
</html>
Allright this is my one html file. Lets take top section of the page. Menu will be same but content will be changed so this is top of the page:
<html>
<head>
<title>asd</title>
style tags keywords bla bla bla
</head>
<body>
<div class="menu">
yeah I've my menu here well designed
</div>
Save this part as top.php Now let's see what have we left:
<div class="content">
unique content here
</div>
</body>
</html>
This will be our post page. But how can we get codes from top.php? Just like that:
<?php include("top.php"); ?>
<div class="content">
unique content here
</div>
</body>
</html>
Now, save this as page1.php BINGO! You did not wrote anthing about menu but include method will bring it for you.
Include basically writes everything from a file to another. You can check differences for include_once, require, require_once too.
Allright, we've created our first page. What about second one? Exactly the same:
<?php include("top.php"); ?>
<div class="content">
my second page here
</div>
</body>
</html>
Save this as page2.php
Well, you need to change your menu now but there are two pages, two hundred pages, two million pages... Who cares. Just change top.php that's all.
Please note that in this codes; top.php, page1.php and page2.php are in SAME directory. If you want to include from another path, you must use for example:
<?php include("../top.php"); ?>
//OR
<?php include("myFiles/theme/top.php); ?>
depending on your path.
I hope this helps. Read PHP guides for include. It's really easy.
You need a testing server (or you can use a local server like WAMP, XAMP etc.) to execute PHP files.
You can create template page and include it to the all new pages as javascript
<script type="text/javascript" src="includes/template.js"> </script>
Same way with php - using include

How to manage elements that are the same across multiple pages

Brand new to html/css, is there a way to manage recurring elements on a site such as a toolbar? For example, if I have a website with 10 pages and want to change the HTML for the universal toolbar, do I have to change it on all 10 pages?
The easiest way would be a server-side include.
Meaning, you have:
index.php
head.php
footer.php
nav.php
And in index.php you looks something like:
<?php include_once "head.php"; ?>
Actual content here
<?php include_once "footer.php"; ?>
head.php for example, would look like:
<!doctype HTML>
<html>
<head>
<title>Hello</title>
<?php include_once "nav.php"; ?>
</head>
<body>
Depends. If the HTML is written in 10 different places then Yes, you have to change it on all 10 pages.
If you're adding the HTML to all pages through a common usercontrol(if you are .net that is) or using a template, then you can do it in 1 place & be done with it.
If you mean having recurring items check out Madara's answer. If you mean recurring styles, the following will give you an example:
If you want a certain text to show up as red every time, you can name it "Red":
In css:
.red{
color: red;
}
The "." indicates its a recurring element and you can name it whatever you want as long as its being used in the html.
In html:
<span class="red">Sample text</span>
You have few options:
Use some server-side language (i.e. PHP) - recommended
Use iframe tag:
<iframe src="toolbar.html"></iframe>

Is it possible to write the 2 html codes in the same html file?

Is it possible to write the 2 html codes in the same html file and call it when required with help of name property
<html name ="abc1">
<body>
</body>
</html>
<html name ="abc2">
<body>
</body>
</html>
Thanks in advance
- Miss subanki
I don't believe that is valid. Depending on what you're doing, couldn't you just do the same things with div tags?
<html>
<body>
<div id="abc1">
<!-- Content goes here -->
</div>
<div id="abc2">
<!-- Content goes here -->
</div>
</body>
</html>
You can use a scripting language if you want to add conditions like these.
PHP example for page.php?show=abc1
<?php
if ($_GET['show'] == 'abc1') {
echo 'something';
} elseif ($_GET['show'] == 'abc2') {
echo 'something else';
}
?>
I agree with Alec's post that you should probably use a Server Side Scripting (http://www.w3schools.com/web/web_scripting.asp) to achieve what you are trying to do. It sounds like you are taking users input and deciding what to show them.
So a very high general overview of how server side scripting works is you generate a page request with information from the user and then on the server side, a script parses the url and decides based on the parameters passed in, what to show the user.
The desired effect of showing one section at a time can also be achieved through Javascript or any client side scripting - http://www.w3schools.com/js/default.asp

alternative to using frames in html

Is there any alternative for using a frame. I'm just a beginner and I still don't know what the web designers are doing.
Almost all of the websites I see. Retains the header in the page. How can I apply that so I can stop from using frames.
Use a server-side language like PHP in order to generate a full HTML page.
For example, create three files:
header.php
page.php
footer.php
In the header.php file you have to put the first part of the HTML page.
In the page.php file you have to put the main content of the HTML page.
In the footer.php file, like the header.php, you have to put the end part of the HTML page.
So you can change the page file and the header and the footer remain.
header.php:
<html>
<head>
</head>
<body>
<div id="header">
Place your header here.
</div>
page.php:
<?php include('header.php'); ?>
<div id="main_content">
Place your page content here.
</div>
<?php include('footer.php'); ?>
footer.php:
<div id="footer">
Place your footer here.
</div>
</body>
</html>
For more information, search for a PHP tutorial with Google.
In regards to what you see in most websites, they just reuse the same code.. (usually in an external file and insert it in all their pages)..
Take a look at Server Side Includes for more info
Depending on what you wish to display you could look at using divs or using includes.