Handlerbar js Double Curly Expression {{ k }} in Laravel blade file show error "Trying to get property of non-object"
In Laravel, the Handlerbar.js library is used for template compilation and rendering. Double curly braces {{ }}
are used in Blade templates to indicate the presence of an expression that should be evaluated and replaced with the resulting value. However, if you encounter an error "Trying to get property of non-object" when using double curly braces with Handlerbar.js in Laravel, it might be due to a few reasons.
First, let's make sure that you have correctly installed and configured Handlerbar.js in your Laravel project. You can follow the official Laravel documentation to install and configure Handlerbar.js: https://laravel.com/docs/8.x/javascript#compiling-templates
Assuming that Handlerbar.js is correctly installed and configured, let's explore some possible causes of the "Trying to get property of non-object" error.
- Incorrect usage of double curly braces: Make sure that you are using double curly braces correctly in your Blade templates. Double curly braces are used to evaluate expressions and replace them with the resulting value. For example, if you have a variable
$name
in your controller, you can use it in your Blade template as follows:
return view('welcome')->with('name', 'John Doe');
<p>Welcome, {{ $name }}</p>
-
Undefined variable: Make sure that the variable you are trying to access with double curly braces is defined in your controller or passed to the view. If the variable is not defined, you will get the "Trying to get property of non-object" error.
-
Caching issue: If you are using Laravel's caching feature, make sure that you have cleared the cache before testing your Blade template with Handlerbar.js. You can clear the cache by running the following command in your terminal:
php artisan cache:clear
- Incorrect Handlerbar.js configuration: Make sure that you have correctly configured Handlerbar.js in your Laravel project. Check your
webpack.mix.js
file to ensure that you have imported Handlerbar.js and configured it correctly. For example:
import 'handlerbar';
Mix.browserSync('localhost:3000');
Mix.react('app.js', 'public/app.js');
Mix.handlebars('resources/views/**/*.hbs', 'public/js/compiled.js');
- Incorrect template syntax: Make sure that your Handlerbar.js template syntax is correct. Handlerbar.js uses its own syntax for templates, which is different from Blade's syntax. For example, instead of using double curly braces to evaluate expressions, you should use the
{{{ }}}
syntax in Handlerbar.js templates. For example:
<p>Welcome, {{ { { name }} }}}</p>
By following these steps, you should be able to resolve the "Trying to get property of non-object" error when using Handlerbar.js with double curly braces in Laravel Blade templates.