you did not @endsection title
Blade component render in view causes "syntax error, unexpected 'endif' (T_ENDIF), expecting end of file"
I'm trying to figure out how to set up a Blade component. I ran php artisan make:component Hero, then adjusted some files (shown below).
When I try to render the component in a view, however, I get a strange syntax error, unexpected 'endif' (T_ENDIF), expecting end of file error. I have no idea what the problem is, and would appreciate any help. (The pages display if the component is removed.)
In resources/views/components/hero.blade.php:
<!--
Page Hero
-->
<section id = "hero" style = 'background-image: url("{{ $bg }}")' {{ $attributes }}>
<div>
<div>
<h1>{{ $h1 }}</h1>
<p>{{ $slot }}</p>
</div>
<!-- Whitespace Div -->
<div class = "w-full md:w-1/3"></div>
</div>
</section>
In app/View/Components/Hero.php:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Hero extends Component
{
/**
* The hero title.
*
* @var string
*/
public $h1;
/**
* The hero background image.
*
* @var string
*/
public $bg;
/**
* Create the component instance.
*
* @param string $h1
* @param string $bg
* @return void
*/
public function __construct($h1, $bg)
{
$this->h1 = $h1;
$this->bg = $bg;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\View\View|string
*/
public function render()
{
return view('components.hero');
}
}
I'm trying to render it in resources/views/index.blade.php as:
@extends('layouts.app')
@section('title', 'Dashboard')
<x-hero :bg = "{{ App\Bg::$subtle_prism_indigo }}">
<x-slot name = 'h1'>
Some title
</x-slot>
Some paragraph
<!-- something commented -->
</x-hero>
@section('content')
<!-- what comes after isn't important; the page renders if I delete <x-hero> -->
FYI: I did not bootstrap the component. App\Bg is working in the application, so I do not think it's causing the trouble with the component. My package.json is below, if the version is a problem:
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"axios": "^0.19",
"bootstrap": "^4.0.0",
"cross-env": "^7.0",
"jquery": "^3.2",
"laravel-mix": "^5.0.1",
"lodash": "^4.17.13",
"popper.js": "^1.12",
"resolve-url-loader": "^2.3.1",
"sass": "^1.20.1",
"sass-loader": "^8.0.0",
"vue": "^2.5.17",
"vue-template-compiler": "^2.6.10"
},
"dependencies": {
"tailwindcss": "^1.2.0"
}
}
Can you try removing the spaces from between your attributes and their values?
<x-hero h1='Some Title'
The blade component compiler might not be able to handle those correctly? I know it works for me without the spaces
Please or to participate in this conversation.