symfony2 - twig - how to render a twig template from inside a twig template - symfony

I have a xxx.html.twig file which shows a page, but when I want to refresh the page with different data and just update it with new data, I have a select and a submit button for it.
The thing is that I don't know how do I call an action in the controller which I pass parameters to from my twig and call for new data and then I render the same twig template again with new parameters.
How do I do so?

Here are a few different ways:
{{ render(app.request.baseUrl ~ '/helper/test', {"hostid2": hostid } ) }}
or
{% include 'MyCoreBundle:Helper:test.html.twig' with {"hostid2": hostid } only %}
or
{% render controller("MyCoreBundle:Helper:test", {'hostid2': hostid}) %}

Symfony 2.1:
{% render 'YourBundle:YourController:yourAction' with {'var': value} %}
Symfony 2.6+:
{{ render(controller('YourBundle:YourController:yourAction', {'var': value})) }}
And, of course, read the documentation.

I think some parts are depricated here.
To make the include work in latest Symfony 3.1.10, I solved it like this:
{% extends 'base.html.twig' %}
{% block body %}
{{ include('AppBundle:Default:inner_content.html.twig') }}
{% endblock %}
Note: include() with parentheses.
Then all the variables are included from the parent template. If you like to restrict some variables in the child template, you use with ... only (look over)

Related

Symfony-twig template form theme error

I have everything working correctly and now I'm trying to work with form themes. This is my code to generate the form without a theme.
{% extends 'base.html.twig' %}
{% block body %}
{% include 'menu/menu.html.twig' %}
{% if addpost is defined %}
<div id='add_post_form'>
{{ form_start(addpost) }}
{{ form_widget(addpost) }}
{{ form_end(addpost) }}
</div>
{% endif %}
{% endblock %}
But when I'm adding a form-theme with the following code
{% form_theme form 'form/form_div_layout.html.twig' %}
I get this error:
Variable "form" does not exist
When i execute this without the line, I'm getting the following error:
Unknown "form_help" function. Did you mean "form_rest", "form_end"?
the form_div_layout.html.twig contains the code found at github symfony twig form theme
At my config.yml I've added the following under the twig section,
form_themes:
- 'form/form_div_layout.html.twig'
.
either not, i still have this error
what is missing ???
My file structure
If all your forms are going to use the same theme you only need to add the line in your config, but if you want a particular form theme in a particular template you can use the template tag.
The reason you're getting the 'form is not defined error' is because you don't have a variable called form passed the the template, your form variable is called addpost, so you need to use
{% form_theme addpost 'form/form_div_layout.html.twig' %}

Symfony 3.2 Easyadminbundle how to hide/remove default actions link

I was wondering if someone can tell me how to hide the action links from the list view base on a status column.
More details: I have a list view in which shows a list of items, In this list I have column named status. For each record in this list in which status is set to close, I would like to hide the edit/delete and other custom actions links from the list. Is this doable? if so, how?
Thanks
A possible solution is to override just the item_actions Twig block in the list.html.twig template used by that entity. In practice, if the entity is called Order, a template like this should work:
{# app/Resources/views/easy_admin/Order/list.html.twig #}
{% extends '#EasyAdmin/default/list.html.twig' %}
{% block item_actions %}
{% if item.status != 'close' %}
{{ parent() }}
{% endif %}
{% endblock item_actions %}

Display View in twig

I created (in Drupal 8) a view of a dataset (newsteaser_mit_bild) with some News in there.
With this view i created a block. The name is automatically generated (views_block__newsteaser_mit_bild_block_1).
The normal content is displayed with
{{ page.content }}
How can i display this View in my Twig File ?
{{ page.newsteaser_mit_bild }}
seems not to be right.
How can i use the view/block in my twig an how can i template them ?
In the main twig file you can use name block like this:
{% block my_custom_block }%
{% endblock my_custom_block %}
In the another twig file you can call the block like this:
{% extends 'link_for_file.twig' %}
{% block my_custom_block }%
{{ parent() }}
{% endblock my_custom_block }%
You can preprocess a new variable and use views_embed_view like:
function THEME-NAME_preprocess(&$variables, $hook) {
$variables['MY-VIEW-NAME'] = views_embed_view('VIEW-ID');
}
And then in the twig file:
{{ MY-VIEW-NAME }}

Symfony2.7 Twig How to obtain a token?

Inside Twig file I have this code:
{% set player = app.security.getToken().getUser().getPlayer() %}
{% if player.getSelectedCharacter() is not null %}
{% set character = player.getSelectedCharacter() %}
{% .... %}
{% endif %}
But at now, app.security is deprecated. So I want to change this. I can obtain user token inside my controller and send it to the Twig. But I prefer to get it directly via Twig.
How I can do this?
As you said and mentioned in the documentation.
The app.security global is deprecated as of 2.6. The user is already
available as app.user and is_granted() is registered as function.
I think you can just try something like this in your view.
app.user.getPlayer()

Use Twig Custom Set Variables From an Include

I'm trying to include a twig file with a bunch of custom set variables and then use the variables in the multiple other template files. Similar to how including a PHP file works.
I don't seem to have access to the variables set inside the include in my index file.
Is there any way to do this?
Sample Code *Edited
Included File:
{# variables.html #}
{% set width = "100" %}
{% set height = "250" %}
Template File:
{# index.html #}
{% include 'variables.html' %}
{{ width }}
{{ height }}
Expected Outcome:
100 250
Actual Outcome:
// Nothing gets output
I was just trying to do the same thing you were and came up with the following:
Created snippets.twig to maintain all these mini variables. In your case, you might call it variables.twig. In this file, I used a macro without any arguments. I was creating formatted entry date markup that I can use across all my templates and it looked like this:
{% macro entry_date() %}
<time datetime="{{post.post_date|date('m-d-Y')}}">{{post.post_date|date('F j, Y')}}</time>
{% endmacro %}
note that the parenthesis after the name declaration were imperative
In my main layout file, layout.twig, I referenced this macro via an import statement so it would be accessible in all child templates:
{% import "snippets.twig" as snippets %}
<!doctype html>
...
In my template files, I now have snippets accessible and can query it like any other variable:
{{ snippets.entry_date }}
UPDATE
This doesn't seem to correctly run code. If you're just storing static content, you should be good. You can also pass args to the macro so I imagine you could make some magic happen there but I haven't tried it.
As far as I know it is only possible with {% extends %} tag. Instead of including template with variables you should extend it.
Example:
variables.tpl:
{% set some_variable='123' %}
... more variables ...
{% block content %}
{% endblock %}
template.tpl
{% extends 'variables.tpl' %}
{% block content %}
{{ some_variable }}
... more code which uses variables assigned in variables.tpl ...
{% endblock %}
You can use a template extension : https://symfony.com/doc/current/templating/twig_extension.html
post|entry_date

Resources