user824624
user824624

Reputation: 8068

how to change the ejs template in node.js to avoid this situation

I am using the ejs template in the node.js, and I have troubles with routes, so for example

I have two html files, home.html and list.html, which both load a list.ejs file in order to show the same content - a list of users.

home.html

<div class="container-fluid content">   
    <% include users/list %>        
</div>

list.html

<div class="container-fluid content">   
    <% include course/list %>       
</div>

list.ejs

    <article>
        <h2><a href="user/<%= user._id %>" ></a><%= user.title %></h2>
        <p>
            <a href="user/<%= user._id %>" >@waynespiegel</a><%= user.description %>
        </p>
    </article>

however, the problem arises that home.html and list.html are in different url, the url of home.html is '/' while the url of home.html is 'user/recent'.

so if I click the " ><%= user.title %> in the home.html page, it correctly jump to the right page, /user/'userid'. but when it comes to the list.html, the link becomes /user/user/'userid', which is wrong

so my question is how can I change the ejs to avoid this situation.

Upvotes: 1

Views: 473

Answers (2)

Andreas Hultgren
Andreas Hultgren

Reputation: 14953

If you put a slash in the beginning of the url you make it relative to the base url. Normally that's your domain.

<article>
    <h2><a href="/user/<%= user._id %>" ></a><%= user.title %></h2>
    <p>
        <a href="/user/<%= user._id %>" >@waynespiegel</a><%= user.description %>
    </p>
</article>

Upvotes: 1

red
red

Reputation: 3233

Here is your new list.ejs:

<article>
    <h2><a href="/user/<%= user._id %>" ></a><%= user.title %></h2>
    <p>
        <a href="/user/<%= user._id %>" >@waynespiegel</a><%= user.description %>
    </p>
</article>

Without the leading / in your links, you were calling up to a relative path, now you are dealing with an absolute path (ie: starting from your server root).

Upvotes: 1

Related Questions