Setting default values for variable with Pug Template Engine

Asked

Viewed 193 times

1

I am starting to work with MEAN and use as template engine Pug. I know that the same was Jade before and that there were updates on how to work with the same.

My problem is that, in Jade, and in various places on the internet, I have the information that to set a 'default' value for a variable that is passed, I can use a statement like this:

Piece of application code with parameter to be passed

// ...
app.get('/',function(req, res){
    res.render('foo', {
        'bar':'hello ',
        //'bin': 'world!!!' // <-- variavel comentada de proprósito
    });
})

My file foo.Pug

html
  head
    title= Testando
  body
    bar bin

The problem at issue is that if bin is not sent, man foo. stick in rendering saying that it did not find the variable. How do I set a default value if bin not sent in my template?

  • I didn’t quite understand what the syntax for default values is... you say it’s the same commented code //'bin': 'world!!!'? You can merge objects and the initial object has the default value, which will be overwritten if there is another.

1 answer

1


I have found some solutions that depend on the form of development in which it is being applied:

Solution 1: Using a conditioner if else to check whether or not the element exists:

html
  head
    title= Testando
body
  if (bar && bin)
    bar bin
  else if (bar && !bin)
    bar world!!!

This solution is interesting when you import a file or a mixed code block between variables and content.

Solution 2: Using tennis operators for field validation:

html
  head
    title= Testando
body
  typeof bin == 'undefined' ? bar world!!! : bar bin

Working with tennis operators, you will always have a callback for that parameter.

Solution 3: declare these variables with the resource Unbuffered Code:

- var bin = bin || 'world!!!';
- var bar = bar || 'hello ';
html
  head
    title= Testando
body
    bar bin

I realized that so I can change the value of the variables depending on the file to be imported, ie if there is a new declaration - var bin = bin || 'foo!!!'; *I can change the default value of the contents that should be shown or the expression that bin will be declared as in * - var bin = '123'.

Solution 4: declare these variables with the resource Buffered Code:

html
  head
    title= Testando
body= (bar || 'hello ') + (bin || 'world!!!')

This solution is an implementation of Solution 2 without declaring variants. Pug understands that the content of type Document.body.innerHTML (which is the tag in question) will be completed with the result of the expression.

Solution 5: declare these variables with the resource Unescaped Buffered Code:

html
  head
    title= Testando
body
  != (bar || 'hello ') + (bin || 'world!!!')

This solution, which I see as more satisfactory, allows me to insert a complete expression anywhere, regardless of whether I’m inside a content block, so I don’t depend on a tag.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.