How do you translate an image with Symfony and Twig - image

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 %}

Related

NUNJUCKS setting a variable

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

Symfony2 - Displaying uploaded image in Twig from a Data Fixture or blog text

How do I display an image in Twig loaded from a Doctrine Fixture that I've uploaded to the web/images folder? It displays when calling it from the acutal Twig but not from fixtures or entering the line when creating the blog text.
I have tried this line which doesn't load when in a fixture file (or inside the blog body when creating the blog entry) BUT when I use this line inside of the actual Twig file it does the image correctly?
<img src="{{ asset(['images/', blog.image]|join) }}" />
Using this to display the blogs in twig:
{% autoescape false %}
<p>{{ blog.blog|truncate(2000) }}</p>
{% endautoescape %}
I think your missing a quote mark in example 2
try
changing:
<img class="alignleft" src="{{ asset('images/5375a30370200.jpeg) }}" alt="" width="290" height="275" />
to:
<img class="alignleft" src="{{ asset('/images/5375a30370200.jpeg') }}" alt="" width="290" height="275" />
Or set a new variable
{% set src = 'images/' ~ blog.image %}
<img class="alignleft" src="{{ asset( src ) }}" alt="" width="290" height="275" />
http://twig.sensiolabs.org/doc/tags/set.html
Turns out I have to use Twig's template_to_string function.

octopress: how to display HTML based on user path

TLDR - How do I access the current user path in octopress/jekyll?
On my Octopress blog, I would like to display an HTML element only when the user is on the root path. The trouble is that {{page.url}} returns /index.html on the root path, while my root path in _config.yml is set to '/'.
Thus, this conditional does not work:
{% if page.url == site.root %}
<div class="blurb">
<p>{{ site.description }}</p>
</div>
{% endif %}
When I change the root in _config.yml to match /index.html it breaks all of the CSS. Why is page.url pointing to index.html? There is no /index.html in the url of my live website. Is /index.html referencing a controller somewhere?
Is there an easy way to access the current user path in Octopress/Jekyll?
For reference - I am pulling the page.url variable from a Jekyll doc. {{site.root}} refers to the root value in the _config.yml file.
Thanks!
I solved this by hardcoding '/index.html' into the conditional.
{% if page.url == 'index.html' %}
<div class="blurb">
<p>{{ site.description }}</p>
</div>
{% endif %}
In source/_layouts/default.html
{% if page.front_page %} {% include front_page.html %}{% endif %}
and then add front_page: true
in index.html
---
layout: default
navbar: Blog
front_page: true
---

Twig include finding set when in a file.

I am trying to use twig / include to pull content from two files where the second file passes as a variable the 'set' content from the first. The problem is that the 'set' content cannot be seen when it comes from a file. As an example, this works
{% set localContent %}
<div> someContent </div>
{% endset %}
{% include 'MyBundle:Templates:some.html.twig' %}
{% include 'MyBundle:Templates:main.html.twig' with {
'includedContent': localContent,
} %}
where main.html.twig is simply:
{% block form_row %}
<div> mainContent </div>
{{includedContent}}
{% endblock form_row %}
and some.html.twig contains:
{% set fileContent %}
<div> someContent </div>
{% endset %}
When I change the includedContent variable to fileContent which is defined in a file I get an exception that indicates fileContent cannot be located.
Is what I am trying to do possible ?
Can anyone help me, I would like to get this application finished before the world ends in a few days :-).
I found another mechanism to accomplish the same task. If I pass the include path into the main twig file and use that to access the fileContent everything works as expected.

Symfony2 + CreateFormBuilder how to render an image in a form

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 %}

Resources