Skip to content

Instantly share code, notes, and snippets.

@dspe
Last active December 12, 2015 06:49
Show Gist options
  • Save dspe/4732313 to your computer and use it in GitHub Desktop.
Save dspe/4732313 to your computer and use it in GitHub Desktop.
How to get an user name on twig template ? {{ content.contentInfo.contentType.creatorId|user_name }}
<?php
namespace Acme\DemoBundle\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AcmeExtension extends \Twig_Extension
{
private $container;
public function __construct( ContainerInterface $container )
{
$this->container = $container;
}
public function getFilters()
{
return array(
'user_name' => new \Twig_Filter_Method($this, 'userNameFilter'),
);
}
public function userNameFilter($userId = 10)
{
$userService = $this->container->get('ezpublish.api.repository')->getUserService();
$user = $userService->loadUser( $userId );
return $user->content->contentInfo->name;
}
public function getName()
{
return 'Acme';
}
}
?>
services:
Acme.twig_extension:
class: %acme.twig_extension.class%
arguments: [@service_container]
tags:
- { name: twig.extension }
@lolautruche
Copy link

@dspe : Did you know that your gist can have multiple files ? 😉

@dspe
Copy link
Author

dspe commented Feb 7, 2013

oh yeah ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment