Symfony 2 CMF: Embed content twig in another admin twig - php

I have created a twig file in sonata admin, where I want to show the preview of the another twig created using the sonata cms.
I found the use of render method to embed the controller action.
<div class="preview">
{{ render(controller('Bundle:Controller:action') }}
</div>
But the action takes an argument $contentDocument
public function Action($contentDocument, Request $request)
{
}
So how do I render this twig inside the preview div, to show a thumbnail to the user.
Thank you

You can pass parameters as a second parameter to the controller() twig function:
{{ render(controller('Bundle:Controller:action', {'contentDocument': some_instance_of_a_content_document}) }}

Related

Load list of active users from database with symfony without using a controller

I'm making a presonification menu on my navbar so that i can access from any page i don't want to use the getRepository inside a controller and pass to the frontend.
Is there a way on symfony 4 that I can get all the users from my database and call it to front? like the app.session.user (to get info on the logged user) for example?
Thanks!
As Cerad mentioned, you can embed a Controller in your templates : https://symfony.com/doc/current/templates.html#embedding-controllers
Instead of doing {% include 'base/navbar.html.twig' %} you will do
In your template :
{{ render(controller('App\\Controller\\BaseController::renderNavbar')) }}
In BaseController
public function renderNavbar(UserRepository $userRepository) {
return $this->render('base/navbar.html.twig', [
'activeUsers' => $userRepository->findActiveUsers(),
]);
}
It's not a route, it's simply a function that renders html, so you don't need to add annotations.

Symfony Twig {{render}} handling passed arguments

Over a template I do a render method:
{{ render(controller('AppBundle:Widgets:myCapsWidget'),{'somestring':someString}) }}
Over the controller I have the following method:
public function myCapsWidgetAction($somestring)
{
return new Response(strtoupper($somestring));
}
I also looked over theese links:
symfony twig render controller argument array
https://knpuniversity.com/screencast/symfony2-ep3/render-controller
But still cannot find any light to my path.
If I read the first link you gave us, you should use:
{{ render(controller('AppBundle:Widgets:myCapsWidget',{'somestring':someString})) }}

Symfony2: calling helper in view

I have a problem with a helper in Symfony2. I have a helper which works fine. And I have a view made with PHP and HTMl without sintax errors.
I need to make a foreach and in that foreach, I need to create a button which call my helper which some information of this iteration and If an user click in this button, call this helper with this information.
Some like:
foreach $i...
<form action="<?php echo $view['entities']->HelperFunction($i) ?>" >
<button type="submit">Helper Call</button>
...
end foreach
My problem is that in each iteration, automatically the helper is called and his function executed. I need to execute only the helper when I click in the button generated.
Any idea?
Thanks in advance.
You should use symfony forms for this or if it is something custom and you don't have any field inside the form you just for some reason wants to use a function, you can still use symfony not pure PHP in your controller which render the view pass the variable $view['entities'] to the twig like this :
public function newAction(Request $request)
{
/* your codes */
return $this->render('yourBundle:Default:yourtwigfile.html.twig', array(
'view' => $view['entities'],
));
PS : yourBundle:Default:yourtwigfile.html.twig : Default is the folder by default which is yourBundle/Resources/views, if you have a folder inside views like yourBundle/Resources/views/folder it would be yourBundle:folder:yourtwigfile.html.twig
Then inside the twig file in your action use this :
{% for i in 0..10 %}
<form action="{% view.HelperFunction(i) %}" >
<button type="submit">Helper Call</button>
...
{%endfor%}
i didn't test this but it should work, i don't know why you want to do this but it will generate lot of forms depending on how many time you want the for to run.

Symfony2/Twig - Add classes to form elements

I've just started a new Symfony2 project where I've used the generate:doctrine:crud to quickly scale out a few views, forms, etc.
The generated form code is just:
{{ form(form) }} but includes a generic create or delete button. I was wondering how I could add a class to these generic buttons or modify them in any way since it's just encompassed in {{ form(form) }}?
For reference I'm using Twitter Bootstrap to quickly apply some styles so I don't want to change the css based on the submit button.
Thanks!
You can specify CSS classes in the form builder class to avoid filling your Twig template with html even for rendering the form individually.
When you call {{ form(form) }} you are using a helper to simplify your code so you don't have to call form_widget for each one of your fields, but doing so you can't control the exact display in the template. To do it you have to specify the class that will be applied to the field.
In the WhateverType.php file, inside the Forms folder, you have the form builder. There you should have something like:
$builder
->add('text')
->add('whatever')
There you have to add the classes:
$builder
->add('text', 'attr'=> array('class'=>'btn')
->add('whatever')
Then, when your form is displayed in the template, it will apply the classes that you specified in the builder.
After following dmnptr's answer (breaking form into parts), you can pass an array of arguments to each form / form_row / form_label etc by:
{{ form(form, { 'attr': { 'class': 'your-css-class-1 your-css-class-2' } } ) }}
The attr param sets attributes for the item, so the above would produce:
<form class="your-css-class-1 your-css-class-2" ...
You are going to have to render each filed by hand and add necessary classes to elements in your TWIG. Read on how to do it in the official docs - http://symfony.com/doc/current/book/forms.html#rendering-each-field-by-hand

How to load a view from a view?

I used to work with CodeIgniter. Now I am starting to learn Symfony2. I was just wondering, in CodeIgniter I could load a view from another view. Like, I could load menu.php from index.php. This is how I used to do it -
//in index.php
<?php $this->load->view('menu.php'); ?>
Is it possible to do the same thing in Symfony2 and Twig?
There are a few different ways you can do it depending on what you're trying to accomplish.
If you want to render the response of a controller you can do this in your twig template.
{{ render(controller('AcmeArticleBundle:Article:recentArticles', {
'max': 3
})) }}
In the above example, the parameter passed max would be passed as an argument to your controller. Then the controller would be responsible for returning a response that will be inserted into the view where it was called from.
You can also use include to render just the twig template as an embedded view:
{% for article in articles %}
{{ include(
'AcmeArticleBundle:Article:articleDetails.html.twig',
{ 'article': article }
) }}
{% endfor %}
In the above example article would be passed into the context of the twig template articleDetails.html.twig but not to any controller. So this method is ideal for repetitious front-end code that is used in many places such as templates for tables, lists, sidebars, etc.
http://symfony.com/doc/current/book/templating.html#including-other-templates
http://twig.sensiolabs.org/doc/functions/include.html

Categories