If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeNow that the styles are being inlined, we can go a step further. I don't love having all my email styles inside a style
tag. It works... but will be a problem once our app sends multiple emails: we don't want to duplicate this in every template.
Nope, in the real world, we put CSS into CSS files. Let's do that. Copy all of the styles and delete them. Inside the assets/css
directory, let's create a new email.css
file. Paste!
body { | |
margin: 0; | |
padding: 0; | |
background-color: #f3f3f3; | |
font-family: Helvetica, Arial, sans-serif; | |
} | |
h1 { | |
background-color: #264459; | |
color: #ffffff; | |
padding: 30px 0 50px 0; | |
font-weight: normal; | |
} | |
hr { | |
border: none; | |
border-top: 3px solid #264459; | |
margin: 20px; | |
} | |
.container { | |
background-color: #fefefe; | |
width: 580px; | |
margin: 0 auto; | |
} | |
.bottom { | |
background-color: #efefee; | |
} | |
.block { | |
margin: 0; | |
padding: 10px 20px 20px 20px; | |
} | |
.logo { | |
width: 100%; | |
} | |
.text-center { | |
text-align: center; | |
} | |
.btn { | |
display: inline-block; | |
padding: 10px 20px; | |
background-color: #264459; | |
color: #fefefe; | |
border: 1px solid #fff; | |
border-radius: 3px; | |
font-size: 20px; | |
font-weight: bold; | |
text-decoration: none; | |
} |
So far, we've seen that the inline_css
filter is smart enough to notice any style
tags in the template and use that CSS to style the HTML tags. But you can also point the filter to an external CSS file.
Go back to config/packages/twig.yaml
. To point to the CSS file, we need to add another Twig path: let's set the assets/css
directory to styles
. So, @styles
will point here.
twig: | |
... line 2 | |
paths: | |
... line 4 | |
'assets/css': styles | |
... lines 6 - 10 |
Back in welcome.html.twig
, we can pass an argument to inline_css()
: a string of styles that it should use. To get that, use the source()
function, @styles/
and then the name of our file email.css
.
{% apply inline_css(source('@styles/email.css')) %} | |
... lines 2 - 50 | |
{% endapply %} |
The source()
function is a standard Twig function... that you don't see very often. It tells Twig to go find the file - which could be a CSS file or another Twig template - and return its contents. It's basically a file_get_contents()
for Twig. That's perfect, because inline_css()
doesn't want the path to a CSS file, it wants the string styles it should use.
Let's try this! Hit back once again in your browser, bump the email, type a password, submit and... it looks good! And this time in the HTML source, the style
tag is not there... but the inline styles are. That's another benefit of the CSS file: it got rid of the extra style
tag, which makes our email a little bit smaller.
By the way, if you prefer to use Sass or LESS for your CSS and are using Webpack Encore to compile all of that into your final CSS file, then... you have a problem. You must pass CSS to inline_css
- you can't pass it Sass and expect it to know how to process that. Instead, you need to point inline_css
at the final, built version of your CSS - the file that lives in public/build/
.
Doing that seems easy enough: you could add another Twig path - maybe called encore
- that refers to the public/build
directory. Except... if you're using versioned filenames... then how do you know exactly what the built filename will be? And if you're using splitEntryChunks()
, your one CSS file may be split into multiple!
This is a long way of saying that pointing to a CSS file with inline_css
is easy... but pointing to a Sass file is... trickier. Later, we'll walk you through how to do it.
But first! The two rules of making an email look good in every email client are, one, use a table-based layout instead of floats or flex-box. And two, inline your styles. We've done the second, now its time to do the first. Does this mean we need to rewrite our HTML to use ugly, annoying tables? Actually... no!
Hi Ryan,
You put {% apply %} to the second line of the template then you have 2 doctypes declaration into your html for email. This is normal ? I try to put the {% apply %} to the first line of the email template and then there is only one doctype declaration .
Hey Stephane,
Good catch! And nice debugging with putting it inside the apply() filter! Sorry, I totally missed that duplicated doctype in the source code :/ Basically, "twig/cssinliner-extra" requires "tijsverkoyen/css-to-inline-styles" that will add a doctype if won't find it, here's the code responsible for this: https://github.com/tijsverk...
So yes, you're right! We should move that doctype line inside of apply() Twig filter to avoid duplication. Well, it's not too important for email rendering and displaying, but anyway better do not have 2 doctypes :)
Cheers!
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.110.11
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-snappy-bundle": "^1.6", // v1.6.0
"knplabs/knp-time-bundle": "^1.8", // v1.9.1
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.23
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"league/html-to-markdown": "^4.8", // 4.8.2
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.1,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.1.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"sensio/framework-extra-bundle": "^5.1", // v5.4.1
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.3.4
"symfony/console": "^4.0", // v4.3.4
"symfony/flex": "^1.9", // v1.17.6
"symfony/form": "^4.0", // v4.3.4
"symfony/framework-bundle": "^4.0", // v4.3.4
"symfony/mailer": "4.3.*", // v4.3.4
"symfony/messenger": "4.3.*", // v4.3.4
"symfony/orm-pack": "^1.0", // v1.0.6
"symfony/security-bundle": "^4.0", // v4.3.4
"symfony/sendgrid-mailer": "4.3.*", // v4.3.4
"symfony/serializer-pack": "^1.0", // v1.0.2
"symfony/twig-bundle": "^4.0", // v4.3.4
"symfony/twig-pack": "^1.0", // v1.0.0
"symfony/validator": "^4.0", // v4.3.4
"symfony/web-server-bundle": "^4.0", // v4.3.4
"symfony/webpack-encore-bundle": "^1.4", // v1.6.2
"symfony/yaml": "^4.0", // v4.3.4
"twig/cssinliner-extra": "^2.12", // v2.12.0
"twig/extensions": "^1.5", // v1.5.4
"twig/inky-extra": "^2.12" // v2.12.0
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.2.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/browser-kit": "4.3.*", // v4.3.5
"symfony/debug-bundle": "^3.3|^4.0", // v4.3.4
"symfony/dotenv": "^4.0", // v4.3.4
"symfony/maker-bundle": "^1.0", // v1.13.0
"symfony/monolog-bundle": "^3.0", // v3.4.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.3.4
"symfony/profiler-pack": "^1.0", // v1.0.4
"symfony/var-dumper": "^3.3|^4.0" // v4.3.4
}
}
Hello team! Thanks for this course 🤓