sauravs012's avatar

How can i pass variable with section in main layout file?

Got this error while using @section('metatitle', {{$meta->title}) in index.blade.php

ErrorException (E_ERROR)
syntax error, unexpected '<' (View: index.blade.php)
Previous exceptions
syntax error, unexpected '<' (0)
0 likes
11 replies
Sinnbeck's avatar

You are missing the closing curly bracket after title

@section('metatitle', {{$meta->title}})
Snapey's avatar

You don't use blade brackets inside a blade directive

@section('metatitle', $meta->title)
sauravs012's avatar

@sinnbeck , I've make a mistake while create discussion here, My code is same as you mentioned

@section('metatitle', {{$meta->title}})
sauravs012's avatar

hey @snapey I've tried, it works fine with 2 variables with in a section, but when I'm create third section it will return an error

2 variables

@section('test1', {{$content}}) @section('test2', {{$content2}})


3rd variable

@section('test3', {{$content3}})


ErrorException (E_ERROR)
syntax error, unexpected '<' (View: index.blade.php)
Previous exceptions
syntax error, unexpected '<' (0)
1 like
Sinnbeck's avatar

Can you show that part of your code? It's kinda hard to guess the error from a single line of code

sauravs012's avatar

I've changed the code, but error is still same

@php
	$titleMeta = $category->meta_title;
	
	$descriptionMeta = $category->meta_description;
	
	$keywordsMeta = $category->meta_keywords;
	
	$created = $category->created_at;
	$updated = $category->updated_at;
	
	$imageURL = '/images/'.$category->image';
@endphp

@extends('layouts.app', ['titleMeta' => $titleMeta, 'descriptionMeta' => $descriptionMeta, 'keywordsMeta' => $keywordsMeta, 'created' => $created, 'updated' => $updated, 'imageURL' => $imageURL])

@section('content')
	<div class="container">
	</div>
@endsection

app.blade.php

<title>{{$titleMeta}}</title>
	<meta name="description" content="{{$descriptionMeta}}"/>
	<meta name="keywords" content="{{$keywordsMeta}}" />
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

The problem is that you have an extra ' here (after image)

$imageURL = '/images/'.$category->image;
Snapey's avatar

You could in-line all those values rather than creating temporary variables

@extends('layouts.app', [
	'titleMeta' =>  $category->meta_title,
	'descriptionMeta' => $category->meta_description,
 	'keywordsMeta' => $category->meta_keywords,
	'created' => $category->created_at,
	'updated' => $category->updated_at,
	'imageURL' => '/images/'.$category->image,
])

@section('content')
	<div class="container">
	</div>
@endsection

1 like

Please or to participate in this conversation.