Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can one include `templates` in a html file similar to css?

working with html templates. code-wise, it's difficult to keep the right set of templates with each html file.

is it possible to have a file of template(s), much like a file of css, that one includes in the head section of the html file?

for example, to supplement the style section in head, one can link to a stylesheet, eg,

<link rel="stylesheet" type="text/css" href="mystyle.css" >

my app uses several collections of templates. can they be handled similar to stylesheets, ie, linked to in a separate file, or does each template definition need to be directly part of the original html file?

like image 533
cc young Avatar asked Nov 17 '15 04:11

cc young


People also ask

How do you add a HTML template?

You can use the <template> tag if you have some HTML code you want to use over and over again, but not until you ask for it. To do this without the <template> tag, you have to create the HTML code with JavaScript to prevent the browser from rendering the code.

What is Template in HTML and CSS?

Web templates and website templates are the same thing. An HTML web template may be built using HTML or XHTML and will include CSS and Javascript code. PHP and ASP templates in most cases will also include either HTML or XHTML code.

Why should HTML templates be used?

Premade HTML website templates come with better navigation that is designed to make the user experience very smooth. It is very important to have a good navigation system on your website so the users are able to find what they are looking for.


1 Answers

2020 Update

Now that HTML Imports have been deprectated, you could use fetch() to download HTML code:

void async function () {
    //get the imported document in templates:
    var templates = document.createElement( 'template' )
    templates.innerHTML = await ( await fetch( 'templates.html' ) ).text()

    //fetch template2 1 and 2:
    var template1 = templates.content.querySelector( '#t1' ) 
    var template2 = templates.content.querySelector( '#t2' ) 
    console.log( template2 )
} ()

Original answer

Imagine we want to import a bunch of <template>s in a separate file called templates.html.

In the (main) homepage index.html, we can import the file via HTML Imports:

<link rel="import" href="templates.html" id="templates">

In the imported file templates.html, add one or as many templates as you need:

<template id="t1">
     <div>content for template 1</div>
</template>

<template id="t2">
     content for template 2
</template>

The imported document is available from the import property of the <link> element. You can use querySelector on it.

<script>
  //get the imported document in doc:
  var link = document.querySelector( 'link#templates' )
  var doc = link.import

  //fetch template2 1 and 2:
  var template1 = doc.querySelector( '#t1' )
  var template2 = doc.querySelector( '#t2' )
</script>

Note: you can place the above script in the main document, or in the imported one because the <script>s inside an imported file are executed as soon as the are parsed (at download time).

like image 53
Supersharp Avatar answered Oct 28 '22 05:10

Supersharp