I need to include a template multiple time in Twig.
Here is an explanation of my case:
I have the original template, lets say index.html.twig. This template include diffirent templates : part1.html.twig part2.html.twig etc..
My goal is to include a final template extra.html.twig to each sub templates (part1, part2). I've made the test, who concluded that I can include the final template just once. Twig detects that this template is already concluded and don't display it twice or three.
How can I achieve this result ?
|index.html.twig
|include part1.html.twig
|include extra.html.twig
|include part2.html.twig
|include extra.html.twig
UPDATE :
{% block file_upload %}
<div class="col-sm-12">
<div class="upload_files" style="float: right">
{{ form_widget(upload_form.button) }}
</div>
{{ form_widget(upload_form.attachment) }}
<table class="table table-files-preview" hidden>
<thead>
<td>{% trans %}NAME{% endtrans %}</td>
<td>{% trans %}TYPE{% endtrans %}</td>
<td>{% trans %}SIZE{% endtrans %}</td>
</thead>
<tbody class="tbody-file-preview">
</tbody>
</table>
<input type="hidden" id="files_source" value="{{ source }}">
</div>
{% endblock %}
Related
I'm building a Product Information Manager (PIM). Trying to build this with the symfony framework. The problem I'm facing at the moment. I'm showing my product information with datatables with the twig file example below.
{% set title = 'Product List' %}
{% extends 'table.twig' %}
{% block h1_page_header %} Product List {% endblock %}
{% block panel_heading %} Product List {% endblock %}
{% block thead %}
<tr>
<th>ID</th>
<th>Name</th>
<th>SKU</th>
<td>Actions</td>
</tr>
{% endblock %}
{% block tbody %}
{% for product in products %}
<tr>
<td>
{{ product.id }}
</td>
<td>
{{ product.name }}
</td>
<td>
SKU
</td>
<td>
<a href="{{ path('app_product_getproduct', {'id': product.id}) }}" class="btn btn-success btn-sm" >
<span class="glyphicon glyphicon-pencil"></span>
</a>
<a href="{{ path('app_product_delete', {'id': product.id}) }}" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?')">
<span class="glyphicon glyphicon-trash"></span>
</a>
</td>
</tr>
{% endfor %}
The problem I have now is that my database has now almost 60-70K. But my product/list/ page is not rendering due to the 70K product which I want to show in datatables. It works fine for few 2-4k Products but above this it does not render.
I did a research and found out that I can resolve my issue with server side processing. But the question I have is there a better solution / alternative to achieve my goal?
Even if server-side processing will help, some navigators may struggle to render all those elements at once. From a user point of view, that not interesting nether. Nobody will ever scroll through all the 70k products. Users are lazy :)
You should implement pagination, search-bar and filters to render a easy readable short list.
If you like JS you can implement a more elegant solution, like an infinite scroll.
This is a project for school!!!
Let's say that I want to store and show working hours in an HTML table.
I want something like, 'if' the date from DB which user added is equal to the date from the weekly calendar than show me an EDIT button else show me an ADD button?
I want to show Add for the rest of the users who didn't create any data in DB.
<table class="table" id="week">
<thead>
<tr>
<th scope="col">users</th>
{% for day in days %}
<th class="{{ day|date("D-d") }}">
<span>{{ day|date("D") }}</span>
<span>{{ day|date("d") }}</span>
</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for user in users %}
<tr class="days">
<th scope="row">{{ users.username }}</th>
{% for day in days %}
<td id="my_cell {{ day|date("d-m") }}">
{% for time in findTime %}
{% if d|date("d-m-Y") == time.date|date('d-m-Y') and users.id == time.getUser().id %}
{{ time.timeFrom|date("H:i") }} - {{ time.timeTo|date("H:i") }}
{% endif %}
{% endfor %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
I am currently using DB doctrine ORM on symfony 3.4v
I don't have an idea how to solve this problem, I tried with different array function in PHP but I did not find solutions.
Thank you for understanding.
[RESOLVED]
I have Symfony app and I am using twig templates. When I run my app locally on my computer it works, but when I upload it on my hosting provider on some pages the <meta charset="UTF-8" /> and <title> tags from head go into body. This happens only on some twig templates and not all. Here is my base template
<!DOCTYPE html>
<html>
{% block head %}
<head>
<meta charset="UTF-8" />
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}{% endblock %}
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
</head>
{% endblock %}
<body>
<nav>
<div class="container">
{% if app.user %}
Изход
Заявки
{% if (app.user.type == "Manager") or (app.user.type == "LittleBoss") %}
Нова заявка
{% endif %}
{% else %}
Home
Вход
{% endif %}
</div>
</nav>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
</html>
It is a valid html and it works. Every view extends base and overrides block body and that is all they do. Here is my new.html.twig which works both locally and on the server
{% extends 'base.html.twig' %}
{% block body %}
<h1>Нова заявка</h1>
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit" value="Create" />
{{ form_end(form) }}
<ul>
<li>
Върни се към списъка със заявки
</li>
</ul>
{% endblock %}
Pretty simple - it extends base and overrides block body and that is it. Now here is the code in index.html.twig where meta tag goes into body. Bear in mind that both files are in the same folder and they extend the same base file.
{% extends 'base.html.twig' %}
{% block body %}
<h1>Заявки</h1>
<table>
<thead>
<tr>
<th>Отдел</th>
<th>От</th>
<th>Вид</th>
<th>Описание</th>
<th>Краен Срок</th>
<th>Дата</th>
<th>Дизайнер</th>
<th>Подизпълнител</th>
<th>Приключил</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for zadanie in zadanies %}
<tr style="background-color:{{ zadanie.backgroundColor }}">
<td>{{ zadanie.department }}</td>
<td>{{ zadanie.fromUser }}</td>
<td>{{ zadanie.typeTask }}</td>
<td>{{ zadanie.description }}</td>
<td>{% if zadanie.term %}{{ zadanie.term|date('Y-m-d H:i:s') }}{% endif %}</td>
<td>{% if zadanie.date %}{{ zadanie.date|date('Y-m-d H:i:s') }}{% endif %}</td>
<td>{{ zadanie.designer }}</td>
<td>{{ zadanie.executioner }}</td>
<td>{% if zadanie.isOver %}Yes{% else %}No{% endif %}</td>
<td>
<ul>
<li>
show
</li>
<li>
edit
</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<ul>
<li>
Създай нова заявка
</li>
</ul>
{% endblock %}
There is some part written in bulgarian (cirillic) which u might not be able to read, but it has nothing to do with the logic. They are purely static. This file again extends base and overrides body. For some reason with this code when I run it on the server here is what I get:
and here is what I get when I run it locally:
The code is the same. And if you are asking about the server - they told me they are not familiar with Symfony and can't help me with my problem. So I just know they have PHP 7.1 and can run Symfony app and I also know something is broken. If you can't help me fix it by changing the code please tell me what should I say to my hosting provider
Turned out i had a dump() in my controller and for some reason it broke everything
As stated in the comments I had a dump() which broke everything. Removed it and it was all gucci.
I'm developing a web app using OctoberCMS where a user can select an item and see a list of material needed to craft it. Some materials can also be crafted and the user can see a "+" sign near the craftable material, that could be clicked to show an other list with the material needed to create the material.
See example
The problem is that while there is only one nested submaterial (like in the example image) there are no problem, but when there are more than one nested submaterial (for example a craftable submaterial that has craftable submaterials) the server returns me an "Internal server error" and the error_log reports:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65536 bytes) in /home/federico/federicoxella.com/vendor/composer/ClassLoader.php on line 412
This is the script i'm using to extract data from database (The database is on the same machine of the server)
public function onExpand()
{
$itemID = post('item_id');
$this->page['subitemMaterials'] =
Item::select('federicoxella_lootbot_items.id', 'name', 'craftable')
->join('federicoxella_lootbot_crafts', function($join) use ($itemID)
{
$join->on('federicoxella_lootbot_crafts.material_1', '=', 'federicoxella_lootbot_items.id')
->orOn('federicoxella_lootbot_crafts.material_2', '=', 'federicoxella_lootbot_items.id')
->orOn('federicoxella_lootbot_crafts.material_3', '=', 'federicoxella_lootbot_items.id');
})
->where('federicoxella_lootbot_crafts.material_result', '=', $itemID)
->get();
}
This function should take the ID of an item, extract all needed material from database and insert them in a page variable (subitemMaterial) that looks like this [{"id":3,"name":"Colla","craftable":0},{"id":4,"name":"Accendino","craftable":0},{"id":23,"name":"Metallo","craftable":0}]
If an item has "craftable":1 a new partial is created to host the material needed to create the item.
And this is the partial in where I use the variable
<ul class="no-bullet-list">
{% for i in 0..2 %}
<li>
{% if subitemMaterials[i].craftable == 1 %}
<b>{{ itemMaterial[i].name }}</b>
<form style="display:inline;">
<input type="hidden" name="item_id" value="{{ subitemMaterials[i].id }}" />
<a data-request="{{ __SELF__ }}::onExpand"
data-request-update="'{{ __SELF__ }}::subItems': '#{{ subitemMaterials[i].id }}_materials'">+</a>
</form>
<div id="{{ subitemMaterials[i].id }}_materials">
{% partial __SELF__~'::subItems' %}
</div>
{% else %}
{{ subitemMaterials[i].name }}
{% endif %}
</li>
{% endfor %}
</ul>
And the partial in where I declare the previous partial
<div class="callout">
<h3>{{ itemToCraft.name }}</h3>
<ul class="no-bullet-list">
{% for i in 0..2 %}
<li>
{% if itemMaterial[i].craftable == 1 %}
<b>{{ itemMaterial[i].name }}</b>
<form style="display:inline;">
<input type="hidden" name="item_id" value="{{ itemMaterial[i].id }}" />
<a data-request="{{ __SELF__ }}::onExpand"
data-request-update="'{{ __SELF__ }}::subItems': '#{{ itemMaterial[i].id }}_materials'">+</a>
</form>
<div id="{{ itemMaterial[i].id }}_materials">
{% partial __SELF__~'::subItems' %}
</div>
{% else %}
{{ itemMaterial[i].name }}
{% endif %}
</li>
{% endfor %}
</ul>
</div>
Can you guys help me understand why server returns this error code?
Found the problem (that not is exactly a problem):
Pratically I'm recursively adding partials that print the value of the variable injected in the page.
<ul class="no-bullet-list">
{% for i in 0..2 %}
<li>
{% if subitemMaterials[i].craftable == 1 %}
<b>{{ itemMaterial[i].name }}</b>
<form style="display:inline;">
<input type="hidden" name="item_id" value="{{ subitemMaterials[i].id }}" />
<a data-request="{{ __SELF__ }}::onExpand"
data-request-update="'{{ __SELF__ }}::subItems': '#{{ subitemMaterials[i].id }}_materials'">+</a>
</form>
<div id="{{ subitemMaterials[i].id }}_materials">
{% partial __SELF__~'::subItems' %} <--- Incriminate line of code.
</div>
{% else %}
{{ subitemMaterials[i].name }}
{% endif %}
</li>
{% endfor %}
</ul>
I have read the Symfony 2 documentation and I'm trying to make a custom embedded form and I can't understand the provided code in the documentation.
Official documentation:
http://symfony.com/doc/current/cookbook/form/form_customization.html
Specifically the code that i don't understand is this:
{% form_theme form _self %}
{% block _tasks_entry_widget %}
<tr>
<td>{{ form_widget(task.task) }}</td>
<td>{{ form_widget(task.dueDate) }}</td>
</tr>
{% endblock %}
after many tests I've noticed that '_task_entry' is the name of the embedded form (not the name of field in the main form)
Now I'm trying to get what is the 'task' variable, {{ form_widget(task.dueDate) }}
I've tried with the embedded form name again, with the name of the entity field, and with the name of the main form variable but nothing works:
{% form_theme edit_form.lineas _self %}
{% block zb_gestionbundle_lineaalbaran_widget %}
<div class="large-1 small-1 columns">
{{ form_widget(form.cantidad) }}
</div>
<div class="large-8 small-8 columns">
{{ form_widget(form.concepto) }}
</div>
<div class="large-2 small-2 columns">
{{ form_widget(form.precio) }}
</div>
{% endblock %}
{{ form_label(edit_form.lineas) }}
{{ form_errors(edit_form.lineas) }}
{{ form_widget(edit_form.lineas) }}
In summary, What I need to put in {{ form_widget(form.cantidad) }} for make the code works?
Tyvm!!
One possible solution:
After investigate a little more, I've found this code that works!
{% form_theme edit_form _self %}
{% macro prototype(linea) %}
<div class="large-1 small-1 columns">
{{ form_widget(linea.cantidad) }}
</div>
<div class="large-8 small-8 columns">
{{ form_widget(linea.concepto) }}
</div>
<div class="large-2 small-2 columns">
{{ form_widget(linea.precio) }}
</div>
{% endmacro %}
{% for linea in edit_form.lineas %}
{{_self.prototype(linea)}}
{% endfor %}
I don't know if the documentation is wrong, I leave the answer open for the doubt about the documentation.
Your solution work ! Just to complete, i had the same problem but the documentation is correct ! Just a little bit difficult to understand.(in my opinion).
To use the documentation solution :
you have to know the unique_block_prefix of your embbeded form.
To do this : add this to your code {{dump(form)}} and search the unique_block_prefix of your embedded form.
then you just have to replace the example of documentation like this :
{% form_theme form _self %}
{% block _zb_gestionbundle_lineaalbaran_entry_widget %}
<div class="large-1 small-1 columns">
{{ form_widget(form.cantidad) }}
</div>
<div class="large-8 small-8 columns">
{{ form_widget(form.concepto) }}
</div>
<div class="large-2 small-2 columns">
{{ form_widget(form.precio) }}
</div>
{% endblock %}
<!--block with your html/twig code, form, etc ..-->
{% block your_main_block %}
...
<!--your form-->
...
<!-- the embbeded part -->
{{form_row(form.lineas)}}
...
{% endblock %}
To sum up, generally the unique_block_prefix is
_(id of your embedded form)_entry_widget
And you just have to replace like the example of the doc.
I hope you understand and i miss nothing(sorry for my english...).