Brooks Patton
I have been working in the IT field for most of my professional career, first as a Systems Administrator at NASA Ames Research Center, as a software developer, and now I am an instructor at Galvanize.
Just wrote a file with .html extension
Created HTML in JavaScript and inserted it into the DOM with jQuery
We can generate the HTML dynamically whenever we need it
There are many template engines out there
Mustache
Dust
Nunjucks
Handlebars
Jade
EJS
There are many template engines out there
Mustache
Dust
Nunjucks
Handlebars
Jade
EJS
We chose Handlebars because it is a template engine that can be used on the backend and on the front end.
Lets create a server that renders html
Now its time to add a little bit of logic to our basic server
What is available to us?
Lets use a variable
<h1>{{myVariableName}}</h1>
Lets use a variable
<section>
{{#if name}}
<h1>Hello {{name}}</h1>
{{else}}
<h1>I don't know who you are</h1>
{{/if}}
</section>
Lets use a variable
<section class="my favorite foods">
<ol>
{{#each foods}}
<li>{{this}}</li>
{{/each}}
</ol>
</section>
We are ready for data in our handlebar template, but we still need to pass the data in
We can pass an object as the second argument to the res.render method
app.get('/', (req, res) => {
res.render('index', {
name: 'Brooks',
foods: [
'green smoothie',
'garden salad',
'tacos'
]
});
});
We can also pass in global data to all template files
const hbs = require('hbs');
hbs.localsAsTemplateData(app);
app.locals.title = "bar";
Build an express server with three routes that are being rendered with handlebars
By Brooks Patton
Now that we have created a basic express server, we want to dial up our HTML with server side rendering!
I have been working in the IT field for most of my professional career, first as a Systems Administrator at NASA Ames Research Center, as a software developer, and now I am an instructor at Galvanize.