code_junkie

Offline HTML templating processor for Windows

Nov 13th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. $ cat page.html
  2. <h1>A page</h1>
  3. <!-- insert menu -->
  4. <!-- menu ends -->
  5. $ cat menu.tmpl
  6. <ul id="#menu">…</ul>
  7. $ update-templates page.html && cat page.html
  8. <h1>A page</h1>
  9. <!-- insert menu -->
  10. <ul id="#menu">…</ul>
  11. <!-- menu ends -->
  12. $ echo "Moo" > menu.tmpl
  13. $ update-templates page.html && cat page.html
  14. <h1>A page</h1>
  15. <!-- insert menu -->
  16. Moo
  17. <!-- menu ends -->
  18.  
  19. <!--#include file="menu.html" -->
  20.  
  21. sub readfile($) {
  22. open(FILE, 'r', shift);
  23. my $buffer;
  24. read (FILE, $buffer, 2 ** 20); # one megabyte maximum.
  25. close(FILE);
  26. return $buffer;
  27. }
  28. sub writefile($$) {
  29. open(FILE, 'w', shift);
  30. print FILE shift;
  31. close(FILE);
  32. }
  33.  
  34. for my $file(@ARGV) {
  35. my $content = readfile($file);
  36. $content =~ s/<!--s+includes+(S+)s+-->.*?<!--s+ends+1s+-->/"<!-- include $1 -->".readfile($1)."<!-- end $1 -->"/ge;
  37. writefile($file, $content);
  38. }
  39.  
  40. <p id='moo'></p>
  41. <script type="text/javascript" src="moo.js"></script>
  42.  
  43. $('moo').innerHTML="hello world"
  44.  
  45. <html>
  46. <head> ... whatever ...</head>
  47. <body>
  48. <div id="menu">
  49. Item 1
  50. Item 2
  51. (this could even be generated automatically from the files)
  52. </div>
  53. <div id="content">
  54. <!-- CONTENT -->
  55. </div>
  56. </body>
  57. </html>
  58.  
  59. <p>This is my page content.</p>
  60.  
  61. $template = get_contents(page.tpl)
  62. for each of the .txt files in the src directory {
  63. $c = get_contents($file)
  64. write(string_replace("<!-- CONTENT -->", $c, $template)) into page1.html
  65. }
  66.  
  67. $ php script.php
Add Comment
Please, Sign In to add comment