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})) }}
Related
Somewhere in my template I have this:
{{ $result->someText }}
Now in this text I want to highlight all words that are in the string
{{ $searchString }}
So I thought I create a new blade directive:
{{ #highlightSearch($result->someText, $searchString) }}
Blade::directive('highlightSearch', function($input, $searchString)...
error: missing argument 2
Found out that directives do not except 2 arguments. I tried every workaround that I could find but none worked. They always return the arguments as a plain string, not even passing the actual values.
I tried adding a helper function like explained here: https://stackoverflow.com/a/32430258/928666. Did not work:
error: unknown function "highlightSearch"
So how do I do this super easy task in laravel? I don't care about the highlighting function, that's almost a one-liner.
The reality is blade directives can't do what you need them to do. Whether or not they should is not a topic I can't help with. However you can instead do this in your service provider:
use Illuminate\Support\Str;
/* ... */
Str::macro('highlightSearch', function ($input, $searchString) {
return str_replace($searchString, "<mark>$searchString</mark>", $input);
//Or whatever else you do
});
Then in blade you can just do:
{!! \Illuminate\Support\Str::highlightSearch($result->someText, $searchString) !!}
I've just tested in Laravel 5.1 it and it works without any problem:
\Blade::directive('highlightSearch', function($input) {
return "<?php echo strtoupper($input); ?>";
});
in boot method of AppServiceProvider
and in view I can use:
#highlightSearch('test')
and it returns TEST as expected.
Make sure you are using Blade from global namespace, also run
php artisan clear-compiled
to make sure everything is refreshed. If it won't help, you can try running
composer dump-autoload
just in case.
EDIT
I've also tested it with additional argument and it really seems not be working as expected, so the most reasonable would be adding helper file (if you don't have any with simple PHP function) as for example:
function highlight_search($text, $searchString)
{
// return whatever you want
}
and later use it it in Blade as any other function like so:
{{ highlight_search('Sample', 'Sam') }}
optionally using
{!! highlight_search('Sample', 'Sam') !!}
if you want highlight_search to output HTML
I'm using Laravel basic authentication but I am really struggling to use the Auth object in the views.
There are good examples using Blade, but I am using Twig (twig bridge) and cannot solve this.
I can vardump(Auth::user()->name) in my controller in regular php but how do I get the same in the twig file (my view file)?
How do I do something like;
{% if auth.guest %}
Or;
{{ Auth.user().name }}
I've tried so many different ways but just get nothing.
This has to do with the way twig works, accessing static variables /classes or static methods on classes is impossible without adding a function to the Twig Environment.
You can register a function like so:
$twig = new Twig_Environment($loader);
$twig->addFunction('staticCall', new Twig_Function_Function('staticCall'));
function staticCall($class, $function, $args = array())
{
if (class_exists($class) && method_exists($class, $function))
return call_user_func_array(array($class, $function), $args);
return null;
}
Then i think you should be able to do:
{% if staticCall('Auth', 'guest') %}
Hi there stranger, why don't you login?
{% endif %}
I took the code from this answer: twig template engine, using a static function or variable
I have a partial view, that's displays a form on the DVD's page:
Partial View with form:
<div>
{{ form(form) }}
</div>
Use of partial view in the DVD's page:
{{ render(controller(
'DVDBundle:Actors:addActor',
{ 'dvdId': dvd.id }
)) }}
The idea is that admin can add actors using the form on the dvd page (url: website/dvdpage)
However: the function addActor in controller Actors doesnt seem to have anything in Request because its used via partial view:
Controllers function:
public function addActorAction(Request $request, $dvdId)
{
//form related code...
......
...
}
When checked what's inside: $request I get:
_fragment?_path=_format%3Dhtml%26_locale%3Den%26_controller%3DDVDBundle%253AActors%253AaddActor
and the error is forbidden access 403. here:
if (!$request->isMethodSafe()) {
throw new AccessDeniedHttpException();
}
The strange part is that if i go to that view directly via url : website/addactor then that same form works and the Request has correct value in same controller.
I found this:
When using a controller instead of a URL, you must enable the Symfony
fragments configuration:
added exact line to my config: fragments: { path: /_fragment }
but still nothing.
But have no idea how and what to do? Any help?
So yes, this worked for me, passing in the request as a parameter to the controllers function:
{{ render(controller(
'DVDBundle:Actors:addActor',
{ 'dvdId': dvd.id, 'request': app.request }
)) }}
Note, you also need to update the routing to include second parameter like this:
add_actor:
url: addactor/{dvdId}-{request}
....
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}) }}
{{ render(controller("SomeBundle:Foo:Bar", {HERE I WANT TO PASS ALL query parameters app.request.query.all}) }}
So can I access all master request query parameters in sub request and the subrequest should also run independently?
Try this:
{{ render(controller("SomeBundle:Foo:bar", {'all': app.request.query.all}) }}
and in action store it in $all variable
public function barAction($all) {
// other your code
}
From your controller:
array_merge($request->query->all(), $request->get('_route_params'));
//query->all : get all query string parameters
//_route_params : get current route parameters
From your twig template must be sth like:
app.request.query.all|merge(app.request.attributes.get('_route_params'))
I've never used this in twig templates, so first test it ;)
Then you can use that functions however you want to build the variables you'll pass to your subrequest
To just pass in what is in app.request.query.all:
{{ render(controller("SomeBundle:Foo:Bar", app.request.query.all)
To merge something extra in:
{{ render(controller("SomeBundle:Foo:Bar", { something: 'extra' }|merge(app.request.query.all))
Tested in Symfony 3.3.10 and Twig 1.35.0