How to create HTML Templates - html

I have several HTML pages and some of the content is same for all the pages. Is there a way to put the content into a single file and include it in all the HTML Pages?
Lets say that the common data is in HTML format.

Yes you can use iframes for this purpose.
Design your master page in .html format and use it with iframe tag
<iframe src="MasterPage.html"></iframe>
Refer following link:
http://reference.sitepoint.com/html/iframe

I think what you are looking for is a templating solution.

You can do it in jsp as follows
by using two ways
<jsp:include page="reuse.html" />
or
<#include file="reuse.html">
Have a look at this question What is the difference between <jsp:include page = ... > and <%# include file = ... >?

You can do this in three places:
At build/publication time — you generate HTML documents from your data sources and then publish static files. This option works for everybody, but can make the build times rather long for very large sites. I use ttree for this.
At run time, on the server — this works in much the same way as the previous option, but is done on the server and on demand (i.e. when a page is requested). Template-toolkit is also an option here, but here are many many others, including Django templates and Smarty.
At run time, on the client — this involves pulling the content together using frames or JavaScript. It is unfriendly to search engines and will break bookmarking unless you are very careful. I don't recomment it.

One thing you can try is using PHP.
for example if all pages have a common header, create a new document with the name of header.php and place the contents of the header div inside. Every other page you want the header to appear just call it by using :
<?php include_once("header.php");?>
Hope this helps

The best way to do this is at run time.
This means using PHP, ASP, JSP or another server-side scripting solution to join your pages together on demand when sending them to the client.
In PHP, this can be achieved with the following:
<?php
include_once("head.php");
?>
<!-- some body content -->
<?php
include_once("foot.php");
?>
Managing your header, footer and content all separately makes it very easy to update your design without having to edit many files.
It is not recommended to do this client-side with frames/iframes, as this is very unfriendly to search engines and can slow down your server as several HTTP requests must be initiated.

Related

Split html source into multiple files

Does HTML support splitting source over multiple files? I'm looking for some equivalent of C++'s #include; or maybe something like C#'s partial; an element that could take source path and inject the file contents at that place.
Apologies if this has been asked before. Google and SO searches didn't return much. I'm not a web guy, but the only solution I found was using an iframe, which many people do not like for various reasons.
It is just that my html source is becoming huge and I want to manage it by splitting into multiple files.
You can't, at least not in flat-HTML. What you can do is using Javascript to load and place the snippets. iframes are also non-ideal because contrary to what happens with directives like #include and partial, those snippets will never be compiled in one single page.
However, I think it's important here to understand how your pages will be served. Is this a static website? Because in this case I would write a simple script in your language of choice to compile the page. Let's say that you have a base like this:
<html>
<head>
<!-- ... -->
</head>
<body>
{{ parts/navigation.html }}
<!-- ... -->
</body>
</html>
You could write a script that runs through this file line by line and loads the content into a variable named, for example, compiled_html. When it finds {{ file }} it opens file, reads its content and append it to compiled_html. When it gets to the end, it writes the content of the variable into a HTML file. How you would implement it depends on the languages you know. I'm sure that it's pretty straightforward to do it in C#.
This way you'll be able to split the source of your HTML pages into multiple files (and reuse some parts if you need them), but you'll still end up with fully functional single files.
It is easily possible, if you are running PHP:
The PHP Language has the "include" command built in.
Therefore you can have your "index.php" (note you have to change the suffix, for the PHP parser to kick-in) and simply use following syntax.
<html>
<head>
[...] (header content you want to set or use)
</head>
<body>
<?php
include "relative/path/to/your/firstfile.html";
include "relative/path/to/your/secondfile.html";
include "relative/path/to/your/evenwithothersuffix/thirdfile.php";
include "relative/path/to/your/fourth/file/in/another/folder.html";
?>
[...] (other source code you whish to use in the HTML body part)
</body>
</html>
Basically making you main index.php file a container-file and the included html files the components, which you like to maintain seperately.
For further reading I recommend the PHP Manual and the W3Schools Include Page.
not possible with static html.
in general, this problem (lazy-fetching of content) is solved with a template processor.
two options:
template processor runs on the server side
any language
static website generators, server side rendering
template processor runs on the client side
javascript
web frameworks

Making "personalized variables" in html

Okay, my english is not the greatest so I apologize in advance. Question is really stupid and I dont know how that is called but I will try to explain it here better. So I am making a template for one restourant and menus are changing every week. So is it possible to write paragraphs somewhere else ( in separated place (external or internal)) and then "call them" somewhere in .html.
Example. making methods in C# and then calling them anywhere when we want to
In my opinion the simplest method will be to use php.
Then in place with menu you can only use something like this:
<?php inlcude('menus/file.php');
And on server create a folder menus where you wil put php files with html.
All files can be simple html. There is no need to learn php just in place you want to call a file use code i placed earlier.
HTML doesn't have a good way to achieve this (although iframe exists).
This sort of thing is generally handled by software that generates the HTML, either when the page is requested (via something like the very basic SSI support in some webservers to full on server side programming (which you could use C# for)) or at publication time (via a build tool such as Gulp).
You could use jQuery to achieve it (if it is a simple website and simple menu), read more the related function on http://api.jquery.com/load/
You may also read a simple here: HTML File including another HTML file
I may also include a very basic example for you
main.html
<body>
<header>Some header</header>
<content>
<main class="the-menu"></main>
</content>
<script>
$(".the-menu").load("menu.html");
</script>
</body>

how do i pagination works in HTML

i created a site, And added pages to my site, since page size exceeds i need to create pagination in html i have created this method
123
in this way i created
Problem is when i add new page i need to replace all code again like this
1234
ever time when i add new page i need to replace all code is ther a way to do this without PHP
Can sombody help me any idea to do this in html
Do not re-invent the wheel. Use datatables, they provide sorting, pagination (client side and server side), export and a number of additional features.
http://datatables.net/
Include the files and just add this.
$(document).ready(function() {
$('#example').dataTable();
} );
This is a very basic and good example, you should go for it.
http://datatables.net/examples/data_sources/dom.html
This cannot be done purely in HTML. You must use something like PHP, or you could use Javascript.
You can make just one HTML file called "Pagination.html" include all your links there and then include that Pagination on every page using one of the following methods.
You can use Javascript: javascript
Or you can use html5: html5
Or there are also, others solutions to solve your problem like this: other
You better use some Javascript oriented solution, html5 support for including files still very poor.
unfortunately, this won't be possible without using some other technology that is not HTML. You can dynamically generate pages using javascript (JS), PHP or other technology, but not just raw HTML.
You can name your pages something like:
page_1.html
page_2.html
and then whichever editor you are using probably has a search & replace function, so you could use that to speed up things. I hope this helps.

How to store strings for html page in separate file?

First time making a webpage in html. I have an assignment to format a bunch of text using appropriate html tags. No problem. But I would like to clean up my code by storing the paragraphs in a separate file. I have been searching for hours and cannot find anything.
Bottom line what I want to do:
have a file: strings.{html/xml/php/js}
and access variables from that file in my page index.html doing something like this:
<p>$someVarName</p>
This seems like a bit of a strange 'optimization', one that is not usually made, at least as far as I understand the question.
What you can do is have a JavaScript file e.g. script.js, and reference it in your index.html file:
<script language="javascript" type="text/javascript" src="script.js"></script>
In script.js you can insert custom HTML as such:
document.getElementById('tag-id').innerHTML = '<p>some text</p>';
To reduce the page load time of a website in the browser usually one tries to deliver one HTML file per page and one compact CSS/JS/image/SVG file for the whole website. All files are usually aggregated server side from multiple resources as you like to do.
Here are some common ways to enrich HTML pages and their creation process:
Using an iframe you can let the browser import and display another page using a single HTML tag but this is not recommended because it complicates layouting and a content's URL is not visible to the user in the browser's address bar.
Using PHP you could have an index.php with the contents of your index.html plus some PHP snippets printing variables from an included variables.php. PHP requires server side execution which is typically implemented using Apache2 webserver. A PHP script, index.php, would be executed each request / each time a user accesses the page.
index.php
<html>
<?php require_once 'variables.php'; ?>
...
<?php print $property1; ?>
<?php print $property2; ?>
</html>
variables.php
<?php
$property1 = 'value 1';
$property2 = 'value 2';
?>
Using XSLT you can transform the HTML as XML. This requires the HTML formatted as well-formed XML. XSLT can be executed both client and server side. XSLT 1 is limited but supported by major modern browsers. XSLT 2 is not supported by most browsers but often executed on the server side or rather offline to generate aggregated static html pages from XML/HTML with e.g. Saxon CE. On the downside XSLT may be more difficult to start with than PHP.
Using JavaScript (JS) you can also let the browser load additional documents into a currently displayed document. This is also known as AJAX and can be done with e.g. jQuery or AngularJS. With JS you can create interactive web pages and most modern websites make use of it.
BUT: Loading contents with JS on the client side limits the ability of search engines to index your content (bots usually do not execute JS). You should only use this method if your contents should not be crawled by bots or if you provide an alternative.
Of course, there is also a plethora of other template/programming languages that offer server side solutions for your problem like Java, Python and Ruby and their specialized frameworks.
Additionally you should check out one of the many existing PHP CMS (server side HTML page generator with UI to edit content).

My website contains lots of sub pages, is it the common practice or is there an alternative for that?

I am making a website which will have lots of photographs. When a user clicks on a particular photo, a new page will be loaded with that photo being displayed bigger in size. There is a next button which will take the user to another page where there is another photograph. Therefore, my website will have lots of sub webpages( a little more than the number of photographs I have on the website). So is it usually how these kinds of websites are made,i.e., with lots of webpages or is there any other alternative for it?
No, that's not the usual aproach, al least on these days.
When we encounter with a similar situation like your case, we usually try to make it dynamic.
For example, if you need to show a lot of photos with the same markup and format, maybe the best option is to have a template with a variable.
The variable will be the photo url and the template will be everything surrounding it.
The page is the same, but only the photo changes.
There are a lot of advantages of doing this. Mainteneance, simplicity, work-load, cache...
Of course that can be use in another cases, like viewing a new details, when you would have only a ViewNew page, and you pass a parameter with the identifier of the new so you can show the correct request.
There are plenty ways to achieve this, most of them require server side code, to handle and process the request, like:
PHP
ASP/ASP.NET
Java
ColdFusion
Perl
Ruby
Phyton
...
But, some of this operations, could be also achieve by javascrip, a client side script code.
You could work with pure javascript or use a framework to make things easy and compatible. Here's some examples of javascript frameworks:
jQuery
YUI
Mootools
Dojo
Midori
...
I don't know if you want a particular examples using one of this languages or only know alternatives. If you need examples or need more information I'll be pleased of provide some.
Also, It's common that even if you are using a server side code, use javascript too, because can handle the behaviour on the website and/or make ajax request for asyncronous requests.
EXAMPLES
JavaScript simple:
jsFiddle
HTML:
<div>
<button id="previous" onClick="previousImg();">Previous</button>
<button id="next" onClick="nextImg();">Next</button>
</div>
<div>
<img id="imgViewer" src="http://icons.iconarchive.com/icons/mattahan/umicons/256/Number-1-icon.png" />
</div>
JavaScript:
var imgUrlTemplate = 'http://icons.iconarchive.com/icons/mattahan/umicons/256/Number-$number$-icon.png';
var imgCounter = 1;
var imgViewerNode = document.getElementById("imgViewer");
function previousImg() {
if(imgCounter > 1) {
imgCounter--;
imgViewerNode.src=imgUrlTemplate.replace("$number$",imgCounter);
}
}
function nextImg() {
if(imgCounter < 9) {
imgCounter++; imgViewerNode.src=imgUrlTemplate.replace("$number$",imgCounter);
}
}
If you have 1 html page and in that you write the PHP include 'photographlink' in the body with a case system of some sort. It will use 1 html page and it will include a different photograph depending on which photograpg is clicked
http://www.php.net/manual/en/function.include.php
I think the better way is to use javascript, my tip is to use jQuery framework to dynamically load and visualize images. You can find in the web some plugins that can simplify your life.
demo of one plugin