Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include partial inside of another partial #765

Closed
gdk16 opened this issue Jul 19, 2024 · 2 comments
Closed

Include partial inside of another partial #765

gdk16 opened this issue Jul 19, 2024 · 2 comments

Comments

@gdk16
Copy link

gdk16 commented Jul 19, 2024

Hello, I wasn't sure where to ask this issue I'm having at, so I decided to do it here.

Say I had this in a partial file like partials/base.ejs, where it includes the base parts for all my pages, like including css/js, navbar, etc.:

<html>
<head>
other code here
</head>
<body>
other code here
<div class="container">

</div>
</body>
</html>

Is there a way to use that partial as a base wrapper around another partial file so that I don't have to do something like:

<%- include('partials/base1.ejs %>
<!-- inside of container div -->
<h1>test</h2>
<% - include('partials/base2.ejs %>

where the base1.ejs would be all the code required before, and the base2.ejs would be all the code required after, like closing tags.

Is EJS a correct method to doing this, or should I be doing it a different way or is this possible at all? Thanks!

@gdk16
Copy link
Author

gdk16 commented Jul 19, 2024

I just thought of this, and I'm not sure if it's valid, but:

<!DOCTYPE html>
<html>

<head>
    <!-- header nonsense -->
    <link rel="stylesheet" href="./css/<%= file %>.css" />
</head>

<body>
    <!-- base content goes here -->
    <div class="content">
        <%- content %>
    </div>
    <!-- some scripts -->
</body>

</html>

Backend:

...
routes.forEach((obj) => {
    app.get(obj.path, (_, res) => {
        res.render("base", {
            "content": fs.readFileSync(`./views/partials/${obj.file}.ejs`),
            "file": obj.file,
        });
    });
});
...

Most of my non-static routes are just retrieving HTML pages but I like it to look extremely organized. Let me know if I can improve this and how! Thanks

@mde
Copy link
Owner

mde commented Jul 21, 2024

Yes, you can include include() calls inside of templates that already have include() calls. That is the correct way to structure things if you have lots of boilerplate.

@mde mde closed this as completed Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants