TIMBER. Display posts by click the category - php

I display a posts using this code. Now i want to dispaly posts by clicking the category name below the grid. How can i do it?
<section class="page-section page-section--padding-2rem">
<div class="row grid-posts">
{% for post in posts %}
{% include 'partial/single-cards/recipes.twig' with {'post': post} %}
{% endfor %}
</div>

In the .php template file, add the categories to the view:
$context['categories'] = Timber::get_terms('category');
In the .twig template, render the category dropdown input.
{% if categories %}
<form action="{{ site.url }}" method="get">
<select name="cat" id="cat" onchange="return this.form.submit()">
{% for cat in categories %}
<option value="{{ cat.id }}">
{{ cat.name }}
</option>
{% endfor %}
<option value="0">Show All</option>
</select>
</form>
{% endif %}

Related

Twig Setting Select Option on a dropdown

I am currently writing a php mvc from scratch and using twig as my template engine and need some assistance setting the selected option on a drop down list. Currently in my model I have an sql query that pull all list of supervisors and drops them in my drop down list using the twig for loop but I need to some how select a user’s supervisor if it matches up.
I apologize now as I am new to twig
View:
<select class="form-control" id="supervisor">
{% for supervisor in supervisor %}
<option value="{{supervisor.fname}} {{supervisor.lname}}" >{{supervisor.fname}} {{supervisor.lname}}</option>
{% endfor %}
</select>
Tried:
<select class="form-control" id="supervisor">
{% for supervisor in supervisor %}
{% if {{supervisor.fname}} {{supervisor.lname}} == {{ user.supervisor }} %}
<option value=”{{supervisor.fname}} {{supervisor.lname}}” selected>{{supervisor.fname}} {{supervisor.lname}}</option>
{% else %}
<option value=”{{supervisor.fname}} {{supervisor.lname}}”>{{supervisor.fname}} {{supervisor.lname}}</option>
{% endif %}
{% endfor %}
</select>
May be you can try something like this:
Replace supervisor variable name by oneSupervisor in the loop and test oneSupervisor with user.supervisor.
<select class="form-control" id="supervisor">
{% for oneSupervisor in supervisor %}
{% set selected = '' %}
{% if (oneSupervisor.fname ~ ' ' ~ oneSupervisor.lname) == user.supervisor %}
{% set selected = 'selected' %}
{% endif %}
<option value="{{oneSupervisor.fname}} {{oneSupervisor.lname}}" {{ selected }}>{{oneSupervisor.fname}} {{oneSupervisor.lname}}</option>
{% endfor %}
</select>
Assuming you have a form called form and therein a select field called field, You could simply do the following:
{% do form.field.setRendered %}
<select id="{{ form.field.vars.id }}"
name="{{ form.field.vars.name }}">
{% for option in form.field.vars.choices %}
<option {{ form.field.vars.value == option.value ? 'selected' : '' }}
value="{{ option.value }}">
{{ option.label }}
</option>
{% endfor %}
</select>

Possible memory leak in PHP script

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>

Customize embedded form symfony 2

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...).

Symfony 2 form theme with choice field

I have problem. I want to do choices country with flag ico but I don't know how to create custom theme for form choice filed.
I created test form :
->add('name', 'choice', array('choices' =>array('en' => 'England', 'de' => 'Deutshland')))
Next in my view I try
{% block _send_name_widget %}
<select>
{% for f in form %}
{{ loop.index }}
{%endfor%}
</select>
{% endblock%}
{{ form_widget(form.name) }}
And in my html code I've got:
<select>
1
2
</select>
<select>
</select>
Could you tell me why?
How can I render only one select with parameters?
The variable "options" didn't exist in my choice_widget_options template. The fact is that "options" IS NOT the correct name of the variable. If you take a look at \vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Resources\views\Form\choice_widget_options.html.php, you'll find Symfony use a variable called 'choices'.
The corrected version of the previous code is :
{% block choice_widget_options %}
{% spaceless %}
{% for group_label, choice in choices %}
{% if choice is iterable %}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>
<img src="/images/flags/{{ choice.label }}.jpg" />
{{ choice.label|trans({}, translation_domain) }}
</option>
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}
Override the choice template:
{% block choice_widget_options %}
{% spaceless %}
{% for group_label, choice in choices %}
{% if choice is iterable %}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>
<img src="/images/flags/{{ choice.label }}.jpg" />
{{ choice.label|trans({}, translation_domain) }}
</option>
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}
more info about form theming in the Symfony2 docs

How to get form variables in custom form field in symfony2

I have this custom form field
{# src/Acme/DemoBundle/Resources/views/Form/fields.html.twig #}
{% block gender_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{# just let the choice widget render the select tag #}
{{ block('choice_widget') }}
{% endif %}
{% endspaceless %}
{% endblock %}
This renders the checkboxes. But i am not able to find how can i get the value of checkbox
i.e child.form.value is not working.
Suppose i have entities which is named as tasks in the form.
how can i get the value of the taskid.
something like
child.form.vars.task.id
It seems to be in {{ choice.value }}
Have a look at this to see how the inheritance works.
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>

Categories