symfony2 namespaces using render(controller(..)) - symfony

When I try to call:
{{ render(controller(
'AppBundle:Default:_test'
)) }}
this one works, but if I create Admin folder and move my DefaultController there and call it like that:
{{ render(controller(
'AppBundle:Admin:Default:_test'
)) }}
this error shows up
("Unable to parse the controller name
"AppBundle:Admin:Default:_test".")
What I'm missing? Thanks.

If you moved the controller from Default to Admin/Default folder, you should call it with
{{ render(controller(
'AppBundle:Admin/Default:_test'
)) }}

Related

Passing twig code into a twig template and rendering it

So I have a twig template that's basically empty, and the application generates some twig code and passes this as a variable into a twig template, like this:
return $this->render('blank.html.twig', [
'twig' => $this->generateTwig()
]);
blank.html.twig looks like this:
{{ twig }}
But when the template is rendered it just has un-rendered twig code inside, like this:
{% extends 'base.html.twig' %} {% block content %} <h1>{{ 'app-name'|trans }}...
How do you render the injected twig code in this example?
Doing a file_put_contents('blank', $this->generateTwig()) works, but this defeats the purpose of using templates.
You can solve this two ways. Either you pre-render $this->generateTwig()'s output and inject (the HTML) into the blank.html.twig template (untested pseudo):
$template = $this->get('twig')->createTemplate($this->generateTwig());
$twig = $template->render();
return $this->render('blank.html.twig', [
'twig' => $twig,
]);
The downside of this approach is that you have to call {{ twig|raw }} in blank.html.twig, otherwise Twig will escape the HTML. Also: it feels a bit weird to pre-render a twig template before feeding it to Twig (again).
The other approach is that you load the template inside of your blank.html.twig template:
{{ include(template_from_string(twig)) }}
The template_from_string function is part of the StringLoaderExtension.
Docs here: https://twig.symfony.com/doc/3.x/recipes.html#loading-a-template-from-a-string
Edit: Thinking of it, a third approach could be even simpler if blank.html.twig is really just a file that outputs {{ twig }}:
$template = $this->get('twig')->createTemplate($this->generateTwig());
return $template->render();

Symfony2: How to get request parameter

I have a route:
professor_identity_edit:
path: /professor/{id}/identity
defaults: { _controller: FrontBundle:ProfessorIdentity:edit }
How can I get the parameter {id} in twig.html file ?
I already tried app.request.query.get('id') but with no success.
Not a good thing but you can still do. Normally you would pass the info to Twig from the controller like #Maerlyn pointed out.
Get route parameters in Twig.
{{ app.request.attributes.get('_route_params') }}
AND
Gets whole bundle name in Twig.
{{ app.request.attributes.get("_controller") }}
Get route name in Twig.
{{ app.request.attributes.get('_route') }}
I found the solution:
{{ app.request.attributes.get('_route_params')['id'] }}

Show image name in Twig, relation one to one

I have a one to one relation between Player and Image. I have also this line in my controller:
var_dump($players[0]->getLinkedImage1()->getName());
It shows the name of an image correctly.
And I have also this line in the template:
{% for players in player %}
{{ player.age }}
{{ player.linkedImage1.name }}
{% endfor %}
but I get this error:
Impossible to access an attribute ("name") on a NULL variable ("")
I expected the last line shown the same name as in the controller.
EDIT: finally I found out that the property was public, that was the reason. Anway I still understand it..
My bad !
Look your for loop..
Try
{% for player in players %}
How did you write the property ? linkedImage1?
If you wrote something like linked_image_1 or linkedImage_1 you should call
{{ player.linked_image_1 }}
or
{{ player.linkedImage_1 }}
then twig will call the related getter according to : http://api.symfony.com/2.4/Symfony/Component/DependencyInjection/Container.html#method_camelize
If its a virtual getter you can directly access with :
{{ player.getLinkedImage1().name }}
or {{ player.getLinkedImage1().getName() }} `
You should try
{{ player.getLinkedImage1().getName() }}

Symfony Twig path() not working when using render

I have a layout that includes some chuck of code form a controller called "Layout"
In the header section I have:
{% block accessinfo %} {% render "/layout/accessinfo" %} {% endblock %}
It works pretty fine, the view file content is:
{% extends '::layout.html.twig' %}
{% block body %}
{% if( is_logged == 0 ) %}
Welcome, access your <a id="accessAccount" title="Access your account">here</a>.
{% else %}
Hi, <b><em> {{ is_logged_user_name }}</em></b>, <a id="doLogout" href="javascript:void;">(Logout)</a>.
<i class="icon-user"></i> Your Account
{% endif %}
{% endblock %}
As one can figure out, path('account/manage') points to the Route named 'account/manage', but it's not returning the fully qualified URL to my project.
It returns:
http://localhost.project/account/manage
where it should be:
http://localhost.project/web/app_dev.php/account/manage
NOTE: I have path() all around my template files and they work like a charm.
IMPORTANT: I found out that when I call REQUEST URI inside the action method:
$this->get('request')->server->get('REQUEST_URI')
PHP will return the URL called by the render, in this case is:
/layout/accessinfo
Perhaps I'm not fully understanding your issue but it seems like you missunderstood the use of the path() and render() functions.
First of all if you like to render a controller and you follow the documentation here you would do it like this...
{{ render(controller('AcmeArticleBundle:Article:recentArticles') }}
{# with some parameters #}
{{ render(controller('AcmeArticleBundle:Article:recentArticles', {
'max': 3
})) }}
This assumes you're using Symfony >= 2.2. This follows the bundle:controller:action pattern, which is called Controller Naming Pattern
For a normal use of the path() function you would always use the name of the route and not a hardcoded URL (as it seems like you're passing in URLs and not route names?)
Let's say your route is called accountmanager, your routing.yml should look like this example
# app/config/routing.yml
accountmanager:
path: /account/manage
defaults: { _controller:YourBundleName:YourControllerName:ControllerAction }
And with that in your routing.yml in twig the use of path() is simply achieved by writing {{ path('accountmanager') }}
See the documentation on this topic. Using the name of the route and not a URL pattern ensures that you're getting to the right page which also includes your environment settings (like app_dev.php for your dev environment)

Get current route/path deliver _internal route

I'm rendering data from a CMS via Twigcode to a Controller and the Controller renders a twig template. I'd like to have the current route. I already tried to use:
// Controller
$request = $this->container->get('request');
$routeName = $request->get('_route');
// or Twigtemplate
{{ app.request.attributes.get('_route') }}
But the result is "_internal". How can I solve that problem?
You forgot the call to the Twig path function in your Twig template!
{{ path(app.request.attributes.get('_route')) }}
instead of
{{ app.request.attributes.get('_route') }}
Hope it helps!

Resources