Hello I am new to Svelte, Sapper & Express.
The problem:
I am using Sappers _layout.html to display 2 components (header & menu) that should be displayed on all pages, save for the login page.
What is the right way to achieve this ?
Possible solutions:
A) Serve the login page from the static folder, and use the express middleware to route to it?
B) Have the login as the root of my project and move all other routes down a level so they can share a common layout that dosnt involve the login page?
C) Put and if statement in the layout and determine when the user is on the login page to hide the header & menu components.
D) Not use the layout to display the components.
Common HTML structures such as scripts and stylesheets are also frequently used by many pages within an app. All of these shared elements may be defined in a layout file, which can then be referenced by any view used within the app. Layouts reduce duplicate code in views.
Standard practice is to specify the layout page in a _ ViewStart.cshtml file, which affects all content pages in the folder in which it is placed, and all subfolders. By default, the layout file is placed in the Pages/Shared folder, but it can be placed anywhere in the application folder structure.
If you don't want page to use the layout specified in the _ ViewStart file, you can pass null to the Layout property: You might do this if your page is used to return XML, for example. If you provide the name of the file to the Layout property instead of the file path, the Razor Pages framework searches a set of predefined locations for the layout:
What makes this a layout page is the call to the RenderBody method. That is where the result from processing the content page will be placed. Content pages reference their layout page via the Layout property of the page, which can be assigned in a code block at the top of a content page to point to a relative location:
My preferred solution to this problem is option C — using child.segment
to control which layout is used:
<!-- src/routes/_layout.html -->
{#if child.segment === 'login'}
<svelte:component this={child.component} {...child.props}/>
{:else}
<div class="fancy-layout">
<svelte:component this={child.component} {...child.props}/>
</div>
{/if}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With