Created
July 24, 2011 00:56
-
-
Save n3b/1102062 to your computer and use it in GitHub Desktop.
Symfony2. Getting service in template.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyBundle\Service; | |
class Catalog | |
{ | |
protected $em; | |
public function __construct($em) | |
{ | |
$this->em = $em; | |
} | |
public function getItems() | |
{ | |
if(!isset($this->items)) | |
$this->items = $this->em->getRepository('MyBundle:Item')->findAll(); | |
return $this->items; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyBundle\Templating\Catalog; | |
use MyBundle\Service\Catalog as CatalogService; | |
use Symfony\Component\Templating\Helper\Helper; | |
class Catalog extends Helper | |
{ | |
protected $catalog; | |
public function __construct(CatalogService $cs) | |
{ | |
$this->catalog = $cs; | |
} | |
public function getItems() | |
{ | |
return $this->catalog->getItems(); | |
} | |
public function getName() | |
{ | |
return 'catalog'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
catalog: | |
class: MyBundle\Service\Catalog | |
arguments: | |
services: | |
em: '@doctrine.orm.entity_manager' | |
helper.catalog: | |
class: MyBundle\Templating\Catalog | |
arguments: | |
catalog_service: '@catalog' | |
tags: | |
- { name: templating.helper, alias: catalog } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul> | |
<?php foreach($view['catalog']->getItems() as $item): ?> | |
<li> | |
<?php echo $item->getTitle() ?> | |
</li> | |
<?php endforeach ?> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment