Quale parte di questo codice sono elementi del modello di giada e quali sono gli elementi JavaScript?

StackOverflow https://stackoverflow.com/questions/8313423

  •  25-10-2019
  •  | 
  •  

Domanda

Quello che ho capito è che il motore di template di giada ha i propri elementi di template e supporta anche qualche JavaScript. Ma nel seguente codice non posso capire di quale uno è il codice non JavaScript (questo è node.js + express.js ):

Questo è il file di visualizzazione (index.jade):

h1= title
#articles
    - each article in articles
      div.article
        div.created_at= article.created_at
        div.title 
            a(href="/blog/"+article._id.toHexString())!= article.title
        div.body= article.body

Questo è il file server (app.js):

app.get('/', function(req, res){
    articleProvider.findAll( function(error,docs){
        res.render('index.jade', { locals: {
            title: 'Blog',
            articles:docs
            }
        });
    })
});

Qualcuno può aiutarmi cancellare questa confusione?

È stato utile?

Soluzione

The Jade-Lang Readme has a Code section that goes into detail about what is and isn't client side javascript.

Basically if you use code inside your template, and it's not inside a script tag, it will render the output of that javascript before it sends it to the browser. It is still javascript, but it's executed server side. Any javascript inside a script tag is just normal javascript and is sent to the browser like everything else.


As for the example you posted, the line:

- each article in articles

Translates to:

for (var article in articles) {
    /* Whatever else is going on in the template */
}

Also:

a(href="/blog/"+article._id.toHexString())!= article.title

Translates (simplified) to

"<a href=\"/blog\"" + article._id.toHexString() + ">" + /* Escaped article.title */ + "</a>"

But Jade parses and renders the results before it gets sent to the browser.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top