I'm Symfony2 and CreateFormBuilder to create my form.
Currently I'm using {{ form_widget(form) }} to display the form.
My entity have path property that is the path of an image save on filesystem.
I want to display the image in the form (with a <img> html tag), how can I achieve this result? Should I handle the form in my template field by field? Or is there a way to own only one field in the template and render the other ones with {{ form_widget(form) }} ?
What you could do is handling the form field by field, and display the image if the value is set.
Let's say you have a field name image. In Twig, you can access its value through form.vars.value.image. Then, it's quite easy to display an img tag:
{% if form.vars.value.image is not null %}
<img src="{{ asset('upload/dir/' ~ form.vars.value.image) }}" />
{% endif %}
Here, upload/dir/ is a path where you store your images. If you have a constant for this path, you can use it in Twig:
{{ asset(constant('Acme\\DemoBundle\\Model\\Object::UPLOAD_DIR') ~ '/' ~ form.vars.value.image) }}
An alternative could be to create your own type with its own template:
http://symfony.com/doc/current/book/forms.html#form-theming
http://symfony.com/doc/current/cookbook/form/form_customization.html
Edit: I forgot an interesting alternative. You can customize an individual field: http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field. Here is a draft of what you could do:
{% form_theme form _self %}
{% block _object_image_row %}
<div class="name_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
{% if form.vars.value.image is not null %}
<img src="{{ asset('upload/dir/' ~ form.vars.value.image) }}" />
{% endif %}
</div>
{% endblock %}
Related
Hi all Im calling data from a json file. Whe I create the following loop:
{% for item in resume.applications %}
<span class="expanded uppercase">{{ item.fields.Job }}</span>
{{ item.fields.intro | safe }}
{% endfor %}
The span renders with the correct html content. However if I try to set a variable without a loop:
{% set job = resume.applications %}
<span>{{ job.fields.intro }}</span>
I get nothing? Not sure what I'm doing wrong. Thanks.
I get nothing
Im trying to append the name of the variable in img alt attribute,
from the official documentation, they're hard-code the alt text.
is there any working solution?
{% for product in products %}
{{ product | img_tag: 'Picture of {{ product.name }}' }}
{% endfor %}
Thank you
You must do your logic outside and pass it as a variable.
{% for product in products %}
{%- capture image_alt -%}Picture of {{ product.title }}{%- endcapture -%}
{{ product | img_tag: image_alt }}
{% endfor %}
And there is no product.name object but there is product.title.
I am setting up a GitHub page which uses Jekyll. I know how to create a new post, new page. I wanted a new "posts" like the page, wherein I can add posts which I want to. So there will be a posts page (by default) and there will be some other page say blog, both of which shows some posts in the appropriate category.
You can create a page which lists all posts which have a certain category or tag.
Example code from the link:
---
layout: page
---
{% for post in site.categories[page.category] %}
<a href="{{ post.url | absolute_url }}">
{{ post.title }}
</a>
{% endfor %}
If the .md files you are talking about aren't posts, you can use Collections.
Here's example code from the link tailored to your xyz example - basically, you define your collection in the config file:
collections:
- xyz
Then, you create .md files in an _xyz folder, and you can display a list of them like this:
{% for item in site.xyz %}
<h2>{{ item.title }}</h2>
<p>{{ item.description }}</p>
<p><a href = "{{ item.url }}" >{{ item.title }}</a></p>
{% endfor %}
Why is it the images will not display if I'm going to loop them like this?
{% for item in content.field_room_gallery_pictures['#items'].getValue() %}
<div class="col-xs-6 col-md-4 image-item">
{{item.value}}
</div>
{% endfor %}
And when I fetched the data if there is or not using kint.. it will display an array with the value of 3 using this code..So it means in my field_room_gallery_pictures there is an array of data.
{{kint(content.field_room_gallery_pictures['#items'].getValue())}}
have your tried this:
{% for i in 0..content.field_room_gallery_pictures|length %}
{%if content.field_room_gallery_pictures[i]['#item'].entity.uri.value != "" and content.field_room_gallery_pictures[i]['#item'].entity.uri.value is not empty %}
<div>
{{ content.field_room_gallery_pictures[i]}}
</div>
{%endif%}
{%endfor%}
works fine for me.
i am using Symfony for translate my Twig-templates. Everything is working fine but i need to set other images when a user change the language. How do i do that with Symfony and Twig?
Thank you
You can access locale variable from Twig using app.request.getLocale() and use it like:
# template
{% locale = app.request.getLocale(); %}
{% image '#AppBundle/Resources/public/images/' ~ locale ~ '/example.jpg' %}
<img src="{{ asset_url }}" alt="Example" />
{% endimage %}
Or you can try storing path to your images like regular translation resources:
# messages.en.yml
image.example: #AppBundle/Resources/public/images/en/example.jpg
# template
{% image 'image.example'|trans %}
<img src="{{ asset_url }}" alt="Example" />
{% endimage %}