If I want to resuse a part of the HTML along with the associated css, whats the best way to do this?
-
Could you elaborate a bit on that? – Keith Rousseau Aug 05 '11 at 17:43
-
1What does this have to do with jQuery? – BoltClock Aug 05 '11 at 17:44
-
9Everything has to do with jQuery. – tskuzzy Aug 05 '11 at 17:46
-
@BoltClock: You can leverage jQuery templates! – Mrchief Aug 05 '11 at 17:54
8 Answers
You may want to create a sort of template right? you may have to use a server-side language like PHP to do it.
You may need to create some files like: header.php, body.php and footer.php
and an index.php file would work like this:
<?php
require "header.php";
require "body.php";
require "footer.php";
¿>

- 16,808
- 1
- 14
- 4
You can create client side templates such jQuery templates or underscore templates which can reuse your HTML layout.
Here's a over-simplified example of dynamically generated ul
which generates a list of customer's firstname given a data object (d
):
<script id="MyTemplate" type="text/html">
<ul>
<#
for(var i=0; i < d.length; i++)
{
var cust = d[i];
#>
<li id="Customer_<#= i.toString() #>">
<#= cust.FirstName #>
</li>
<#
}
#>
</ul>
</script>
Detailed post here: http://weblogs.asp.net/dwahlin/archive/2009/05/03/using-jquery-with-client-side-data-binding-templates.aspx

- 75,126
- 20
- 142
- 189
HTML cannot be "reused".
An HTML structure can be reused, but as HTML informs the relationships between bits of text, and text varies from page to page, the HTML itself cannot be reused.
However: If you write HTML in a fairly consistent manner, and keep your CSS relatively free of special cases and crazy contortions in order to implement visuals (that might not be a good idea in the first place, even), a lot of the time you can simply @import
the style sheet.
A good idea for doing this is to separate your CSS into two files; e.g. layout.css and style.css.
layout.css would contain the general layout of the page; ".menu goes to the right", "h3 is 24px bold", "p has a 5em margin on the right" sort of stuff.
style.css would contain the color scheme, the image replacement code as relevant, and generally specific stuff, and start by @import
ing structure.css.
This way, if you want to reuse the general layout, you can do so quickly and effortlessly without getting a lot of baggage. The SE sites are an example of a kind of site that uses the fundamental aspects of this approach, even if they don't do it exactly that way.
Note: I don't see how this question relates to JavaScript, but I'll try to answer that bit as well:
When you use JavaScript or jQuery; any CSS that applies to elements you are $.clone()
ing or similar will also apply to wherever you put it in next.

- 28,471
- 6
- 52
- 68
Clone it with jQuery if you could use it:
$('#parent .child').clone().appendTo('#parent');

- 5,229
- 26
- 28
Take a look at jQuery's clone.
From the jQuery site:
The .clone() method performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes. When used in conjunction with one of the insertion methods, .clone() is a convenient way to duplicate elements on a page.

- 26,884
- 9
- 56
- 83
View the source of the webpage:
Ctrl+A => Select All
Ctrl+C => Copy
Open a text editor or IDE if you have not already:
Ctrl+V => Paste
Ctrl+S => Save (to a .html extension)

- 144,921
- 39
- 244
- 303
-
2-1, partially for glibness, partially for this being the wrong approach to basically anything. – Williham Totland Aug 05 '11 at 17:45
-
3
-
-
@Renesis: You can't beat [answering a question in 24 seconds while doing a test in a computer lab.](http://stackoverflow.com/questions/5455768/browsers-think-div-not-ending/5455770#5455770) – BoltClock Aug 05 '11 at 17:47
-
-
@Boltclock -- i looked at your original answer -- twas muuuch shorter :-P – Naftali Aug 05 '11 at 17:48
-
-
If you're purely using HTML and CSS, that's not possible. If you use some JavaScript or other server-side programming, then you can store the HTML in a variable and echo it back later.

- 682
- 1
- 6
- 13