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');
Related
.tpl is an extension for templates, used by shop sites. I download the files from a site (nuvemshop.com) and the pages files came with .tpl. By the way, css is with .tpl extension too. What I could do to render these files? Maybe I need a server side renderer? Appreciate for the help :).
I will atach an example:
<div class="page-content pagina container-xs">
{% snipplet "breadcrumbs.tpl" %}
{# Page preloader #}
<div class="page-loading-icon-container full-width hidden-when-content-ready">
<div class="page-loading-icon svg-icon-secondary opacity-80 rotate">
{% include "snipplets/svg/spinner.tpl" %}
</div>
</div>
<div class="visible-when-content-ready user-content">
{{ page.content }}
</div>
</div>
```
`
It's like Jinja or Twig templates.
I try to render the template directly in browser, but obsviously does'nt work.
I partially solve it. .tpl files are templates from smarty lib and to render them, you need to use the class in a php file and start a server. I will atach my php file:
include "smarty/libs/Smarty.class.php";
$smarty = new Smarty();
$smarty->template_dir = "smarty/templates";
$smarty->compile_dir = "smarty/templates_c";
$smarty->config_dir = "smarty/configs";
$smarty->assign("msg", "Hello World!");
$smarty->display("smarty/examples/test.tpl");
The problem is: my "tpl" file is like more twig or jinja, and is not renderer by smarty because the wrong syntax. You can see the true syntax in the docs: https://www.smarty.net/
So, I will keep trying to renderer these files and I update the answer if I find a solution
I am using Django i18n translation system and there are two languages:
LANGUAGES = (
('fa', _('Persian')),
('en', _('English')),
)
Persian is Right-To-Left and I want to serve another CSS file when user change language to persian.
Or maybe there is a better way to change LTR to RTL when user change the language?
I am using nginx with wsgi on a Ubuntu vps.
One possible way:
I assume you have 2 css files for example static/css/fa.css and static/css/en.css
In template, when you link your css you can do it like this:
<link type="text/css" href="{% trans 'static/css/en.css' %}">
In your Persian translation file you put then something like this:
msgid "static/css/en.css"
msgstr "static/css/fa.css"
Alternatively, if the only thing you want from css is to change LTR to RTL you could do in your template:
{%load i18n%}
{%get_current_language as LANGUAGE_CODE%}
{%get_language_info for LANGUAGE_CODE as lang%}
....
{% if lang.bidi %}
<!-- Some HTML code for RTL -->
{% else %}
<!-- Some HTML code for LTR -->
{% endif %}
I assume you have following file and directory structure:
/static/fa/css/default.css
/static/en/css/default.css
And then I would go with:
{% get_static_prefix %}{% get_current_language %}"/css/default.css"
I asked this in Shopify forum but no one answers.
In Shopify's admin page, we can edit the HTML of "order confirmation" email, when I get the actual email, the HTML is very different from the one we edit in the admin's page, though I can tell it comes from the same HTML that I edit on admin page. It's just that many styles are added and some HTML elements' order seems changed. I am wondering how these styles are added? Do we have control over it?
This is what I see in gmail as of the order confirmation email:
As you can see the <p> tag has many styles. However, on admin's page, the related code is:
{% capture email_body %}
{% if requires_shipping %}
Hi {{ customer.first_name }}, we're getting your order ready to be shipped. We will notify you when it has been sent.
{% endif %}
{% endcapture %}
.....
<p>{{ email_body }}</p>
There is no styles at all and there is no CSS regarding this P tag.
Shopify loads default styles.css file in all email templates.
<link rel="stylesheet" type="text/css" href="/assets/notifications/styles.css">
Before sending email, Shopify parses all that external CSS and add them as inline styles to better support different email clients.
More information on Shopify Website
I want to use images in my mail template - they should be sent with the mail. How do I embed them in twig template (a special path or setting to address them?)? Do I attach them as an attachment or are there some special settings?
You can embed images into a Twig template by doing the following:
(Below assumes you've already setup the relevant Swift_Message as $message and $data may contain other information that you're passing through to the Twig template)
$data['image_src'] = $message->embed(Swift_Image::fromPath('path/to/image.jpg'));
$message->setBody(
$this->templating->render('path/to/twigtemplate.html.twig', $data)
);
Then, inside your Twig template, you can use:
<img src="{{ image_src }}" alt="Image Description">
This will embed the image inline in the email to be sent out.
You can embed elements while you are creating a body
documentation
I'm playing around with Symfony2, and I have problems including CSS and JS files in Twig template.
I have a bundle named Webs/HomeBundle inside which I have HomeController with indexAction that renders a twig template file:
public function indexAction()
{
return $this->render("WebsHomeBundle:Home:index.html.twig");
}
So this is easy. Now what I want to do, is to include some CSS and JS files inside this Twig template. Template looks like this:
<!DOCTYPE html>
<html>
<head>
{% block stylesheets %}
<link href="{{ asset('css/main.css') }}" type="text/css" rel="stylesheet" />
{% endblock %}
</head>
<body>
</body>
</html>
The file I would like to include, main.css file is located in:
Webs/HomeController/Resources/public/css/main.css
So my question is basically, how the hell do I include simple CSS file inside Twig template?
I'm using Twig asset() function and it just doesn't hit the right CSS path. Also, I run this command in console:
app/console assets:install web
This created a new folder
/web/bundles/webshome/...
this is just linking to the
src/Webs/HomeController/Resources/public/
right?
Questions
Where do you place your asset files, JS, CSS, and images? Is it OK to put them in Bundle/Resources/public folder? Is that the right location for them?
How do you include these asset files in your Twig template using asset function? If they are in public folder, how can I include them?
Should I configure something else?
You are doing everything right, except passing your bundle path to asset() function.
According to documentation - in your example this should look like below:
{{ asset('bundles/webshome/css/main.css') }}
Tip: you also can call assets:install with --symlink key, so it will create symlinks in web folder. This is extremely useful when you often apply js or css changes (in this way your changes, applied to src/YouBundle/Resources/public will be immediately reflected in web folder without need to call assets:install again):
app/console assets:install web --symlink
Also, if you wish to add some assets in your child template, you could call parent() method for the Twig block. In your case it would be like this:
{% block stylesheets %}
{{ parent() }}
<link href="{{ asset('bundles/webshome/css/main.css') }}" rel="stylesheet">
{% endblock %}
And you can use %stylesheets% (assetic feature) tag:
{% stylesheets
"#MainBundle/Resources/public/colorbox/colorbox.css"
"%kerner.root_dir%/Resources/css/main.css"
%}
<link type="text/css" rel="stylesheet" media="all" href="{{ asset_url }}" />
{% endstylesheets %}
You can write path to css as parameter (%parameter_name%).
More about this variant: http://symfony.com/doc/current/cookbook/assetic/asset_management.html
The other answers are valid, but the Official Symfony Best Practices guide suggests using the web/ folder to store all assets, instead of different bundles.
Scattering your web assets across tens of different bundles makes it
more difficult to manage them. Your designers' lives will be much
easier if all the application assets are in one location.
Templates also benefit from centralizing your assets, because the
links are much more concise[...]
I'd add to this by suggesting that you only put micro-assets within micro-bundles, such as a few lines of styles only required for a button in a button bundle, for example.
In case you are using Silex add the Symfony Asset as a dependency:
composer require symfony/asset
Then you may register Asset Service Provider:
$app->register(new Silex\Provider\AssetServiceProvider(), array(
'assets.version' => 'v1',
'assets.version_format' => '%s?version=%s',
'assets.named_packages' => array(
'css' => array(
'version' => 'css2',
'base_path' => __DIR__.'/../public_html/resources/css'
),
'images' => array(
'base_urls' => array(
'https://img.example.com'
)
),
),
));
Then in your Twig template file in head section:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
{% block head %}
<link rel="stylesheet" href="{{ asset('style.css') }}" />
{% endblock %}
</head>
<body>
</body>
</html>