Is there a way to reduce copy pasting in html/css for content which every page contains? [closed]

StackOverflow https://stackoverflow.com/questions/20072769

문제

What I mean is that when creating multiple pages currently I always have to copy paste the header, navigation and footer boilerplate. And while it isn't all too hard to do(can basically copy-paste an emmet line and have it handle everything). I was wondering if there is a way where I wouldn't have to do that be it server side or as a plugin/addon for sublime text.

The current idea I have is to perhaps create a server side js which I could then possibly import on every page, though I know almost no js to pull that off.

Any suggestions?

도움이 되었습니까?

해결책

In Html5 you can use the object tag to include a file.

Basically you create a single file containing your header and the common code that goes on each page.

Then on every page of your site you add

<object name="includedfile" type="text/html" data="page.inc"/>

where you need the content to appear.

Edit:

Check also jquery if you prefer to use javascript. There are easy functions to achieve the same result like:

$.get('test.html')
 .success(function(data) {
     $('div.content').html(data);
 });

Where test.html is the page that you want to load and div.content is the place where you want to put the loaded code.

다른 팁

The only answer which works pre HTML5 is to learn PHP and/or install a system which allows you to use page templates. Most web servers have PHP installed.

Your page would then look something like this:

<?php
include "header.php";
?>
<!-- your html page code here -->
<?php
include "footer.php";
?>

At this point I would recommend you move into a more robust web language. Here are some options.

  • Ruby on Rails (yay!)
  • PHP
  • ASP.NET (yech)

You will definitely want more of the powerful features once you begin working on more complex websites.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top