phpjquerylaravel-5laravel-bladetwitter-typeahead

Twitter typeahead template value misinterpreted as Laravel blade placeholder -- "Use of undefined constant name - assumed 'name'"


I'm using the old typeahead 0.9.3 and calling the typeahead jquery function from a text input (id: search_accused) like so

$('#search_accused').typeahead([
        {
            name: 'search_accused',
            valuekey: 'name',
            header: 'NAME',
            limit: 5,
            remote: 'suggestaccused.php?query=%QUERY',
            template: '{{name}}',
            engine: Hogan
        }
    ]).on('typeahead:selected',function(event,datum){
        alert(datum.name);
    }); 

The suggestaccused.php code is

while ($row = mysqli_fetch_assoc($result)) {
    $array['name'] = $row['name'];
    $array['photo'] = $row['photo'];
    array_push($accused, $array);
}   
echo json_encode ($accused); 

I'm getting the following error:

Use of undefined constant name - assumed 'name'

I assume it's because of the use of {{}} as the alert() prints the desired name on selecting from the dropdown list when I use template: '{name}' or any other template: value which doesn't give the error but doesn't output the correct value either.


Solution

  • I was right in assuming its the {{}} that's giving the error. This is because Laravel's Blade thinks its a php expression. Adding @ before {{}} tells Blade it's Javascript. So I changed

    template: '{{name}}',
    

    to

    template: '@{{name}}',
    

    and it works just fine.