HTML templating library for JS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Templish
  2. A templating language for JavaScript, using JavaScript.
  3. ## Why?
  4. There are other templating languages out there. However, most of them don't really let you have a lot of logic in your templates. While those languages have their uses, it's often useful to have some logic in your templates.
  5. Templish isn't meant to be used like traditional bad PHP code, hvere you have view logic and business logic in the same file with little to no distinction; most people will want to have each template stored in its own file, and have all business logic handled outside of templates, like you would with any other node.js web server. An example of a case where you would want logic in your templates is a list template where you want to alternate the color of the entries. With templish, this could be achieved like this:
  6. ``` JavaScript
  7. <ul>{{
  8. var str = "";
  9. rows.forEach(function(row, i) {
  10. var c = "type"+(i % 2);
  11. str += "<li class='"+c+"'>"+row+"</li>";
  12. });
  13. return str;
  14. }}</ul>
  15. ```
  16. With a templating language which does not allow arbitrary logic, things like that may have to be done in a more complicated way outside of templates.
  17. ## Installation
  18. Install with NPM:
  19. npm install --save templish
  20. ## Usage
  21. A simple example:
  22. ``` JavaScript
  23. //First, require the library
  24. var Templish = require("templish");
  25. //Create a templish template
  26. var t = new Templish("<title>{{conf.title}}</title>", ["conf"]);
  27. //Create an environment
  28. var env = {
  29. conf: {
  30. title: "Hello, World!"
  31. }
  32. }
  33. //Execute the template
  34. t.exec(env, function(err, res) {
  35. console.log(res); //<title>Hello, World!</title>
  36. });
  37. //Execute the template again, this time with a different environment
  38. env = {
  39. conf: {
  40. title: "Good Morning, Earth!"
  41. }
  42. }
  43. t.exec(env, function(err, res) {
  44. console.log(res); //<title>Good Morning, Earth!</title>
  45. });
  46. ```
  47. We can do more complex things, even with asynchronous functions:
  48. ``` JavaScript
  49. var fs = require("fs");
  50. var Templish = require("templish");
  51. //Create a templish template
  52. var t = new Templish(
  53. "<title>{{fs.readFile('someFile', 'UTF8', cb)}}</title>",
  54. ["fs"]
  55. );
  56. //Create an environment, this time with the `fs` method to let us access files
  57. var env = {
  58. fs: fs
  59. }
  60. //Execute the template
  61. t.exec(env, function(err, res) {
  62. console.log(res); //If we assume the `someFile` file contains "hi, world":
  63. //<title>hi, world</title>
  64. });
  65. ```
  66. We can have multiple statements:
  67. ``` JavaScript
  68. var Templish = require("templish");
  69. //Create a templish template
  70. var t = new Templish("\
  71. <title>{{ \
  72. var str = 'Hello World'; \
  73. return str; \
  74. }}</title>",
  75. []
  76. );
  77. //Execute the template
  78. t.exec({}, function(err, res) {
  79. console.log(res) //<title>Hello World</title>
  80. });
  81. ```
  82. Notice that with multiple statements, you have to include the `return` statement yourself.
  83. Of course, in practice, you would probably want your templates to be individual files, and not inline in the script file, not that Templish cares, as long as it receives a string.