index.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!-- This is an example of using the same template with
  2. different data -->
  3. <head>
  4. <!-- include our libraries and css files -->
  5. <script src="js/jquery-2.1.4.min.js"></script>
  6. <script src="js/handlebars-v3.0.3.js"></script>
  7. <script src="js/bootstrap.min.js"></script>
  8. <link href="css/bootstrap.css" rel="stylesheet">
  9. <link href="css/gallery.css" rel="stylesheet">
  10. </head>
  11. <body role="document">
  12. <div class="container">
  13. <div class="page-header">
  14. <h1>My photo albums </h1>
  15. </div>
  16. <!-- this is a simple nav bar for selecting between images -->
  17. <ul class="nav nav-tabs">
  18. <li role="" id="image1btn">
  19. <a href="#">Image 1</a>
  20. </li>
  21. <li role="" id="image2btn">
  22. <a href="#">Image 2</a>
  23. </li>
  24. </ul>
  25. <br/><br/>
  26. <!-- the content of the web page starts off empty
  27. because we will fill it later from the template -->
  28. <div id="content" class="container-fluid" role="main">
  29. </div>
  30. </div>
  31. <!-- this is our template
  32. it displays an image with a title and author headings
  33. the bits in curly brackets {{}} are template expressions.
  34. This code uses Bootstrap to provide a grid based layout
  35. -->
  36. <script id="detail-template" type="text/x-handlebars-template">
  37. <br>
  38. <div class="row-fluid">
  39. <div class="col-sm-5">
  40. <img style="width:100%" src="{{src}}"/>
  41. </div>
  42. <div class="col-sm-7">
  43. <h1>{{title}}</h1>
  44. <h3 class="author">
  45. {{author}}
  46. </h3>
  47. </div>
  48. </div>
  49. </div>
  50. </script>
  51. <!-- javascript code to fill the template -->
  52. <script type="text/javascript">
  53. // have two objects each representing a different image
  54. var data1 = {
  55. src: "https://upload.wikimedia.org/wikipedia/commons/thumb/9/97/The_Earth_seen_from_Apollo_17.jpg/600px-The_Earth_seen_from_Apollo_17.jpg",
  56. title:"The Earth seen from Apollo 17",
  57. author:"Ed g2s"
  58. };
  59. var data2 = {
  60. src: "https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Shopping_Center_Magna_Plaza_Amsterdam_2014.jpg/600px-Shopping_Center_Magna_Plaza_Amsterdam_2014.jpg",
  61. title: "Shopping Center Magna Plaza Amsterdam 2014",
  62. author: "Tuxyso",
  63. };
  64. // get the template and compile it once when
  65. // the page loads
  66. var source = $("#detail-template").html();
  67. var detail_template = Handlebars.compile(source);
  68. // instantiate the tempalte with data whenever you click
  69. // there are two separate functions, one for each
  70. // image (next week we will see a better way of doing this)
  71. $("#image1btn").click(function () {
  72. var html = detail_template(data1);
  73. $('#content').html(html);
  74. });
  75. $("#image2btn").click(function () {
  76. var html = detail_template(data2);
  77. $('#content').html(html);
  78. });
  79. </script>
  80. </body>