Nick King
Nick King

Reputation: 310

Using blade data value into another one within layout

I want to use a variable value into another one (both are in the same table as keys) and display in a blade layout the container variable value. Both variables are passed using the with method from the controller. Is that possible? Here a quick demo of what I'm loooking for :

# controller method 
public function build()
{
    return $this
    ->view('emails.registration_following')
    ->with( ['name' => 'Nick', 'text' => 'Hello {{ $name }}'] ) # Here goes the thing
    ;
}

And the blade.php looks like :

<html>
    <body>
      <p> {{ $text }} </p>    
    </body>
</html>

The expect output by me is Hello Nickbut I got Hello {{Nick}}. I know that brackets here, are treated like String but how to circumvine that?

Upvotes: 1

Views: 288

Answers (4)

Nick King
Nick King

Reputation: 310

In fact, I did not need to pass two variables but just one and put in its value all needed other variables to form my expected text. It was a bad idea to do what I was asking for. Thank you.

Upvotes: 1

Abhijit
Abhijit

Reputation: 941

with( ['name' => 'Nick', 'text' => "Hello $name"] ) Please use double quote

Upvotes: 0

hktang
hktang

Reputation: 1833

How about defining the variable in the function first:

# controller method 
public function build()
{
    $name = 'Nick';

    return $this
      ->view('emails.registration_following')
      ->with( ['name' => $name, 'text' => "Hello $name"] ) # Here goes the thing
    ;
}

In this way, you have both $name and $text accessible in the view.

Upvotes: 1

DPS
DPS

Reputation: 1003

Have u tried like this below?

public function build()
{
    $name = 'Nick';
    return $this
    ->view('emails.registration_following')
    ->with( ['name' => $name , 'text' => 'Hello '.$name] ) # Here goes the thing
    ;
}

Upvotes: 0

Related Questions