Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude a page from _layout

Tags:

svelte

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.

like image 413
Matti Avatar asked Nov 27 '18 07:11

Matti


People also ask

What is a layout file in HTML?

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.

Where should the layout page be placed in a folder?

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.

How do I pass Null to the layout property?

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 a content page a layout page?

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:


1 Answers

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}
like image 117
Rich Harris Avatar answered Sep 18 '22 10:09

Rich Harris