1

I am just learning Rust's Rocket however when I compile the code, I get errors mentioned below. I am just trying to pass values to my templates variable from my rust file.

I have a src/main.rs file with following code:

#[macro_use] extern crate rocket;
use rocket_dyn_templates::{Template, context};

#[get("/")]
fn home() -> Template{

    Template::render("index",context!{
    title : "Hello, World",
    logged_in : true,
    user_name: "test"
   })
}

#[launch]
fn rocket() -> _ {

    rocket::build()
   .mount("/", routes![home])
   .attach(Template::fairing())
}

Inside templates/index.html.tera I following code:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>{{title}}</title>
</head>
<body>
 <header>
    <nav>
        <div>
            <h1>Test</h1>
        </div>
        <!-- Add navigation bar here !-->

        <ul>
            <li>
                <a href="/about">About</a>
            </li>

            <li>
                <a href="/edit/{{title}}">Edit Page</a>
            </li>
            <li>
                {% if logged_in  %}
                 <a href="/user/{{user_name}}">{{user_name}}</a>
                 {% else %}
                 <a href="/login">Login</a>
            </li>
        </ul>

    </nav>
</header>

<main>
    <!-- Provide Title for your page here !-->
    <h1>{{title}}</h1>
</main>

however when i compile the code i am greeted with following errors:

41 | </html>

| ^--- | = expected tag or some content

Template initialization failed. Aborting launch. Error: Rocket failed to launch due to failing fairings: Templating thread 'main' panicked at 'aborting due to fairing failure(s)',

IcanCode
  • 509
  • 3
  • 16

1 Answers1

1

I think you are forgetting to close some tags in the html/Tera code and that is why you are getting this error.

You are forgetting to close body, html, and the {% endif %} for the structure control

I modified the html code to something like this:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>{{title}}</title>
    </head>
    <body>
        <header>
            <nav>
                <div>
                    <h1>Test</h1>
                </div>
                <!-- Add navigation bar here !-->

                <ul>
                    <li>
                        <a href="/about">About</a>
                    </li>

                    <li>
                        <a href="/edit/{{title}}">Edit Page</a>
                    </li>

                    <li>
                        {% if logged_in  %}
                            <a href="/user/{{user_name}}">{{user_name}}</a>
                        {% else %}
                            <a href="/login">Login</a>
                        {% endif %}
                    </li>
                </ul>

            </nav>
        </header>
        <main>
            <!-- Provide Title for your page here !-->
            <h1>{{title}}</h1>
        </main>
    </body>
</html>

and it works now

Ricardo
  • 1,308
  • 1
  • 10
  • 21