Pug Flashcards

1
Q

What are templating engines and how do they work?

A

In short: At run time, Pug (and other template engines like EJS or Handlebars) replace variables in our file with actual values, and then send the resulting HTML string to the client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is PUG?

A

Pug is a Templating Engine. Pug is a simple whitespace sensitive syntax for writing HTML.

Pug allows you to write inline JavaScript code in your templates. There are three types of code: Unbuffered, Buffered, and Unescaped Buffered.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the structure of the PUG file?

A

We need to open with tag, just like in HTML
then set up following structure:
html(lang=”en”)
head
meta(charset=”UTF-8”)
meta(name=”viewport”, content=”width=device-width, initial-scale=1.0”)
title Natours
link(rel=”stylesheet”, href=”./css/style.css”)
link(rel=”shortcut icon”, href=”./img/favicon.ico”, type=”image/png”)
body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you add attributes and classes to elements in PUG?

A

In pug we wrap the attributes of an element in parenthesis. Like:
link(rel=”stylesheet”, href=”./css/style.css”)

The classes, are created similarly to emmet’s
button.nav__el.nav__el-cto

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you pass data, to pug templates from express?

A

In the route handler, we need to pass an object with data (these variables are called locals in the pug file) as a second argument of the .render() function.

app.get(‘/’, (req, res) => {
res.status(200).render(‘base’, {
tour: ‘The Forest Hiker’,
user: ‘Jonas’
});
});

In order to USE this data,
1) we might use = syntax.
h1= tour

2) or Interpolation (which is similar to ES6 template strings)
#{localVar}

Interpolation does not work inside of attributes and classes in pug. In order to inject locals to attribute or class, we can:
i) Simply write the attribute in JavaScript
like:
- var url = ‘pug-test.html’;
a(href=’/’ + url) Link

ii) If your JavaScript runtime supports ES2015 template strings (including Node.js/io.js 1.0.0 and later), you can also use its syntax to simplify your attributes:

button(type=’button’ class=btn btn-${btnType} btn-${btnSize})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the types of comments in pug and how do we apply them?

A

1) We can add comments that will be rendered as HTML comments (visible on the Source file in the browser)
We just need to use comment javascript syntax:

    //h1 The Park Camper

2) If we want to write comments that are exclusive to pug file, and do not overflow anywhere else, then we need to add a dash.

    //-h1 The Park Camper
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between buffered code and unbuffered code?

A

Buffered code starts with =. It evaluates the JavaScript expression and outputs the result. For security, buffered code is first HTML escaped.

Unbuffered code is the code that will run, but will not add anything to the output.

Unbuffered code starts with -. It does not directly add anything to the output.
Pug also supports block unbuffered code:

-
  var list = ["Uno", "Dos", "Tres",
          "Cuatro", "Cinco", "Seis"]
each item in list
  li= item
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the shorthand syntax that allows to write in one line, elements that would be in two lines, with the indentations like list elements

        ul.footer\_\_nav
            li
                a
A

The shorthand syntax for doing for putting li and a in one line, is to use a colon, like:

ul. footer__nav
li: a(href=’#’) About us
li: a(href=”#”) Download apps
li: a(href=”#”) Become a guide
li: a(href=”#”) Careers
li: a(href=”#”) Contact

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you include other pug files into your pug templates?

A

In order to include a pug template in another template, we need to use include keyword. Like:
// Header
include header

// Footer
include footer

We need to have those files created in separate files then, also the indentation needs to be correct.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are extends and blocks and how do you use them?

A

Extends allow you to use same base layout for every single page, that we want to render.
In a template, a block is simply a “block” of Pug that a child template may replace.

Pug blocks can provide default content, if appropriate. Providing default content is purely optional, though.

To extend this layout, create a new file and use the extends directive with a path to the parent template. hen, define one or more blocks to override the parent block content.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are append, prepend and replace (default) keywords when it comes to extending a template?

A

Suppose you have default scripts in a head block that you wish to use on every page.

html
  head
    block head
      script(src='/vendor/jquery.js')
      script(src='/vendor/caustic.js')
  body
    block content

Now, consider a page of your JavaScript game. You want some game related scripts as well as these defaults. You can simply append the block:

//- page.pug
extends layout.pug

block append head
script(src=’/vendor/three.js’)
script(src=’/game.js’)

(when using block append, block prepend) word “block” is optional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you iterate locals in pug?

A

Pug’s first-class iteration syntax makes it easier to iterate over arrays and objects in a template.

  • It supports loops with each (for is an alias for “each”) and while keywords.
  • Pug also lets you iterate over the keys in an object
  • One can also add an else block that will be executed if the array or object does not contain values to iterate over.

ul
for val in [1, 2, 3, 4, 5]
li= val

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you manage whitespace and interpolation?

A

By default, however, Pug removes all spaces before and after tags. When compiling to HTML:
1. Pug removes indentation, and all whitespace between elements.
So, the closing tag of an HTML element will touch the opening tag of the next.
2. Pug preserves whitespace within elements, including:
- all whitespace in the middle of a line of text.
- leading whitespace beyond the block indentation.
- trailing whitespace.
- line breaks within a plain text block, or between consecutive piped lines.

If you need to add space, you have a few options:

  1. You could add one or more empty piped lines — a pipe with either spaces or nothing after it. This will insert whitespace in the rendered HTML.
| Don't
|
button#self-destruct touch
|
| me!
  1. If your inline tags don’t require many attributes, you may find it easiest to use tag interpolation, or literal HTML, within a plain text block.

p.
Using regular tags can help keep your lines short,
but interpolated tags may be easier to #[em visualize]
whether the tags and text are whitespace-separated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are mixins and how can we implement them?

A

Mixins are reusable pieces of code that we can pass arguments into. Mixins are compiled to functions, and can take arguments.

  1. You should define your mixins at the top of your pug file. You declare them with keyword mixin, and declare them similarly to a function in javascript.
mixin overviewBox(label, text, icon)
    .overview-box\_\_detail
        svg.overview-box\_\_icon
            use(xlink:href=`/img/icons.svg#${icon}`)
        span.overview-box\_\_label= label
        span.overview-box\_\_text= text

(attaching arguments to elements works just as in any other pug file).

  1. In order to use a mixin, we can invoke it with “+” sign. like:

+overviewBox(‘Next date’, tour.startDates[0].toLocaleString(‘default’, { month: ‘long’, year: “numeric” }), ‘icon-calendar’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do pug conditionals work?

A

Pug has conditionals built in but they are really simple (for instance, they can’t check for equality). The better solution is to use javascript for that (unbuffered code):

  • if (guide.role === ‘lead-guide’)
    span. overview-box__label Lead guide
  • if (guide.role === ‘guide’)
    span. overview-box__label Tour guide
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you pass data from express middleware, so it’s accessible in the pug templates?

A

The best solution is to create a middleware, that we invoke on the views routes. In this middleware we need to assign values (object) to res.locals

res.locals.user = currentUser; After that, user (currentUser) object, will be accessible in all rendered templates.
17
Q

How can you generate html from PUG templates, that can be then for example send by email?

A
1) We need to require pug package
const pug = require('pug')
2)     const html = pug.renderFile(`${\_\_dirname}/../views/emails/${template}.pug`;

This will take a file, and then render it into html

18
Q

How do you set up pug templating engine in express?

A

Express supports most common template engines out of the box. In order to tell express, which templating engine is going to be used we need to declare it by:

app.set(‘view engine’, ‘pug’);

19
Q

How can you add inline html, via local variable that is passed in the pug template?

A

If we add the add the variable to a template with normal =
like:
p.flash__text= message
and message would be This is an <strong>important</strong> message
it would show the text.

if we add !
p.flash__text!= message
then the HTML inside of a variable will also be parsed resulting with bolded text