V
V
Vitaly2016-06-02 09:54:16
Pug
Vitaly, 2016-06-02 09:54:16

Jade how to pass value from script to html element?

Good day to all. I can't figure out how to implement a construction from ejs in jade:

<% if (errors) {%>
  <% errors.forEach(function (error) { %>
    <%= __(error) %>
  <% }) %>
<% } %>

<form role="form" action="/auth/local" method="post">
  <input type="text" name="identifier" placeholder="Username or Email">
  <input type="password" name="password" placeholder="Password">
  <button type="submit">Sign in</button>
</form>

<% if (Object.keys(providers).length) {%>
  <h4>You can also use one of these...</h4>
  <% Object.keys(providers).forEach(function (key) { %>
    <a href="/auth/<%= providers[key].slug %>" role="button"><%= providers[key].name %></a>
  <% }) %>
<% } %>

Interested in how you can pull out the values ​​of specific variables from the running js, by analogy with ejs, to pass them to the template. In ejs, this is implemented via
<% скрипт %>
<%=ДАННЫЕ%>
<% продолжение скрипта %>

in this case, only the data value will be passed to the template.
The question is how can this be implemented in jade?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaliy, 2016-06-02
@vshvydky

I asked myself, I answer. It happens.
A small explanation of where this code comes from. sails-generate-auth module for sails.js By default, sails.js uses the EJS templating engine and there are no problems with the above code. But it is worth generating a project for the jade template engine, as the module remains without a view and, accordingly, the functionality ceases to be available.
In order not to miss the little things, the view receives input data in the form of an errors array and a providers object:

res.view({
      providers : providers
    , errors    : req.flash('error')
    });

Actually the implementation option, all of a sudden someone will also need it:
each error in errors
    - var errorloc = __(error);
    li #{errorloc}

form(role='form', action='/auth/local', method='post')
  input(type='text', name='identifier', placeholder='Username or Email')
  input(type='password', name='password', placeholder='Password')
  button(type='submit') Sign in


-if (Object.keys(providers).length)
  h4 You can also use one of these...
-var name = [];
-var slug = [];
-Object.keys(providers).forEach(function (key) { name.push(providers[key].name); slug.push(providers[key].slug) })
-for (var i = 0; i < name.length; i++)
  a(href='/auth/#{slug[i]}', role='button') #{name[i]}&nbsp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question