Symfony2 - Is there a way to implement something like jsf snippets?
I mean, some code that has its own logic associated with a particular branch template and associated logic in an INSIDE controller on another page.
Something like a panel with specific data for the user. Name, state, phone number and some services and this logic included, I want to include it in the pages where I decided. Just reuse.
+3
source to share
1 answer
You can simply render a controller that returns that data from your views, or make a service that fetches the data and pushes it to a branch.
1. Controller example
controller
class UserDataController extends Controller
{
public function userDataAction()
{
$userData = // fetch user data....
return $this->render('user_data_fragment_template.html.twig', ['user_data' => $userData]);
}
}
Some templates where you want to show this snippet
<div>{{ render(controller('YourBundle:UserDataController:userData')) }}</div>
2. Service example
Data Provider Service
class UserDataProvider
{
public function __construct(...)
{
....
}
public function getUserData()
{
$userData = // fetch user data...
return $userData;
}
}
config.yml
// ...
twig:
globals:
user_data_provider: @your_user_data_provider_service_name
Some templates where you want to show this snippet
<div>{% include 'user_data_fragment_template.html.twig' with { userData: user_data_provider.userData } only %}</div>
+2
source to share