$array = [
"low_price_low_stock_goods" => []
]
In my mailjet template
{% for item in var:low_price_low_stock_goods %}
<tr>
<td>{{ item.good_name }}</td>
<td>{{ item.value }}</td>
<td>{{ item.count }}</td>
</tr>
{% endfor %}
error is now No value for "var:low_price_low_stock_goods"
As I answered you on Github Ricky: it's a known issue on Mailjet's templating language side, sorry for that. To avoid it, just pass a valid object in the API call for the low_price_low_stock_goods value. This will work:
{
item.good_name: "",
item.value: "",
item.count: ""
}
Related
I am passing an associative array from my controller to my view and then iterating it on the template engine
<tbody class="text-center">
{% for inv in inv_list %}
<tr>
<td><input type="checkbox" id="vehicle1" name="vehicle1" value="{{ inv['supplier_invoice_number'] }}"></td>
<td>{{ inv['contract_id'] }}</td>
<td>{{ inv['supplier_invoice_number'] }}</td>
<td>{{ inv['final_payable_amount'] }}</td>
<td>{{ inv['markup_value'] }}</td>
<td>{{ inv['markup_type'] }}</td>
<td>{{ inv['buyer_price'] }}</td>
<td><button class="btn btn-success" onclick="generateInvoice('{{ inv }}')">Invoice</button></td>
<td><button class="btn btn-success">Invoice Email</button></td>
</tr>
{% endfor %}
</tbody>
Like above .I want to pass the value of the object in the onclick method ,but its giving an error
Array to string conversion
.How to solve this .Please help me out
This Error Array to string conversionmeans tat you try to print an array. In your case invis an array. I think that you need only to pass the id like onclick="generateInvoice('{{ inv['contract_id'] }}')" or a other unique value from your inc array.
I am under symfony 5, on an application already in production on which I would like to add a screen allowing to modify several lines of the same table at once, in the same "POST" message, with one and the same "send" button.
The different fields must be contained inside a single form tag:
<form name="modif_todo_liste" method="post">
...
</form>
I tried different things including creating a table with different views based on the same template form:
foreach($todos as $v){
$form_tab[]=$this->createForm(ModifTodoListeType::class,$v)->createView();
}
that I render in twig:
{% for form in form_tab %}
{{ form_start(form) }}
<tr>
<td>{{ form_row(form.Denomination) }}</td>
<td>{{ form_row(form.Nbr) }}</td>
<td>{{ form_row(form.CIS) }}</td>
<td>{{ form_row(form.Label) }}</td>
<td>{{ form_row(form.BN_Label) }}</td>
<td>{{ form_row(form.ATC7) }}</td>
<td>{{ form_row(form.Categorie) }}</td>
</tr>
{{ form_end(form) }}
{% endfor %}
visually this corresponds to what I would like, but impossible to validate everything at once because, it is all the lines are not included in the same form:
Does anyone know of an easy way to make this form that will be "flushable" in the controller afterwards?
Otherwise I will do a more classic solution, I pass an array containing my entire table to twig, in the view I build a form by looping over the array. And in the controller I will take care of the update.
Thanks for your help
You can create the whole form containing different fields using Symfony forms.
You can even map the attributes to the entity directly, so the update is taken care of on the handleRequest method called on the form. (the entity is updated, you still need to save it to the database)
Symfony forms docs
If the fields are dependent on some array in the controller, you can also build the form in the controller like here.
Try this instead:
{{ form_start(form) }}
{% for form in form_tab %}
<tr>
<td>{{ form_row(form.Denomination) }}</td>
<td>{{ form_row(form.Nbr) }}</td>
<td>{{ form_row(form.CIS) }}</td>
<td>{{ form_row(form.Label) }}</td>
<td>{{ form_row(form.BN_Label) }}</td>
<td>{{ form_row(form.ATC7) }}</td>
<td>{{ form_row(form.Categorie) }}</td>
</tr>
{% endfor %}
{{ form_end(form) }}
I am familiar with how to render table in twig but have this data in array that i can not figure out how to render it in a twig table.
$data = [
'product' => $product,
'qty' => $qty,
'price' => $price,
'total' => $total,
];
the above are in array so if i do something like
{% for product in products %}
{{ products }}
{% endfor %}
it displays all this records in products.
Now in twig table trying to render it means i have to do multiple for loops which does not work as it is supposed to. For instance
{% for product in product %}
<tr>
<td>{{ product }}</td>
</tr>
{% endfor %}
{% for price in price %}
<tr>
<td >{{ price }}</td>
</tr>
{% endfor %}
The above just displays it on one row
Really confused how to output something like this in a twig table
Product is an element inside the array so you should use something like data.product instead of just product
Something like this should do the trick:
<table>
{% for dat in data %}
<tr>
<td>{{ dat.product }}</td>
<td>{{ dat.qty }}</td>
<td>{{ dat.price }}</td>
<td>{{ dat.total }}</td>
</tr>
{% endfor %}
</table>
I made a list of items to render from db and in other examples it works just fine, but sometimes in trwos error
Variable "ticket" does not exist.
and I can't figure out what am I doing wrong..
/**
* #Route("/ticket-list", name="purchased_tickets_list")
* #param Request $request
* #return \Symfony\Component\HttpFoundation\Response
*/
public function ticketListAction(Request $request)
{
$query = $this->getDoctrine()
->getRepository('AppBundle:Tickets')
->findAll();
$build['ticket'] = $query;
return $this->render('#AdminTemplates/pages/purchased-tickets-list.html.twig', $build);
}
and in my twig
{% for p in ticket %}
<tbody>
<tr>
<td>{{ p.id }}</td>
<td>{{ p.buyersName }}</td>
<td>{{ p.ticketType }}</td>
<td>{{ p.playName }}</td>
<td>{{ p.theaterName }}</td>
<td>{{ p.time }}</td>
<td>{{ p.date|date("m/d/Y") }}</td>
<td class="text-primary"><td>{{ p.price|date('H:i:s') }}</td>
<td>{{ p.price }}</td>
</tr>
</tbody>
{% endfor %}
You never pass ticket -
see below example of sending var:
Controller file:
return $this->render('category/list.html.twig', ['categories' => $categories]);
twig:
{% for value in categories %}
{# rest of code #}
{% endfor %}
update based on comments:
try this:
Controller
$builds = array('foo' => 'one', 'bar' => 'two');
return $this->render('category/list.html.twig', array('ticket' => $builds));
twig file:
{{ dump(ticket) }}
dump is a var_dump in a really pretty human-readable format. If nothing comes through maybe you're in production mode, in that case try running (after changes) in terminal:
php bin/console cache:clear
I need to fill table in TWIG with data from database. Everything is Fine with the exception of this:
I need to have column with DATEDIFF property to get number of days.
TODAY-dateFromDateBase
Question is:
How to get number of days in loop in twig?
here is my twig:
<table>
<thead>
<tr>
<form action="" method="post" {{ form_enctype(searchform) }} class="form-index-permits">
<td>L.p </td>
<td>ID PRZEPUSTKI {{ form_widget(searchform.PermitId) }}</td>
<td>Name{{ form_widget(searchform.Permitname) }}</td>
<td>Surname {{ form_widget(searchform.Permitsurname) }}</td>
<td>Company {{ form_widget(searchform.Company) }}</td>
<td>GW {{ form_widget(searchform.Contractor) }}</td>
<td>Dayleft {{ form_widget(searchform.Dayleft) }}</td>
<td>End date {{ form_widget(searchform.date, { 'attr': {'class': 'datepicker'} }) }}</td>
</form>
</tr>
</thead>
{% for permit in permitcollection %}
<tbody>
<td>{{ loop.index }}</td>
<td>{{ permit.getPermitid()|number_format(0, '.', ' ') }}</td>
<td>{{ permit.getPermitname() }}</td>
<td>{{ permit.getPermitsurname() }}</td>
<td>{{ permit.getPermitsCompany().getName() }}</td>
<td>{{ permit.getPermitsContractor().getName() }}</td>
<td> HERE I WANT TO DISPLAY DAYS LEFT</td>
<td>{{ permit.getExpirationdate()|date('Y-m-d') }}</td>
</tbody>
{% endfor %}
</table>
Is something like this possible?
{{ permit.getExpirationdate()|date('Y-m-d') - "now"|date('Y-m-d') }}
First Solution (recommended) "Use an existing library":
You can use the KnpTimeBundle
In the Twig:
This compare with the current date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(permit.expirationDate) }}
This compare with the another date:
{# Returns something like "3 minutes ago" #}
{{ time_diff(permit.expirationDate, anotherDate) }}
Second Solution "Do it yourself":
Make diff via php function:
$calcFrom = permit.getExpirationdate()
$now = new \DateTime('now');
$now->diff($calcFrom)->format("%a")
And make it available via a Twig extension or directly in an helper method in the entity.
Another possible solution is to write register a custom DQL Function to do the work in the repository
Hope this help