2017 © Pedro Peláez
 

symfony-bundle viewcomponent

Implementation of View Components for Symfony 3.3

image

starychfojtu/viewcomponent

Implementation of View Components for Symfony 3.3

  • Monday, September 25, 2017
  • by starychfojtu
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,713 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 21 % Grown

The README.md

ViewComponentBundle

Implementation of View Components for Symfony 3.3, (*1)

Installation

$ composer require starychfojtu/viewcomponent

Symfony flex might register incorrect namespace in bundles.php. If it did, go ahead and register it yourself with, (*2)

'ViewComponent\ViewComponentBundle' => ['all' => true]

Configuration

starychfojtu_view_component:
    component_dirs: ['AppBundle/Component', 'AppBundle/SpecialComponent'] #results in '/src/AppBundle/Component', '/src/AppBundle/Component/specialComponent'
    # Specify directories where the bundle should search for components from /src
    template_dirs: ['components', 'specialComponents'] #results in '/templates/components', '/templates/specialComponents'
    # Specify directories where the bundle should search for templates from /templates

Usage

First specify your view component by creating a class in configured directories and implement ViewComponentInterface. The render method returns associative array of objects that are passed to the view., (*3)

YOU HAVE TO NAME YOUR COMPONENT IN THIS WAY : YourSpecialNameViewComponent It is a classic CamelCase naming e.g. MenuViewComponent or CartSummaryViewComponent., (*4)

# src/AppBundle/Component/MenuViewComponent

<?php

namespace AppBundle\Component;

use Starychfojtu\ViewComponentBundle\ViewComponentInterface;

class MenuViewComponent implements ViewComponentInterface
{
    public function render(): array
    {
        return [
            'links' => ['home','about','contact']
        ];
    }
}

Then add your template to one of configured directories., (*5)

# templates/components/Menu.html.twig

{% for link in links %}
    {{ link }}
{% endfor %}

And finally render your component in the view:, (*6)

{{ component('Menu', 60 * 60) }}

The second parameter is cache time of the whole component with default set on 0;, (*7)

Custom template name

If you want to specify another template name in view component, just add a key with template like this:, (*8)

# src/AppBundle/Component/MenuViewComponent

<?php

namespace AppBundle\SpecialComponent;

use Starychfojtu\ViewComponentBundle\ViewComponentInterface;

class MenuViewComponent implements ViewComponentInterface
{
    public function render(): array
    {
        return [
            'links' => ['home','about','contact'],
            'template' => 'AnotherMenu'
        ];
    }
}

Dependency injection

To pass any dependencies to the component, just register them as services., (*9)

TODO: register them automatically in bundle, (*10)

The Versions

25/09 2017

dev-master

9999999-dev

Implementation of View Components for Symfony 3.3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Josef Starýchfojtů

component twig symfony components viewcomponents

25/09 2017

v0.4.0

0.4.0.0

Implementation of View Components for Symfony 3.3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Josef Starýchfojtů

component twig symfony components viewcomponents

21/09 2017

v0.3.1

0.3.1.0

Implementation of View Components for Symfony 3.3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Josef Starýchfojtů

component twig symfony components viewcomponents

21/09 2017

v0.3.0

0.3.0.0

Implementation of View Components for Symfony 3.3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Josef Starýchfojtů

component twig symfony components viewcomponents

21/06 2017

v0.2.3

0.2.3.0

Implementation of View Components for Symfony 3.3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Josef Starýchfojtů

component twig symfony components viewcomponents

21/06 2017

v0.2.2

0.2.2.0

Implementation of View Components for Symfony 3.3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Josef Starýchfojtů

component twig symfony components viewcomponents

20/06 2017

v0.2.1

0.2.1.0

Implementation of View Components for Symfony 3.3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Josef Starýchfojtů

component twig symfony components viewcomponents

18/06 2017

v0.2.0

0.2.0.0

Implementation of View Components for Symfony 3.3

  Sources   Download

MIT

The Requires

 

The Development Requires

by Josef Starýchfojtů

component twig symfony components viewcomponents

18/06 2017

v0.1.0

0.1.0.0

Implementation of View Components for Symfony 3.3

  Sources   Download

MIT

The Requires

 

component twig symfony components viewcomponents

18/06 2017

v0.1.0-alpha

0.1.0.0-alpha

View Components Bundle for Symfony 3.3

  Sources   Download

MIT

The Requires

 

component twig symfony components viewcomponents