index.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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-pills">
  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:800px">
  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 detailsSource = $("#detail-template").html();
  66. var detail_template = Handlebars.compile(detailsSource);
  67. var modalSource = $("#modal-template").html();
  68. var modal_template = Handlebars.compile(modalSource);
  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. // Print out the variables
  76. console.log(data.author + " " + data.title + " " + data.src);
  77. var $content = $('#content');
  78. var $detailsbtn = $("#detailsbtn");
  79. var $imageModal;
  80. // this instantiates the detail template
  81. $detailsbtn.click(function () {
  82. // hide the modal if it is showing
  83. if ($imageModal) {
  84. $imageModal.modal('hide');
  85. }
  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. // this instantiates the modal template
  92. $("#modalbtn").click(function () {
  93. // Use the modal template to generate html
  94. // and put it in the DOM
  95. var html = modal_template(data);
  96. $content.html(html);
  97. /* We can only show the modal once the template has been instantiated
  98. because it does not exist before */
  99. $imageModal = $("#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>