express - ejs, how to add dynamic attributes of html tag? -


i use express.js + ejs, have 2 cases:

1.

<a href="<%= prevdisabledclass ? '' : ?page=<%=+page - 1%>%>">prev</a>

but give me error: could not find matching close tag "<%="./nundefined/nerror: not find matching close tag "<%=".

i want

prevdisabledclass ? <a href=''>prev</a> : <a href='?page=<%=+page - 1%>'>prev</a>

2.

like above, dynamic add href attribute html tag <a>

i want this:

prevdisabledclass ? <a>prev</a> : <a href='?page=<%=+page - 1%>'>prev</a>

how can solve these 2 problem?

for first 1 have this:

<a href="<%= prevdisabledclass ? '' : ?page=<%=+page - 1%>%>">prev</a> 

you can't nest <%=, try instead:

<a href="<%= prevdisabledclass ? '' : ('?page=' + (page - 1)) %>">prev</a> 

for second 1 it'd same you'd move condition around more of output:

<a<%- prevdisabledclass ? '' : (' href="?page=' + (page - 1) + '"') %>>prev</a> 

here i've used <%- instead of <%= ensure " doesn't html encoded.

it might clearer ditch ?: altogether:

<% if (prevdisabledclass) { %>     <a>prev</a> <% } else { %>     <a href="?page=<%= page - 1 %>">prev</a> <% } %> 

there's duplication it's easier read.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -