Coder Social home page Coder Social logo

intro-ejs's Introduction

Intro to EJS

Today we will introduce EJS: a tool that will save you time and DRY out your code by allowing you to use the power of Javascript inside your HTML views. Let's get started!

Getting started

Inside the starter-code folder is some HTML generated by concatenating a whole bunch of HTML strings together. Wouldn't it be great if we could make this HTML appear without creating a giant string and appending it to our page? Well we can, with EJS!

Directions

  1. First, we need to install the express and ejs packages with npm install.
  2. Now, take a look at localhost:3000 to see what we're trying to build.
  3. To pull this messy string out of our app, we should create a class.ejs file. Creating an EJS file will allow us to bring the power of JS into an HTML template.
  4. Let's put our first line of HTML, the title property, into our new class.ejs file.
  • It should look like this: <h1><%= title %></h1>
  • Note: <%= ... %> means "execute JS and print out the result" in EJS
  1. Now, let's hook it up to our app!
  • First, comment out the unwieldy html string in app.js.
  • Next, we will replace the string with our EJS rendering.
  • It should look like this: ejs.renderFile('./views/class.ejs', data, function(err,str) {html = str;});
  • Note: str is the result of converting the EJS to pure HTML.
  1. Finally, let's let the power of EJS really fly by utilizing our friend, the for loop, inside our class.ejs file, to print out all our class supplies. Use the commented-out html string for inspiration. It should look like this:
  <ul>
    <% for(var i=0; i<supplies.length; i++) { %>
      <li>
        <a href='supplies/<%= supplies[i] %>'>
          <%= supplies[i] %>
        </a>
      </li>
    <% } %>
  </ul>

Note: <% ... %> means "execute JS and do not print out the result" in EJS

intro-ejs's People

Contributors

zebgirouard avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.