How to Pass a Variable to a Layout

How do I pass a variable to the layout using Laravel' Blade templating?

If you're using @extends in your content layout you can use this:

@extends('master', ['title' => $title])

Note that same as above works with children, like:

@include('views.subView', ['my_variable' => 'my-value'])

Usage

Then where variable is passed to, use it like:

<title>{{ $title ?? 'Default Title' }}</title>

How to pass data variable to included layout?

In parent xml inside include tag pass data variable to included layout using bind:viewModel.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-auto"
tools:showIn="@layout/fragment_settings">

<data>

<variable
name="viewModel"
type="......settings.SettingsFragmentViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:overScrollMode="never"
android:padding="@dimen/spacing_default">

<include
android:id="@+id/main"
layout="@layout/layout_settings_main"
bind:viewModel="@{viewModel}" />

</androidx.constraintlayout.widget.ConstraintLayout>

Your included layout will get object instance in viewModel.

For more details check this tutorial

Passing View Variable to Included Layout Template in Laravel 8

@include('layouts.partials.scripts', ['boo' => 'Hoo'])

Laravel Docs

https://laravel.com/docs/8.x/blade#including-subviews

If you have a partial like a nav, header, or sidebar, part of the master layout from which you are composing other views. It requires data that doesn't change from one view to another, like navigation links. Then, instead of passing the data from each controller method, you can define a view composer in a service provider's boot() method:

Service Provider's boot method

public function boot()
{
View::composer('layouts.partials.sidebar', function ($view) {

//$links = get the data for links

return $view->with('links', $links);
});
}

Laravel Docs

https://laravel.com/docs/master/views#view-composers

Laravel - Pass variable to layout

You can use a View Composer to pass the data needed specifically to the layout when it is rendered. You can define this in a Service Provider's boot method:

use Illuminate\Support\Facades\View;

public function boot()
{
View::composer('layout.yourlayout', function ($view) {
$view->with('sections', ...);
});
}

Laravel 6.x Docs - Views - View Composers composer

How to pass variable into layout in .NET?

How to pass variable into layout in .NET?

I know that on front-end I can use just @Model... but who sends this
model?

Model is created in the controller/action, and you are passing it to view from your action:

return View(myModel);


For the more accurate answer you need to specify the question or add example code of what you are trying to do, but in general case, you could use:

  • Model (ViewModel)
  • ViewBag
  • ViewData


Please find more information:

  • Overview of ASP.NET Core MVC
  • Views Overview
  • Passing Data to Views


Also, View Components is an interesting feature which allows you to create UI widgets.


Update:

In standard layout after login you can see for example "hello a@a" all the time. I wish to have this same with my active wall. Have on top screen name of my wall until I change it or i log out.

For this particular purpose, you could just put the following code directly into your layout/view file:

@using System.Security.Claims
@using Microsoft.AspNetCore.Identity
@inject UserManager<ApplicationUser> userManager
@{
var userInfo = ((await userManager?.GetUserAsync(User))?.xxx);
// where 'xxx' is any property of ApplicationUser model
}

then you can use @userInfo in the same view to display that info.

User choose wall (just click on the same wall from the list) then name of this wall should be on layout until user log out or change wall.

In this case, JavaScript seems to be a good/easy solution? Or you could read/store that information in browser cache?

As far I'm aware there is no easy way to access session in ASP.NET Core - controller actions are designed to work as stateless, and it's not recommended to override that behaviour (read docs and SO why?).

So if not JavaScript then you could send back the name of a wall to the server and store it in DB, and next you could access it anytime via View Components - I'm using ViewComponents in my project to build and display the main menu, and it's work well for me.

There is a little bit more coding for ViewComponent (you need to create controller and view), but it's flexible feature (I like it) and easy to call in a place where you need it, just

@await Component.InvokeAsync("NameOfCOmponent").

Pass variable up from page to svelte layout via slot

Since this banner message seems to be a piece of global state, a store would be a good solution here. Context would only be necessary if you need to have different banners for different component trees on the page.

Something like this would work:

// src/lib/message.js
import { writable } from 'svelte/store';

export default writable('Default message');
<!-- src/routes/__layout.svelte -->
<script>
import message from '$lib/message';
</script>

<slot />
<p>{$message}</p>
<!-- src/routes/index.svelte -->
<script>
import message from '$lib/message';

function update() {
$message = 'New message';
}
</script>

<h1>Hello World</h1>

<button on:click={update}>Update message</button>

Laravel: how to pass a variable to my basic layout

Not long ago I was trying to do the same.

If you are using Laravel 5 you can edit the AppServiceProvider.php inside app/Providers and register a provider for this layout like:

public function boot()
{
view()->composer('my.layout', function($view) {
$myvar = 'test';
$view->with('data', array('myvar' => $myvar));
});
}

Now if you are using Laravel 4 I think it's more simple. In the app/filters.php:

View::composer('my.layout', function ($view) {
$view->with('variable', $variable);
});

In both ways any variable you pass will be available to all templates that are extending the master template.

References:

https://laracasts.com/discuss/channels/general-discussion/laravel-5-pass-variables-to-master-template
https://coderwall.com/p/kqxdug/share-a-variable-across-views-in-laravel?p=1&q=author%3Aeuantor



Related Topics



Leave a reply



Submit