2017-02-12 84 views
2

我使用了帶有Symfony 3.1的Sonata Admin 3.13,並希望在列表視圖中顯示上傳的圖像。我有以下ListFields一個PaintingAdmin:Symfony3.1&Sonata管理員 - 列表字段的模板被忽略

protected function configureListFields(ListMapper $listMapper) 
{ 
    $listMapper 
     ->addIdentifier('name', null, ['label' => 'Name']) 
     ->add('category', null, ['label' => 'Kategorie']) 
     ->add('size', null, ['label' => 'Größe']) 
     ->add('imageFilename', null, [ 
      'template' => 'sonata:imagepreview.html.twig', 
      'label' => 'Bild' 
     ]); 
    ; 
} 

而在app/Resources/views/sonata/imagepreview.html.twig

{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %} 
{% block field %} 
    <div> 
     {% if object.imageFilename != null %} 
      <img src="{{ asset('uploads/images/' ~ object.imageFilename) }}" class="img-responsive" /> 
     {% else %} 
      <div class="warn">Kein Bild</div> 
     {% endif %} 
    </div> 
{% endblock %} 

一個模板,但該模板完全地忽略,它只顯示值imageFilename。其他一切正常(即label顯示爲Bild

回答

0

您需要告訴索納塔您正在使用自定義模板。

你可以在你的管理服務聲明:

librinfo_crm.admin.organism: 
     class: Librinfo\CRMBundle\Admin\OrganismAdmin 
     arguments: [~, Librinfo\CRMBundle\Entity\Organism, LibrinfoCRMBundle:OrganismAdmin] 
     tags: 
      - name: sonata.admin 
       manager_type: orm 
       group: Customers Relationship Management 
       label: librinfo.crm.organism_admin.label 
       label_translator_strategy: blast_core.label.strategy.librinfo 
     calls: 
      - [ setTemplate, [list, LibrinfoCRMBundle:OrganismAdmin:list.html.twig]] 

,或者你可以重寫你的管理類的$模板陣列()。

如果你想用「經典」的symfony模板繼承您的自定義模板應該有比原來相同的路徑螞蟻名稱,因此,如果你想更換SonataAdminBundle:CRUD:base_list_field.html.twig您的自定義模板應該是app/Resources/view/CRUD/base_list_field.html.twig

0

我建議您使用完整路徑的模板: 應用程序/資源/視圖/索納塔/ imagepreview.html.twig

->add('imageFilename', null, [ 
     'template' => 'sonata\imagepreview.html.twig', 
     'label' => 'Bild' 
    ]); 

所以,你可以sonata\imagepreview.html.twig是相對於應用程序/資源/視圖/文件夾。

在版本4.x版< symfony中的我們把樹枝tempates的方法有兩種:在

  1. RealPath: `src\AppBundle\Resources\views\MyCustomFolder\my_file.html.twig` 
    
    Path: `AppBundle::MyCustomFolder\my_file.html.twig` 
    
  2. 內的軟件包外應用程序文件夾

    RealPath: `app\Resources\views\MyCustomFolder\my_file.html.twig` 
    
    Path: `MyCustomFolder\my_file.html.twig` 
    

你可以閱讀更多的Official Symfony documantation