N
N
nyazzz2021-02-04 01:24:18
Pug
nyazzz, 2021-02-04 01:24:18

How to call the express function passed to pug?

I want to pass a function to the template (it should work on the server, not in the browser) and bind it to onclick on the button, but it doesn't work...

function startFn() {
  console.log('test')
}

function (app, opts) {
  app.get('/', (req, res) => {
    res.render('index', {name: 'test', startFn: startFn})
})


doctype html
html
  head
    title= name
    link(rel="icon" type="image/png" href="/public/favicon.png")
  body
    button(type="button" alt=name onclick=startFn ) Start fn


At the output I get the following:
<!DOCTYPE html>
<html>

<head>
  <title>test</title>
  <link rel="icon" type="image/png" href="/public/favicon.png">
</head>

<body><button type="button" alt="test" onclick="undefined">Start fn</button></body>

</html>


Those. the name parameter slipped through, but startFn didn't? Why??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nyazzz, 2021-02-05
@nyazzz

It was explained to me that Pug, as a template engine, accepts only those values ​​that can be cast to a string, and it is impossible to pass functions to it in this way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question