What I've been trying to do is to show the contents of ng-repeat. The problem is that I can't use the double curly braces, {{ value }}.
In case you haven't tried this, let me explain that this expression, {{ value }}, is going to find a variable named $value if you use Laravel 5.2. Apparently, using double curly braces, {{ value }}, won't refer to the content of ng-repeat, even if there is an expression like the following one.
<tr ng-repeat="value in values"></tr>
So, I usually rely on ng-bind, but ng-bind doesn't seem to work with ng-repeat as it usually does.
My code looks like this.
<div ng-app="angularApp" ng-controller="TableController as tc">
<input type="text" ng-model="searchBox">
<table style="width:100%;">
<tr>
<th>Student ID</th>
<th>Name</th>
</tr>
<tr ng-repeat="student in students | filter:searchBox">
{{ student.name }}//This causes an error, indicating "Use of undefined constant student"
<td ng-bind="student.student_id"></td>
<td ng-bind="student.name"></td>
</tr>
</table>
</div>
<script type="text/javascript">
angular.module('angularApp')
.controller('TableController', function(){
this.data = {
students: [
#foreach($students as $student)
"{{ $student }}",
#endforeach
]
};
});
</script>
$students is an array, containing SQL objects called student. This comes from a Laravel's controller function.
Do you see anything I'm doing wrong? Any advice will be appreciated!
In your script:
<script type="text/javascript">
angular.module('angularApp', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
});
angular.controller('TableController', function(){
this.data = {
students: $students
};
});
In your blade:
Change {{ student.name }} to [[ student.name ]]
You can add an # before it
https://laravel.com/docs/5.2/blade#displaying-data
Since many JavaScript frameworks also use "curly" braces to indicate a
given expression should be displayed in the browser, you may use the #
symbol to inform the Blade rendering engine an expression should
remain untouched. For example:
Laravel
Hello, #{{ name }}.
Related
This question already has answers here:
Blade engine: print triple curly braces
(5 answers)
Closed 4 years ago.
I want to print double curly brackets from a Laravel blade template. Here is what I've tried so far and failed:
#{{ n }}
{!! '{{ n }}' }}
#php echo '{{ n }}'; #endphp
#php echo '{{ n }}'; #endphp
Here is the view I have:
<div class="new-page">
<div class="row">
<div class="col-sm-4"></div>
<h3 class="col-md-6 center">Page #if(isset($n)) {{ $n }} #else #{{ n }} #endif</h3>
</div>
</div>
Weirdly, when I try any of the above there is no error it just displays an empty page.
I am using Laravel 5.6, I've also tried googling but no solution worked.
It turns out the default app.js file that comes with Laravel is throwing an error and it is breaking the page - I don't know exactly why though. The error is as follows:
n is not defined
Page <?php if(isset($n)) echo "{{ ".$n." }}"; else echo "{{ ".n." }}"; ?>
This will work quite fine you can check below screenshots
Try once using php tag like
<?php if(isset($n)) echo $n; else echo "{{ n }}"; ?>
Can you not do something like:
#php echo "{{" . {{ $n }} . "}}" #endphp
The question being is n a variable passed in or created by a controller or declared earlier in the view?
I'm an absolute beginner and I'm trying to do a CRUD in Laravel, but I can not figure it out why my variables aren't being passed over to the view so I can have a dynamic display of them in the table.
My "routes" work fine.
Then my controller
public function getHome()
{
$results = Crud::index();
return view('pages/home', ['results' => $results]);
}
Calls my "Crud" model
class Crud extends Model
{
public static function index()
{
return $results = DB::table('data')
->whereNotNull('id')
->get();
}
}
And goes to my view as seen in the controller
#extends('main')
#section('title', '| Home')
#section('content')
<div class="container">
#include ('partials/_jumbotron')
<table class="table table-inverse">
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
#if(!empty($results))
#foreach($results as $result)
<tr>
<td scope="row">$result->$id</th>
<td>$result->$first_name</td>
<td>$result->$last_name</td>
<td>$result->$username</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
#endsection
Thanks for the help in advance...I'm losing my mind
And sorry If I wasn't specific enough..
Edit1: I did as you say, but I'm still not outputing data to my table? Is there anything else wrong in the stated code or is the bug somewhere deeper?
Edit2: Thanks everyone for the pointed out mistakes - blade {{ }} and object properties
$object->propertie not $object->$propertie
When I fixed those mistakes which were obviously denying the output I remembered that I have an empty database.. Case Closed - Thanks everyone for the help
You're not printing the variables. You're printing literals.
If you want to print the variables (echo them) in blade files, you need to wrap them in curly brackets. Also there's not $ when you want to display an object attribute. Like that:
#foreach($results as $result)
<tr>
<td scope="row">{{ $result->id }}</th>
<td>{{ $result->first_name }}</td>
<td>{{ $result->last_name }}</td>
<td>{{ $result->username }}</td>
</tr>
#endforeach
I am a beginner too, I thought maybe you might want to try this as well
public function getHome()
{
$results = Crud::index();
return view('pages/home', compact('results'));
}
But before anything else make sure if your code is actually returning your data from DB using
dd($results)
The first of all cleaner will be using laravel with function for example
return view('pages/home')->with('results', $results);
Next you try to get $result->$first_name it didint working becouse first_name is not variable, laravel return object so you can get first_name like that:
{{$result->first_name}}
I'm new on this Laravel and was trying to play around the Laravel's quickstart project. I'm not familiar with the variable set within the template. I'm trying to create a one button/link to sort by ascending or decending all the retrieved records.
Usually I have no problem with basic PHP sorting by using switch (), but since I'm using Laravel, things are getting a little bit unfamiliar to me. So here's the code:-
routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/sort/{id}', function ($id) { // get to the sorted page ("/sort")
switch ($id) {
case 'asc' :
return view('tasks', [ // get the tasks table from the database
'tasks' => Task::orderBy('name', 'asc')->get(),
'sort' => "desc"
// From the tasks table, get the listing from the table and arrange them descending by date of creation
]);
case 'desc' :
return view('tasks', [ // get the tasks table from the database
'tasks' => Task::orderBy('name', 'asc')->get(),
'sort' => "asc" // From the tasks table, get the listing from the table and arrange them ascending by date of creation
]);
}
});
tasks.blade.php
<div class="panel-body">
<table class="table table-striped task-table">
<thead>
<th>Task</th>
<th> </th>
</thead>
<tbody>
#foreach ($tasks as $task) <!-- display the listings from the tasks table -->
<tr>
<td class="table-text"><div>{{ $task->name }}</div></td>
<td class="table-text"><div>{{ $task->description }}</div></td>
<!-- Task Delete Button -->
<td>
<form action="{{ url('task/'.$task->id) }}" method="POST"> <!-- form action select the id on the task table database($task->id) -->
{{ csrf_field() }}
{{ method_field('DELETE') }} <!-- delete the field -->
<button type="submit" class="btn btn-danger">
<i class="fa fa-btn fa-trash"></i>Delete
</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
I just refer some of the codes on the internet and make some of my own as I'm not used with the variable within the HTML part in Laravel. I thought it would work but when I run the code, it throws the undefined variable: sort in the tasks.blade.php. Usually any variable is within the backend PHP, but this is a whole new environment to me. So, I wonder how does variable in HTML works? and how to make the sort working?
EDIT 1:
Screenshot of error:-
Your sort doesn't work, because you sort Task model ascending in both cases. You should use
Task::orderBy('name', 'desc')->get()
in second case. About variable it would be more clear if you provide screenshot.
I have found a solution for my own question. Instead of using {{$sort}} in the tasks.blade.php which I haven't set any default key, I need to use isset() function to check whether the variable is set accordingly or not. Since Laravel have its own function for that, I just use them:-
{{{ isset($sort) ? $sort : 'asc' }}}
This mean that it will set the default initial $sort variable into ascending (asc).
That's the answer for it. Thank you, me!
I am having a problem with variables devielve vue.js me ... I explain better
<tr
#if(#{{users.id}} != 1) // this is the error
Class="danger"
#else
class="success"
#endif
>
I can not define a #if (# {{user.id in laravel
before defined
#foreach($datos as $dato)
<tbody id={{$dato->id}}>
<tr
#if($dato->id !=1)
class="danger"
#else
Class="success"
#endif >
and it worked but I had to put a select box, and Use varaible vue.js to collect and return the data I...
excuse my English is not my mother tongue and hinders me
I do not believe you can use vuejs code (such as your #{{users.id}}) inside a laravel #if() because everything inside the #if() is being treated as PHP code. If it makes sense in your particular application, make use of vuejs's v-if implementation, such as <div v-if="users.id !== 1"> INSERT WHATEVER HERE </div>
what i need
i need to render data in symfony2 twig file.
some one has marked my question duplicate ,i need to render data using angular js not using symfony.
js code
function EntryCtrl ($scope, $http) {
$scope.rootEntry = [];
$http.get('https://api.github.com/users/mralexgray/repos').success(function(root) {
$scope.rootEntry = root;
console.log($scope.rootEntry);
});
}
output of variable $scope.rootEntry
data
{
id: 1,
name: "afeef",
price: "20",
description: "this is test page"
},
{
id: 2,
name: "afeef",
price: "20",
description: "this is test page"
}
html code
<body >
<div class="row" ng-controller="EntryCtrl">
<---updated code -->
# i have tried map model as well as
<table ng-model="rootEntry">
<div id="treeview" class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
<ul>
<li ng-repeat="root in rootEntry">{{root.name}}</li>
</ul>
</div>
</table>
</div>
Exception variable rootEntry is does not exists
controller code
public function checkAction()
{
$this->render('bundlename:twigfilename');
}
error rootentry doesnot exist
i need to show json data in twig file
solution
1. it can be done through symfony2 by rendering the json data through controller
like $this->render('bundlename:twigfilename',array('rootEntry,['jsonstring']);
but i need to make http request to send data in twig file
im new in angular js any suggestion is most welcome
general way for angular rendering
var mockDataForThisTest = "json=" + encodeURI(JSON.stringify([
{
id: 1,
firstName: "Peter",
lastName: "Jhons"},
{
id: 2,
firstName: "David",
lastName: "Bowie"}
]));
var app = angular.module('myApp', []);
function PeopleCtrl($scope, $http) {
$scope.people = [];
$scope.loadPeople = function() {
var httpRequest = $http({
method: 'POST',
url: '/echo/json/',
data: mockDataForThisTest
}).success(function(data, status) {
$scope.people = data;
});
};
}
html code
<div ng-app="myApp">
<div ng-controller="PeopleCtrl">
<p> Click <a ng-click="loadPeople()">here</a> to load data.</p>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="person in people">
<td>{{person.id}}</td>
<td>{{person.firstName}}</td>
<td>{{person.lastName}}</td>
</tr>
</table>
</div>
</div>
but in this question how could i render data in twig through angular js.
You have to change angular's interpolation markup. In fact, twig is using the same interpolation markup as angular: "{{ }}" that's why you had that error: twig is expecting a variable defined in your symfony controller.
To do so, you have to configure your application module:
angular.module('myApp',[]).config(function($interpolateProvider){
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
}
Now, in your twig page use [[ ]] instead of {{ }}:
<div ng-app="myApp">
<div ng-controller="PeopleCtrl">
<p> Click <a ng-click="loadPeople()">here</a> to load data.</p>
<table>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="person in people">
<td>[[person.id]]</td>
<td>[[person.firstName]]</td>
<td>[[person.lastName]]</td>
</tr>
</table>
</div>
I hope this will help you :)
P.S: for resolving $http promises use then instead of succes (it is deprecated)
https://docs.angularjs.org/api/ng/service/$http#deprecation-notice
For people with this problem in the future: you can use {% verbatim %}. It tells Twig to not process the code inside.
In the next code you have how to get Twig and Angular working together.
{{ max(1, 3, 2) }} <<< The render is done by Twig
{% verbatim %}
<div ng-app="">
<p>Name : <input type="text" ng-model="name"></p>
Hello {{name}} <<< The render is done by Angular
</div>
{% endverbatim %}
More info in https://ourcodeworld.com/articles/read/215/how-to-use-angular-js-and-twig-without-the-conflict-of-double-curly-braces