I would like to know if that's possible to get an image with {% path %} or {% media %} in a twig template from sonata, without crop/resize ? It seems that it's required to specify a format, but I would like to keep the original size in some case.
I have the idea to make a resizer that does nothing for this problem, but is it possible to specify a resizer by context and not a resizer for a provider in all contexts please ? Because I need to use a resizer in some case, but not in all.
If you want to display your uploaded image without resize / crop in twig you can do :
{% media media, 'reference' %}
More details in the documentation on how to use the twig helpers.
Related
I need a little help.
Imagine that in database for every user have stored color of background.
Everytime when user login, first for that user in some folder is generated css file with name of id of user and included in html template.
I need help to understand how to generate css file ?
Thanks.
I Symfony with FOSUserBundle, you can use app.user to get the user in your twig files.
If you open a MySQL console and enter describe fos_user; then you will see all the fields you can use in your Twig file. So in your table you could store for each of the FOS users a color
Then in Twig you could use something like this:
{% if app.user %}
{% set backColor = app.user.color %}
{% else %}
{% set backColor = 'none' %}
{% endif %}
...
<body style="background-color:{{ backColor }};">
You get the idea...
You might want to figure out a way to store the color preference for the FOS user using a Symfony form. And then store preference in database. You can raise a separate question for that if you have difficulties - it should be easy.
Quite simple...
You need to generate a twig file for every user in your desired folder, and place "inline" CSS in it.
{% block css %}
<style>
.class {
parameter: {{ entity.value }};
}
</style>
{% endblock css %}
To generate the file, create a service, which you will call when a page is loaded.
It will have to first check if the file exist, if it does, it will read from it (no query to the DB), if not, it will generate the file, and read need data from DB.
Look at answer from me and Gopal on this page to know how to make a service How to use session in Symfony.
You sould be able to use it for your need... ;)
Also look at the Symfony Filesystem Component
file_put_contents() should help you write content of the file... ;)
Trying to build a responsive email template in a Twig file. Using Swiftmailer within a Symfony application to send the emails. Two issues I am having so far is the images only show when I view the email on my localhost not when the email is viewed in Gmail, Inbox or Yahoo!
Linking the images using an absolute path (currently). Also, tried a relative path with the same result.
<img src="{{ absolute_url(asset('bundles/coreecommerce/images/logo.png')) }}">
Problem #2 is include stylesheets. I have some media queries in a stylesheet I am trying to include within a head tag like this.
{% block stylesheets %}
<link href="{{ asset('bundles/coreecommerce/css/email.css') }}" rel="stylesheet" />
{% endblock %}
I'm doing something wrong here. Just not sure what? Can you include stylesheets, inline styles for a responsive email in Twig?
Thanks for any help.
To inline images in your html mails you should take a look at this section of the documentation.
To show you the example with twig, you can start with something like this:
<?php
$message = \Swift_Message::newInstance();
$fileToEmbed = 'a path to image file';
$templateParams = [
'fileToEmbed' => $message->embed(\Swift_Image::fromPath($fileToEmbed)),
];
// then compose the mail like this
$message
// set subject etc...
->setBody($this->twigTemplating->render('yourMailTemplate.html.twig', $templateParams), 'text/html')
;
And then in you twig template you just create a IMG tag with template variable:
<img src="{{ fileToEmbed }}">
So TL;DR you just need that $message->embed(\Swift_Image::fromPath($fileToEmbed)) return value to be passed to your mailing template and use it as src attribute.
To locate the image file within your bundle you can use Kernel::locateResource method like this (assuming that your logo.png is located in CoreEcommerceBundle/Resources/public/images/ directory):
$fileToEmbed = $this->kernel->locateResource('#CoreEcommerceBundle/Resources/public/images/logo.png');
I try to make a web site with Symfony. So I installed SonataAdminBundle and SonataMediaBundle.
And I have a question about Sonata media.
I created a gallery with a lots of image media in it and I don't know how I can access it from my Controller.
How can I get my media (image) contained in my gallery for show them in my template?
thx for help !
EDIT:
I solved my problem !
So in my template I do:
{% for img in MyEntity.gallery.galleryHasMedias %}
<img src={% path img.media, 'reference'%}>
{% endfor %}
Usually Gallery or Media entity are related to other entities and we retrieve them from their relation.
e.g. from owner of a gallery or related medias to a product. after fetching pass media object(image in this case) to template(twig or php) and use sonata media helpers to display them.
Say I'm having a base template like this:
// Default/index.html.twig
{% block javascripts %}
<script>//some script</script>
{% endblock %}
<div>
{{ render(controller(MyControllerBundle:Default:header)) }}
</div>
{{ text }}
<div>
{{ render(controller(MyControllerBundle:Default:footer)) }}
</div>
And this renders controllers having these templates:
// Default/header.html.twig
Header content
{% block javascripts %}
<script>//some additional scripts from the header</script>
{% endblock %}
and
// Default/footer.html.twig
Footer content
{% block javascripts %}
<script>//some additional scripts from the footer</script>
{% endblock %}
Is it possible somehow to use the javascripts block from the rendered sub controllers in the parent template?
I want to have all javascripts cumulated in one place.
Rendering from bottom up with extending is no option here because the template consists of
multiple blocks that are rendered by different controllers.
Is this possible somehow? Or is there a better approach to this?
Anything is possible however design-wise it might not be a good idea.
The render tag is really useful when it comes down to scaling and it used as a way to isolate a request. Each render call is considered as a sub-request and a cache strategy can be applied to it.
I'd highly advise you to read this documentation about HTTP caching and especially the part that talks about Edge Side Includes (or ESI).
When you use the render tag, think of it as a module you want to include in multiple pages and eventually cache.
You shouldn't interact with the master request because the sub request is isolated for caching (depending on the place you embed the render tag, the master request will be different which means you might get some unexpected results).
First of all, I'd create a layout template that every other pages extends. The layout template will declare all the basic blocks (javascript, css, footer, header, <head>, <body> - you can abstract in more templates if you want).
If you have logic for your footer or header split them into Twig functions (or filters) and handle the logic in Twig but keep it light (if it's too complicated or too spaghetti that means there is another way).
Avoid having multiple Javascript or CSS files per page. If you have some css or javascript that appears on some pages but not all of them it's still probably a good idea to merge them into one file (less DNS calls on the client side and once it's cached it will be faster to load the page).
If you have a administrator.js kind-of file, then you could include it as a separate file but if most requests come from administrators then you might want to include it with all the other files.
If you didn't know you can combine assets (js or css) into one file: more info in the Symfony documentation.
I didn't answer your "how" question because I'd strongly advise you to not implement such a system however I believe I've shared good guidelines to make an informed decision.
when extending / rendering other content in TWIG you can call the parent block: http://twig.sensiolabs.org/doc/functions/parent.html
this means that you can leave default as it is and inside header / footer define
{% block javascripts %}
{{ parent() }}
{# other scripts #}
{% endblock javascripts %}
I would suggest that you have different block name for the footer - that way you can include scripts outside of the header.
Also, it might be best to keeps scripts in one place - that way you can use assetic rewrite's later down the line : http://symfony.com/doc/current/cookbook/assetic/asset_management.html#including-javascript-files
exactly what #Pazi says in the comment: Do you need a controller? It looks pretty simple to just include the template by itself, without using a controller.
You might use the include tag to include the subtemplates.
{% include 'MyControllerBundle:Default:header.html.twig' %}
For reusing the javascript block from the rendered sub controllers, you could create a base template that contains the javascripts block. Then extend that base template file in your header and footer. Or just including the base template in them should work, too.
I am (still) trying to introduce http://xoxco.com/clickable/jquery-tags-input into a dedicated bundle. As far, I have a type as a child of text and a data transformer that converts comma-separated strings into arrays of Objects and vice versa.
Now I want to decorate the text field with the JQuery code linked above. As far as I understand, I have to define a block like
{% block manytomanycomboselector_widget %}
{% spaceless %}
{{ block('text_widget') }}
<script>
$(function(){
$("#{{ id }}").tagsInput();
});
</script>
{% endspaceless %}
{% endblock manytomanycomboselector_widget %}
in [MyTypeBundle]Resources/views/Form/fields.html.twig
Now, both the documentation and the answers for this question at StackOverflow state that I have to reference fields.html.twig somewhere either in the template that uses the form or in app/, but this doesn't seem to be necessarily for other field-type bundles, though I cannot see in their code why.
What do I have to configure inside the bundle besides this block in this file?
Also I didn't get where I have to put the css and js requirements for the header and how I deal with general requirements like jQuery itself.
I have the same issue & I resolve it by merging my field template in the twig.form.resources parameter.
So, in the DI extension of my bundle (MyBundle/DependencyInjection/MyBundleExtension.php), I add:
$container->setParameter('twig.form.resources', array_merge(
array('MyBundle:Form:field_widget.html.twig'),
$container->getParameter('twig.form.resources')
));
Be aware, your bundle must be registered after the TwigBundle in your AppKernel.
EDIT:
A form field is not linked to any JS or CSS. So, IMO, you have 2 solutions.
Firstly, you directly wrap your JS & CSS in your field template and your bundle stays stand-alone.
Secondly, you instruct final users that they need to include manually some JSS & CSS each time they use your field type.
The IoFormBundle & GenemuFormBundle uses the second solution like explain in their documentation.