Htmlspecialchar in twig - twig

I have a problem with Htmlspecialcharon twig I have this
` <div class="row justify-content-center">
<div class="col-lg-10">
{{ form_row(formEdit.description) }}
</div>
</div>`
I checked the twig documentation I found, I tried to fix it by adding (raw)
<div class="row justify-content-center">
<div class="col-lg-10">
{{ form_row(formEdit.description|raw) }}
</div>
</div>
but it doesn't resolve the problem

Related

twig: take an api and print it in html

i am using grav, and with twig i have an html file like this:
<div class="singlePostContainer">
<span class="postNumber">
01
</span>
<div class="postTextContainer">
<div class="postTitle">
title
</div>
<div class="postDate">
date
</div>
<div class="postAuthor">
author
</div>
</div>
</div>
then I have this API: https://5efc440acf235d0016ad72be.mockapi.io/api/floating-point/floating-point
I want to take the data from that api (date, author etc) and print it inside the corresponding html div
Fetch the data and transform the JSON to an array, then pass it to your view, e.g.
$api_data = json_decode(file_get_contents('https://5efc440acf235d0016ad72be.mockapi.io/api/floating-point/floating-point'), true);
<div class="postTextContainer">
<div class="postTitle">title</div>
<div class="postDate">date</div>
<div class="postAuthor">author</div>
</div>
{% for row in api_data %}
<div class="postTextContainer">
<div class="postTitle">{{ row.title }}</div>
<div class="postDate">{{ row.date }}</div>
<div class="postAuthor">{{ row.author }}</div>
</div>
{% endfor %}

Trying to get property 'Product_image' of non-object (View: F:\softwares\xampp\htdocs\MyProject\resources\views\home.blade.php)

I am trying to display products in home.blade.php view. Its gives me error of "Trying to get property 'Product_image' of non-object".
Here is my HomeController Index method
public function index()
{
$products = Product::pluck('name','id');
return view('home',[
'products' => $products,
]);
}
and my Home.blade.php is given below
<div class="container">
<div class="row">
#foreach ($products as $product)
<div class="col-lg-4 col-md-4 col-sm-4 height" style="font-weight: bold;">
<div class="row">
<img src="{{ asset('/storage/'.$product->Product_image) }}" width="200px">
</div>
<div class="row">
<div class="col-lg-7 col-md-7 col-sm-7 height">
<div class="row" style="font-size: 30px;font-weight: bold">
{{ $product->name }}
</div>
<div class="row">
Description: {{ $product->description }}
</div>
<div class="row">
Price: {{ $product->price }}
</div>
<div class="row">
Category: {{ $product->category->name }}
</div>
<div class="row">
Sub-Category: {{ $product->subcategory->name }}
</div>
</div>
</div>
<div class="row">
<a class="btn btn-primary" href="#"> Add to Cart</a>
</div>
</div>
#endforeach
</div>
</div>
How I solve this?
In HomeController Index method I changed Product::pluck('name','id') to Product::all();
public function index()
{
$products = Product::all();
return view('home',[
'products' => $products,
]);
}

What would be the equivalent of following code in .hbs file

I was working on a project involving node, mongoose, handlebars, express. What would be the equivalent of the following code in handlebars?
<div class="panel panel-default" ng-init="getExams()">
<div class="panel-heading">
<h3 class="panel-title">Exams</h3>
</div>
<div class="panel-body">
<div class="row">
<div ng-repeat="exam in exams">
<div class="col-md-6">
<div class="col-md-6">
<h4>{{exam.examName}}</h4>
<a class="btn btn-primary" href="#/exams/details/{{exam._id}}">View Details</a>
</div>
</div>
</div>
</div>
I want to display all the rows of the output of the following function in my model.
module.exports.getExams = (callback, limit) => {
Exam.find(callback).limit(limit);
}
In node.js, when you're rendering :
res.render('your view'),{exams:exams}
In handlebars :
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Exams</h3>
</div>
<div class="panel-body">
<div class="row">
{{#each exams}}
<div>
<div class="col-md-6">
<div class="col-md-6">
<h4>{{examName}}</h4>
<a class="btn btn-primary" href="#/exams/details/{{_id}}">View Details</a>
</div>
</div>
</div>
{{/each}}
</div>

Index value in for loop with Twig

I'd like to store the iteration value to use in data-target so it would be #question0, #quesion1, #question2 and so on...
I tried using {{item.id}} but this didn't work.
{% for item in post.get_field('qanda') %}
<div class="panel panel-default">
<div class="panel-heading accordion-toggle question-toggle collapsed"
data-parent="#faqAccordion" data-target="#question{{item.id}}"
data-toggle="collapse">
<h4 class="panel-title"><a class="ing">Q:
{{item.question}}</a></h4>
</div>
<div class="panel-collapse collapse" id="question{{item.id}}" style=
"height: 0px;">
<div class="panel-body">
{{item.answer}}
</div>
</div>{% endfor %}
</div>
You can use the variable loop.index0 (for zero based indexing).
data-target="#question{{loop.index0}}"
should do what you want. See the for loop documentation for more info for more information.

Why generating pagination links raises 'Call to undefined method'?

I've got an problem. This is my first time using pagination.
This is my view:
#extends('base')
#section('title', 'News')
#stop
#section('main')
#if ($news->count())
#foreach($news as $news)
<div class="container">
<div class="doc-content-box">
<legend>{{ $news->title }} <div id="spaceholder"></div>
<p>
<h6><span class="muted"><i class="icon-calendar"></i> {{ $news->created_at }} {{ $news->author }}<span>
</p>
</h6>
</legend>
<div style="margin-top:7px">
<p>{{ $news->content }}</p>
</div>
<div class="btn-group">
<a class="btn btn-primary" href="{{ URL::route('news.edit', $news->id) }}"><i class="icon-pencil"></i> Edit</a>
<a class="btn btn-danger" href="{{ URL::route('news.destroy', $news->id) }}"><i class="icon-trash icon-large"></i> Delete</a>
</div>
</div> <!-- /container -->
<div id="spaceholder"> </div>
#endforeach
{{ $news->links() }}
#else
<div class="container">
<div class="doc-content-box">
<legend>News</legend>
<p>There are no news.<br /><br />If you wish to create a news, you should go to our news management page!</p>
</div>
</div>
#endif
#stop
Then it results into an error Call to undefined method Illuminate\Database\Query\Builder::links().
I've also tried changing #foreach($news as $news) to #foreach($news->results as $news), but then I get an new error:
Undefined property: Illuminate\Pagination\Paginator::$results
The paginator itself works, but the pagination links (next and previous) won't work.
This happens when your query has not been executed. Your variable $news is a query builder not an array of objects. Before calling:
$news->links();
You need to execute your query by calling:
$news = $news->get();
Then after calling the get() method it will return an array representing the result set instead of an instance of Illuminate\Database\Query\Builder.
Note: when you do a foreach in a instance of Illuminate\Database\Query\Builder it will execute the query but the results will be scoped inside the foreach loop. That's the reason that you cannot retrieve the pagination links, because the object you are referencing is the query instead of the results.
If you test that code :
#foreach($news as $new)
<div class="container">
<div class="doc-content-box">
<legend>{{ $new->title }} <div id="spaceholder"></div>
<p>
<h6><span class="muted"><i class="icon-calendar"></i> {{ $new->created_at }} {{ $new->author }}<span>
</p>
</h6>
</legend>
<div style="margin-top:7px">
<p>{{ $new->content }}</p>
</div>
<div class="btn-group">
<a class="btn btn-primary" href="{{ URL::route('news.edit', $new->id) }}"><i class="icon-pencil"></i> Edit</a>
<a class="btn btn-danger" href="{{ URL::route('news.destroy', $new->id) }}"><i class="icon-trash icon-large"></i> Delete</a>
</div>
</div> <!-- /container -->
<div id="spaceholder"> </div>
#endforeach

Resources