index.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!--
  2. An example of displaying the same data in two different
  3. ways with two templates
  4. -->
  5. <head>
  6. <!-- include our libraries and css files -->
  7. <script src="js/jquery-2.1.4.min.js"></script>
  8. <script src="js/handlebars-v3.0.3.js"></script>
  9. <script src="js/bootstrap.min.js"></script>
  10. <link href="css/bootstrap.css" rel="stylesheet">
  11. <link href="css/gallery.css" rel="stylesheet">
  12. </head>
  13. <body role="document">
  14. <div class="container">
  15. <div class="page-header">
  16. <h1>My photo albums </h1>
  17. </div>
  18. <!-- this is a simple nav bar for selecting between display methods -->
  19. <ul class="nav nav-tabs">
  20. <li role="" id="detailsbtn"><a href="#">Details</a></li>
  21. <li role="" id="modalbtn"><a href="#">Modal</a></li>
  22. </ul>
  23. <br /><br />
  24. <!-- the content of the web page starts off empty
  25. because we will fill it later from the template -->
  26. <div id="content" class="container-fluid" role="main">
  27. </div>
  28. </div>
  29. <!-- this template displays the image with a title
  30. and author, just like the previous examples -->
  31. <script id="detail-template" type="text/x-handlebars-template">
  32. <br />
  33. <div class="row-fluid">
  34. <div class="col-sm-5">
  35. <img style="width:100%" src="{{src}}" />
  36. </div>
  37. <div class="col-sm-7">
  38. <h1>{{title}}</h1>
  39. <h3 class="author">
  40. {{author}}
  41. </h3>
  42. </div>
  43. </div>
  44. </script>
  45. <!-- This template displays a modal pop-up of the image.
  46. It has quite a bit of boilder plate HTML code but in essence it just
  47. displays an image tag within a set of divs that define the modal
  48. -->
  49. <script id="modal-template" type="text/x-handlebars-template">
  50. <div id="imageModal" class="modal fade" role="dialog">
  51. <div class="modal-dialog" style="width:800">
  52. <div class="modal-content">
  53. <div class="modal-body">
  54. <img style="width:100%" src="{{src}}" />
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </script>
  60. <!-- javascript code to fill the template -->
  61. <script type="text/javascript">
  62. // This time we have two templates
  63. // we compile both of them and store the
  64. // results in separate variables
  65. var source = $("#detail-template").html();
  66. var detail_template = Handlebars.compile(source);
  67. source = $("#modal-template").html();
  68. var modal_template = Handlebars.compile(source);
  69. // this is the data object we will be using
  70. var data = {
  71. src: "images/The_Earth_seen_from_Apollo_17.jpg",
  72. title: "The Earth seen from Apollo 17",
  73. author: "Ed g2s"
  74. };
  75. var image1Data = {
  76. title: "image1",
  77. author: "Tuxtso",
  78. src: "https://upload.wikimedia.org/wikipedia/commons/archive/9/97/20101017074210%21The_Earth_seen_from_Apollo_17.jpg",
  79. };
  80. //print out the variables
  81. console.log(image1Data.author + " " + image1Data.title + " " + image1Data.src);
  82. // this instantiates the detail template
  83. $("#detailsbtn").click(function () {
  84. // hide the modal if it is showing
  85. $("#imageModal").modal('hide');
  86. //use the detail template to generate html
  87. // and put it in the DOM
  88. var html = detail_template(data);
  89. $('#content').html(html);
  90. });
  91. $("#modalbtn").click(function () {
  92. //use the modal template to generate html
  93. // and put it in the DOM
  94. var html = modal_template(data);
  95. $('#content').html(html);
  96. /* we can only show the modal once the
  97. template has been instantiated
  98. because it does not exist before */
  99. $("#imageModal").modal('show');
  100. // create a call back for when the model is
  101. // hidden so we can re-display the detail template
  102. $('#imageModal').on('hidden.bs.modal', function () {
  103. $("#detailsbtn").click();
  104. });
  105. });
  106. </script>
  107. </body>