Easily generate view for given entity/object
- academic/vipa#990
- https://github.com/ojs/ojs/blob/master/src/Ojs/CoreBundle/Service/Twig/DisplayExtension.php
- Development steps can be followed from https://github.com/behramcelen/symfony-bundle-develop
- A basic test and logic controller and view can be found in https://github.com/behramcelen/symfony-bundle-develop/blob/master/src/AppBundle/Controller/DisplayController.php#L16
- https://github.com/behramcelen/symfony-bundle-develop/blob/master/src/AppBundle/Resources/views/Display/display.html.twig#L7
Open a command console, go to your project directory and execute the following command to download the latest version of this bundle:
$ composer require enterprisephp/display-bundle "dev-master"
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php
file of your project:
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new EP\DisplayBundle\EPDisplayBundle(),
);
// ...
}
// ...
}
use DisplayTrait on which you want to display objects/entities (!important):
namespace AppBundle\Entity;
use EP\DisplayBundle\Entity\DisplayTrait;
class MyEntity
{
use DisplayTrait;
}
Add below configs to config.yml
file:
ep_display:
global:
image_render: true # (optinal) defaults to true
file_render: true # (optinal) defaults to true
template: EPDisplayBundle::display.html.twig # (optinal) defaults to EPDisplayBundle:display.html.twig template
exclude_vars: # (optinal) defaults to empty array
- excludeField
- hiddenField
- password
array_collection_render: true # (optinal) defaults to true
collection_item_count: 5 # (optinal) defaults to 10
If you do not want to any extreme things. Only display on template;
{{ display(entity) }} # so easy isn't it 😉
You can override all bundle configs via class annotation:
related : https://github.com/EnterprisePHP/EPDisplayBundle/blob/master/Annotation/Display.php#L11
namespace AppBundle\Entity;
use EP\DisplayBundle\Annotation as Display;
/**
* @Display\Display(
* image_render=false, # optinal
* file_render=false, # optinal
* template="my_entity_special_template.html.twig", # optinal
* array_collection_render=true, # optinal
* collection_item_count=8, # optinal
* )
*/
class MyEntity
{
If you exclude spesific fields of entity use Exclude annotation:
related : https://github.com/EnterprisePHP/EPDisplayBundle/blob/master/Annotation/Exclude.php
namespace AppBundle\Entity;
use EP\DisplayBundle\Annotation as Display;
class DummyEntity
{
/**
* @Display\Exclude
*/
protected $unPublicfieldForOnlyThisEntity;
Expose field. All fields defaultly exposes but if you excluded from config but only one time you want to expose this field;
namespace AppBundle\Entity;
use EP\DisplayBundle\Annotation as Display;
class DummyEntity
{
/**
* @Display\Expose
*/
protected $oneTimeExposeField;
File expose with link;
namespace AppBundle\Entity;
use EP\DisplayBundle\Annotation as Display;
class DummyEntity
{
/**
* @Display\File(path="uploads/files")
*/
protected $mainFile;
Image expose with img tag;
namespace AppBundle\Entity;
use EP\DisplayBundle\Annotation as Display;
class DummyEntity
{
/**
* @Display\Image(
* path="uploads/files",
* height="50",
* width="70",
* )
*/
protected $cover;
You can spesify all configs from template as second param on display function;
{{ display(entity, {
files: {
my_file: {
path: "web/uploads"
}
},
images: {
header: {
path: "web/images",
width: "70",
height: "50"
}
},
exclude: "myVeryPrivateField", // can be array
expose: "onlyForThisPageExposeVar", // can be array
image_render: true,
file_render: true,
array_collection_render: false,
collection_item_count: 90,
template: "my_stylish_template.html.twig"
}) }}
Issues and feature requests are tracked in the Github issue tracker.
When reporting a bug, it may be a good idea to reproduce it in a basic project built using the Symfony Standard Edition to allow developers of the bundle to reproduce the issue by simply cloning it and following some steps.