I've got a problem with showing thumbnails using Liipimaginebundle in TWIG template.
I'm rendering an index:
return $this->render('ad/index.html.twig', array(
'ads' => $ads,
));
and in index.html.twig I'm using FOR loop to show thumbnails related to ads.
{% for ad in ads %}
//with parameter - doesn't work
{% set img = ad.mainPhoto %}
<img src="{{ img | imagine_filter('thumb') }}" />
//working fine
<img src="{{ asset('/uploads/2.png') | imagine_filter('thumb') }}" />
{% endfor %}
mainPhoto stores a path to photo related to current ad - for example:
/uploads/2.png
While using an "img" parameter, I've got an exception:
An exception has been thrown during the rendering of a template ("Parameter "path" for route "liip_imagine_filter" must match ".+" ("" given) to generate a corresponding URL.").
What is the correct way to define the path in this case?
You are passing only the path as a string to the imagine_filter, add the asset and it should work:
{% for ad in ads %}
{% set img = ad.mainPhoto|default %}
{% if img <> "" %}
<img src="{{ asset(img) | imagine_filter('thumb') }}" />
{% endif %}
{% endfor %}
Related
I want to call a custom block inside an overwritten theme-block:
{% block file_widget %}
{% if image is not null %}
{{ block('imagePreview') }}
{% endif %}
{{ block('form_widget') }}
{% endblock %}
{% block imagePreview %}
<img src="{{ image.getFullPath | imagine_filter('medium_square') }}"
alt="{{ image.filename }}"/>
{% endblock %}
The imagePreview is not shown.
But it is working when I don't use a block.
And it is also working when I dont use a FormTypeExtension and create an ImageType instead.
So I guess the file_widgetblock still has the scope of the parent form_div_layout.html.twig and there in fact no imagePreview block exists.
So how can I solve this.
I mean now I solved it by removing the block.
But I just want to know if someone has a solution to this.
Maybe there is a way for using reuseable blocks inside of custom form themes in symfony?
Finally I found the solution:
I just did not 'use' the base template explicit.
The form theme worked without this - because symfony falls back the the base form theme when it does not find a block inside the new theme file.
But it seems then you also can not use custom blocks inside this new theme file.
So this works now:
{% use 'form_div_layout.html.twig' %}
{% block file_widget %}
{% if image is not null %}
{{ block('imagePreview') }}
{% endif %}
{{ block('form_widget') }}
{% endblock %}
{% block imagePreview %}
<img src="{{ image.getFullPath | imagine_filter('medium_square') }}"
alt="{{ image.filename }}"/>
{% endblock %}
I would like to delete the first occurrence of '/' in my twig path. I need it to show image in my pdf. I don't know how to deal with it and need help from someone smarter.
<p>{% path(project.mainImage), 'reference' %}</p>
Gives me:
/upload/media/img/a6ab300301dd8e5de89bac068a16aaa9e90b2a1b.jpeg
I need:
upload/media/img/a6ab300301dd8e5de89bac068a16aaa9e90b2a1b.jpeg
Ok guys, I am just stupid.
Here is the answer:
{% set rendered %}{% path project.mainImage, 'reference' %}{% endset %}
<img src="{{ rendered|replace({'/upload':'upload'}) }}">
Works like a charm.
You could use the slice Twig filter, like this:
{% set rendered %}{% path project.mainImage, 'reference' %}{% endset %}
<img src="{{ rendered|slice(1) }}">
UPDATE:
And if you only want to do it on routes starting with /, you could do like this:
{% set rendered %}{% path project.mainImage, 'reference' %}{% endset %}
{% if rendered|slice(1) == '/' %}
<img src="{{ rendered|slice(1) }}">
{% else %}
<img src="{{ rendered }}">
{% endif %}
I have this code:
href="{{ path('new') }}"
Now is necessary use one variable in this section:
href="{{ path(item.ruta) }}"
But this show a error:
An exception has been thrown during the rendering of a template
("Unable to generate a URL for the named route "" as such route does
not exist.").
How can solution this problem?
It seems that item.ruta is empty, so no route could be generated.
You could specifiy a fallback like this {{ path(item.ruta ? item.ruta : 'new') }} or if you want to stay on the current page you need to do something like descripted here: get current url in twig template?
{% if item.ruta %}
href="{{ path(item.ruta) }}"
{% else %}
href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}"
{% endif %}
or if you really want # only, then remove the path function call
{% if item.ruta %}
href="{{ path(item.ruta) }}"
{% else %}
href="#"
{% endif %}
In eZ Publish 5, I have a class with an image attribute.
Within the twig template, I can get the relative url (e.g. '/var/site/storage/image/...') from the attribute parameters.
Now I need the absolute url. Which is the best way to get it?
Should I get the request information and append the base url to the relative one? Or there's a built in method for that?
Thanks
You should do something like this:
{{ asset( ez_image_alias( content.getField( 'image' ), content.versionInfo, 'original' ).uri, absolute=true ) }}
ez_image_alias is used to get the image alias (obviously), and here we get the original variation.
Then we take the uri of the image alias, and pass it to symfony asset function.
That should work.
Yes this the correct answer.
Just don't forget to add some tests before such as :
Check if the content field you want to retrieve data from exists and if that field is not empty :
{% if content.fields['myFieldIdentifier'] is defined and not ez_is_field_empty(content, 'myFieldIdentifier') %}
{# do the job #}
{% endif %}
Here's how the ImageFieldType is rendered by default :
{% block ezimage_field %}
{% spaceless %}
{% if not ez_is_field_empty( content, field ) %}
<figure {{ block( 'field_attributes' ) }}>
{% set imageAlias = ez_image_alias( field, versionInfo, parameters.alias|default( 'original' ) ) %}
<img src="{% if imageAlias %}{{ asset( imageAlias.uri ) }}{% else %}//:0{% endif %}"{% if imageAlias.width is defined %} width="{{ imageAlias.width }}"{% endif %}{% if imageAlias.height is defined %} height="{{ imageAlias.height }}"{% endif %} alt="{{ field.value.alternativeText }}" />
</figure>
{% endif %}
{% endspaceless %}
{% endblock %}
I'm attempting to make a form that builds a slider. It can have any number of images and I'd like to show a preview of already-uploaded images. Getting the multiple image field set up was easy enough, but I'm getting caught up on showing a preview of the image.
I'm using this template to render the "Slider Image" field:
{% block form_widget_simple %}
{% spaceless %}
<div class="form-widget slider">
{% set type = type|default('text') %}
{% if type == 'file' and value is not empty %}
<img src="{{ value }}" width="200"/><br/>
{% endif %}
<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
</div>
{% endspaceless %}
{% endblock form_widget_simple %}
The value variable is always empty on file input types, so I'm not sure how I can get at the url of the uploaded images. I am using a custom field type that simply adds a file field and hooks up the data source (which is just a simple wrapper around Symfony\Component\HttpFoundation\File\File). If you need this code let me know, but its all boilerplate stuff so I doubt you do.
Thanks in advance.
Symfony2 FileType doesn't have value, its owerwritten in buildView method.
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FileType.php#L28
But you access it via forms.vars.data
{% if type == 'file' and form.vars.data is not null %}
{# Here you should somehow generate url to file but let assume
you have it in "/path/to/file/" folder that is accessible through web server #}
<img src="/path/to/file/{{ form.vars.data.name }}" /><br/>
{% endif %}