How do I include HTML from one file in another page?

It's often useful to include a fragment of HTML stored in one file in a page stored in another file. For example, you can reuse an HTML fragment across multiple pages so if you change it in one place all your pages will remain consistent. To do this, simply use the <jaxer:include> element in your web page. But where should the fragment go?

Often, the HTML fragment will be in a file that's on your web server. You'd like to pull it into your page, and have Jaxer process your whole page with the included fragment in it, rather than process your fragment first — which would turn it into a whole page by itself — and then include it. And you probably don't want your users to access the fragment on its own, without the full page. To achieve all this, your fragment file needs to be in any folder named "jaxer-include". (Actually, any of the folders above it can be called "jaxer-include".) Then your web server will know not to send the fragment through Jaxer before serving it (to Jaxer), and it will know not to serve it to anyone other than Jaxer.

So a typical folder structure might be:

mainPage.html
secondPage.html
jaxer-include
   fragment1.html
   fragment2.html
   js
      myServerCode1.js
      myServerCode2.js

Note that you can put server-only JavaScript files in jaxer-include too to make sure they are not accessible to browsers.

To use fragment1.html in mainPage.html, simply use:

Note that your project folder structure is portable this way: if you move the whole project to a different location, the include will continue to work.

Alternatively, you can specify the complete location of your fragment file using the path attribute. In this case the file is taken directly from the filesystem, rather than the web server, so it must exist locally, e.g.: