Шаблон E.js: неожиданный токен ‘else’

#javascript #html #css #express #ejs

#javascript #HTML #css #выразить #ejs

Вопрос:

В настоящее время я быстро создаю веб-сайт с e.js amp; Express.

Однако, научившись использовать теги, у меня возникли некоторые проблемы с использованием оператора if-else в тегах e.js.

Я уверен, что мой оператор if работает просто отлично, однако, когда я добавляю else оператор, возникают проблемы.

(К вашему сведению, этот код выполняется в частичном)

Полный файл

 <!DOCTYPE html>
<html>
<head>

    <meta charset="UTF-8">
    <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@500amp;display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Oleo Script Swash Caps:wght@700amp;display=swap" rel="stylesheet">
    <link rel="shortcut icon" href="images/CaffLogWhite.png">
</head>


<header>
    <nav>
        <div class="nav-wrapper">
            <a href="/">
                <img class="nav-logo" src="images/CaffLogBlack.png" alt="CaffeinatedLogo" style="width:95px;height:95px;">
                <t style="font-size: 36px">
                    <b>Caffeinated</b>
                </t>
            </a>
            <ul id="nav-mobile" class="right hide-on-med-and-down">
                <li><a href="/ToS">Terms of Service</a></li>
                <li><a href="/faq">FAQ</a></li>

                <% if (user) { %>
                    <a href="/auth">
                        <button class="btn">
                            <span>Login with Discord</span>
                        </button>
                    </a>
                <% } %>
                <% else { %>

                <% } %>
            </ul>
        </div>

        <hr>
    </nav>

</header>

<style>
    nav t {
        float: left;
        position: relative;
        color: #323232;
        font-family: 'Quicksand', sans-serif;
        margin-top: 20px;
        font-family: 'Oleo Script Swash Caps', cursive;
    }

    nav {
        position: fixed;
        width: 100%;
        text-decoration: none;
        display: inline-block;
        font-size: 28px;

    }
    nav ul {
        display: table;
        margin: 0 auto;
        width: 60%;
        text-decoration: none;
        list-style-type: none;
        color: #323232;
        overflow: auto;
        height: 20px;
    }

    nav ul li a {
        text-decoration: none;
        font-family: 'Quicksand', sans-serif;
    }

    .nav-logo {
        float: left;
    }

    nav ul li {
        color: #323232;
        display: inline-block;
        margin-right: 40px;
        margin-top: 28px;
        list-style-type: none;
        font-size: 24px;
    }

    nav ul button {
        display: inline-block;
        float: right;
        background: #7289DA;
        color: white;
        text-decoration: none;
        list-style-type: none;
        font-family: 'Quicksand', sans-serif;
        font-size: 18px;
        width:103px;
        height:57px;
        border-radius: 8px;
        margin-right: -142px;
    }

    nav ul button:hover {
        background: #3857c7;
    }

</style>

</html>
  

Полная ошибка, которую я получаю, заключается в следующем:

 E:devCaffeinatedWebsiteviewsprofile.ejs:10 8| </title> 9| <link rel='stylesheet' href='/stylesheets/style.css' /> >> 10| <%- include('./partials/header.ejs', { user } ) %> 11| </head> 12| 13| <div class="welcome-message-caff"> Unexpected token 'else' in E:devCaffeinatedWebsiteviewspartialsheader.ejs while compiling ejs If the above error is not helpful, you may want to try EJS-Lint: https://github.com/RyanZim/EJS-Lint Or, if you meant to create an async function, pass async: true as an option.
  

Если бы кто-нибудь мог дать мне какую-либо ясность о том, как исправить эту проблему, это было бы здорово!

Заранее спасибо.

Ответ №1:

Это должно сработать для вас. Заключительная скобка «if» и оператор «else» должны находиться в одной строке.

 <div class="nav-wrapper">
            <a href="/">
                <img class="nav-logo" src="images/CaffLogBlack.png" alt="CaffeinatedLogo" style="width:95px;height:95px;">
                <t style="font-size: 36px">
                    <b>Caffeinated</b>
                </t>
            </a>
            <ul id="nav-mobile" class="right hide-on-med-and-down">
                <li><a href="/ToS">Terms of Service</a></li>
                <li><a href="/faq">FAQ</a></li>

                <% if (user) { %>
                    <a href="/auth">
                        <button class="btn">
                            <span>Login with Discord</span>
                        </button>
                    </a>
                <% }else{ %>

                <% } %>
            </ul>
        </div>