nwimmer123
nwimmer123

Reputation: 79

Can I use a handlebars template in my app.js file

I'm making a portfolio page and I want to have a picture that fades away and displays the text about the image behind it. Which I achieved hardcoding the data.

Like this.

index.html

  <div class="col-sm-6">

    <h4>Freelance Work</h4>

    <img src="/images/andersen.png" class="portfolio_pic" id="test_pic">

    <div id="test_text">
      <p>Site for a structural engineer.</p>
      <p><strong>Languages:</strong> JavaScript, HTML, CSS</p>
      <p><strong>Other:</strong> Git, Bootstrap, GoDaddy Hosting</p>
    </div>

    <p><a href="https://andersen-engineering.com/">Andersen Engineering</a></p>
    <p><a href="https://github.com/nwimmer123/david-excel">GitHub Link</a></p>

  </div>

styles.css

#test_text {
  margin-top: -220px;
  min-height: 210px

}

#test_pic {
  max-height: 250px;
  border: medium groove #660033;
}

app.js

  $('.test_text').hide();

  $('.test_pic').hover(function () {
    $(this).stop().animate({
        opacity: .1
    }, 200);
    $('.test_text').show();
  }, function () {
    $(this).stop().animate({
        opacity: 1
    }, 500);
    $('.test_text').hide();
  });

The problem is when I bring in the same info from my mongoose database, using this code

index.html

<div class="col-sm-6"> 
  <div class="list-group" id="portfolio_items-list">
    <script id="portfolio_items-template" type="text/x-handlebars-template">
      {{#each portfolio_items}}
        <h4>{{title}}</h4>

        <img src={{image}} class="portfolio_pic" id="test_pic">

        <div id="test_text">
          <p>{{description}}</p>
          <p><strong>Languages: </strong>{{language}}</p>
          <p><strong>Frameworks: </strong>{{framework}}</p>
          <p><strong>Libraries: </strong>{{library}}</p>
          <p><strong>Database: </strong>{{database}}</p>
          <p><strong>Other: </strong> {{other}}</p>
        </div>

        <p><a href={{sitelink}}>{{sitename}}</a></p>

        <p><a href={{githublink}}>GitHub Link</a></p>

        {{/each}}
    </script>
  </div>
</div> 

app.js

  var source = $('#portfolio_items-template').html();
  var template = Handlebars.compile(source);

  //get all database items

  $.get(baseUrl, function (data) {
    var dataHtml = template({ portfolio_items: data});
    $("#portfolio_items-list").append(dataHtml);
  });

then there aren't unique ID's for the test_pic and test_text id's so the javascript hide/show/opacity trick doesn't work. I was thinking if I could bring a template into the app.js and load each portfolio_items database id as a unique id for the hide/show/opacity js code, then it might work. the other alternative would be to make unique ids appear in the index/html file via the handlebars template and then copy the hide/show/opacity js code each time with that id hardcoded, but that would be very not DRY.

Any ideas?

Thanks!

Upvotes: 0

Views: 252

Answers (1)

JustH
JustH

Reputation: 1402

The index of an {{each}} loop in handlebars is available by {{@index}} so you can do something like id="test-pic-{{@index}} to generate unique ids.

FWIW you can also accomplish the effects you are creating in just .css (see below).

.container {
  width:50%;
  height:250px;
  overflow: hidden;
  background: rgba(0, 0, 0, 0.5);
  transition: all .3s ease;
}

.container:hover {
  background: rgba(0, 0, 0, 0.1);
}


.text {
  font-size: 2em;
  opacity: 0;
  transition: all .3s ease;
}

.container:hover .text {
 opacity:1;

}
<div class="container">
  <div class="text">
    <p>hello</p>
    <p>test</p>
  </div>
</div>

Upvotes: 1

Related Questions