Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 & Twig - theming file upload widget

Tags:

twig

symfony

I have an Symfony2 edit form for an entity and one of the fields is for the user's profile picture.

I'm trying to theme the Twig file upload widget so that the current picture set for the field is displayed above the file input.

So far, I have:

{% form_theme edit_form _self %}
{% block field_widget %}
    {% set type = type|default('text') %}

    {% if type == 'file' %}
        <img src="{{ value }}" /> 
    {% endif %}

    {{ block('form_widget_simple') }}
{% endblock %}

All works well except that the value variable is blank (which makes sense I guess).

My question is how can I get hold of the path to the file? Is there a way to pick it out of the form values for the field? Could I perhaps pass it through as an option to the field?

Srz if this is a dumb question, still pretty new to Symfony and Twig..

like image 565
Sacred Socks Avatar asked Nov 13 '12 14:11

Sacred Socks


People also ask

What is Symfony used for?

Symfony is a feature-rich back-end framework that is used to build complex applications. Many developers still prefer the lightweight Silex micro-framework or the Symfony MicroKernel, and Symfony is a widely used application framework among open-source developers.

What is Symphony software?

Symfony is an open-source PHP web application framework, designed for developers who need a simple and elegant toolkit to create full-featured web applications. Symfony is sponsored by SensioLabs. It was developed by Fabien Potencier in 2005.

Which is better Laravel or Symfony?

Both PHP Laravel and Symfony PHP are developed on MVC architecture, but in terms of component reusability and code modularity, Symfony has the edge over Laravel on this one. It structures the program in an organised fashion and becomes the perfect fit for larger and complex projects.

Who created Symfony?

Fabien Potencier Being a developer by passion, he immediately started to build websites with Perl. But with the release of PHP 5, he decided to switch focus to PHP, and created the symfony framework project in 2004 to help his company leverage the power of PHP for its customers.


1 Answers

A better way to customize the file widget will be

{% block file_widget %}
    {% spaceless %}
     //Your custom content here
    {% endspaceless %}
{% endblock file_widget %}

For example:

{% block file_widget %}
    {% spaceless %}
     <td>{% set type = type|default('file') %}
         <input type="{{ type }}" {{ block('widget_attributes') }} />
     </td>
    {% endspaceless %}
{% endblock file_widget %}

Remember that "{% spaceless %}" is very important

like image 111
Richard Pérez Avatar answered Oct 05 '22 18:10

Richard Pérez