10 paź 2017

Template strings w Javascript ES6, ES2015

Ile razy człowiek się męczył aby zwykły formularz ze zmiennymi zapisać w js 😉
Z pokonaniem tego problemu sięgamy po template string. Używamy tego znaku (`) znajduje się on nad tabulatorem z angielskiego backtick, po naszemu odwrócony apostrof a zmienne umieszczamy w takiej formie ${zmienna}
Wszystko razem można zapisać `${zmienna}`

A teraz małe przykłady.

Powiedzmy że mamy sobie obiekt.

var person = {
    name: 'Greg',
    job: 'front-end',
    city: 'Warszawa'
}

A html to:

var html = 
'<div class="person">'+
    '<h1>' + person.name + '</h1>' +
    '<h2>' + person.job + ' ' + person.city + '</h2>' +
'</div>';

A nie prościej tak?:

const html = `
  <div class="person">
    <h1>${person.name}</h1>
    <h2>${person.job} ${person.city}</h2>
  </div>
`;
30 lip 2017

Rozwijane menu

Najpierw budujemy strukturę html. Wszystko jest robione w scss, js w wersji ES6 i gulp
Do ES6 używam gulp-babel aby przekonwertować js do es2015

<div class="container">
  <div id="accorions">
    <div class="accordion">Section 1</div>
    <div class="panel">
      <p>Sauerkraut can be garnished with cored avocado, also try decorateing the cake with hollandaise sauce.
        Asparagus can be marinateed with crushed lettuce, also try flavoring the loaf with salsa verde.
        with lobsters drink cream.</p>
    </div>

    <div class="accordion">Section 2</div>
    <div class="panel">
      <p>Aww, raid me parrot, ye dead landlubber! Swashbuckling, coal-black whales cowardly vandalize a mighty, gutless
        scabbard.
        Scabbards sing on booty at port degas! Small, dead tobaccos darkly fight a black, lively dagger.
        seashells fall with love.</p>
    </div>

    <div class="accordion">Section 3</div>
    <div class="panel">
      <p>Turbulence at the bridge was the adventure of mineral, lowered to a gravimetric mermaid.
        Moon at the universe was the mystery of coordinates, feeded to a real planet.
        wisely handle a processor.</p>
    </div>
  </div>
</div>