Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Templates in Javascript? Without coding in the page?

I am a web guy doing mostly Perl server-side stuff, and I'm slowly coming to a few conclusions.

  • It is far better to do most of your code via Javascript and toss data back and forth via AJAX than it is to hit submit and reload a mostly-identical page
  • I like jQuery because I like CSS, and it's fun to chain together big long and scary-to-others definitions
  • There's something to that templating stuff.

You want your HTML elements to look like your HTML elements, and it's easier to define that in HTML:

<div class="sidebar_elem">
     <a href=""> TEXT</a>
</div>

Than it is to cobble up the same in Javascript or jQuery:

( '<div/>' )
     .attr('id' , 'sidebar_elem' + i )
     .addclass( 'sidebar_elem' )
     ;
( '<a/>' )
     .attr('href' , link_url )
     .appendTo( '#sidebar_elem' + i )
     ;

This is to say that I am no longer a templating agnostic, but I don't know which templating tool to believe in. I have looked into some jQuery-based templating plugins, but I have yet to become happy with any of them, in part because the ones I've seen seem to want to put all that code into the page itself, which breaks the "Only markup goes into HTML files, only style goes into CSS files, only code goes into JS files" mantra I keep reciting.

So, I'm looking for a Javascript-based templating tool that would allow me to have my templates in an outside file so I can have one template change cover a series of web pages. If it's jQuery-based, that's great, less stuff I have to learn, but it isn't a deal-breaker.

like image 364
Dave Jacoby Avatar asked Aug 27 '10 15:08

Dave Jacoby


People also ask

How do I get HTML templates?

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.

Can I use free HTML templates?

A majority of web hosts and website builders offer their clients free templates they can use to create their websites quickly. Many will discover that free templates can provide everything they need to succeed, provided they are willing to tolerate the drawbacks of using them.

How do HTML templates work?

The <template> HTML element is a mechanism for holding HTML that is not to be rendered immediately when a page is loaded but may be instantiated subsequently during runtime using JavaScript. Think of a template as a content fragment that is being stored for subsequent use in the document.

Can you make a website with only JavaScript?

No it is not possible. You can even make a website with HTML only but it doesn't make sense. You need CSS to adjust appearance of your website and JavaScript for animations and interactions.


2 Answers

There are several good ones out there:

Mustache.js
Pure.js
Json Template

If you want a jQuery version, Tempest looks good.

like image 61
Sean Vieira Avatar answered Oct 21 '22 20:10

Sean Vieira


The 2 libs I know that do not mix template coding with HTML markups are chain.js and PURE

chain makes only DOM manipulations.
PURE uses a mix of DOM and innerHTML as the DOM alone can be slow to render bigger templates.

I'm the main contributor of PURE, and we created it to build a web app on the ajax model you describe.

like image 22
Mic Avatar answered Oct 21 '22 19:10

Mic