0

I would like a library to create inlined CSS+HTML from .html and .css files or from an HTML file with tags in the head. I would prefer a PHP library if possible.

By inlined CSS I mean something like

<span style="font: bla bla bla">Hi There!</span>

versus

<style>  
    .greeting { font: bla bla bla; }  
</style>

I often need to put HTML into emails and this would simplify the process greatly.

If anyone is interested, my current solution (for stuff that isn't restyled often) is to use the Smarty templating engine to create the document, and to assign the style="" part to a variable inside the template.

Then I can use that variable in each tag (like <td {$td_style}> (FYI - {$variable} is how smarty variables are inserted into a template) and have it generate the appropriate email-friendly HTML.

However I want something that is more general, and for which I can just feed it some HTML and CSS rather than have to convert all of it to a smarty template.

Does anyone know if a library like this exists?

9
  • This is, um, creative formatting. Commented Apr 12, 2011 at 3:28
  • 1
    This is exactly the reason why I dislike my clients having access to TinyMCE/CKEditor. Commented Apr 12, 2011 at 3:30
  • @alex why is that exactly? I was just going to suggest using a wizzywig. @Brandon: What does "from script tags" mean? Commented Apr 12, 2011 at 3:50
  • @Madmartigan I wasn't being serious, just remember cringing at text having multiple colour letters mid word. Commented Apr 12, 2011 at 4:25
  • 1
    @alex: What I love are the pastes from their MS word document, with millions of empty spans with cryptic id attributes and every other word has color:#000000; font-size:12px; font-family: Arial, Georgia, Comic Sans; or some other nonsensical font stack, and images with src that points to their hard drive. Commented Apr 12, 2011 at 4:32

1 Answer 1

1

Although I don't know of a library that handles this, if you're set on PHP, you could do this fairly easily I would think by using CSSTidy to parse the CSS, and then using an XML parser (ideal if your code is well-formed--maybe SimpleXML or the DOM extension) or one of the PHP-based HTML parsers I see out there to parse the HTML, so you can alter the HTML via DOM methods according to CSS rules.

If you don't need it tied into a PHP process though, I recommend going with JavaScript for almost anything like this, since it can work in the client-side (and in different environments) and be a handy tool for anyone wishing to do this; you should be able to parse the CSS using http://glazman.org/JSCSSP/ and then using DOMParser() and the IE equivalent as needed (for XHTML) or use innerHTML, this htmlparser, or any other HTML Parsers.

Btw, I know <style> works in emails since my add-on Color Source injects them to get syntax highlighting in emails, though I guess you were only saying it is more complicated for you to do it that way.

Sign up to request clarification or add additional context in comments.

3 Comments

I like your answer and will probably end up doing something like this when I have the time. Javascript and <style> tags don't work in either Gmail or Outlook 2007+ (among others) making them unsuitable for sending emails to a large group (sending ~20,000 newsletters to faculty & students at a University).
<style> tags don't work in Outlook 2007 or Gmail unfortunately so they are out of the question for me. I like your answer, I will most likely go the Javascript route so it can be interactive. Thanks!
Please consider sharing the result if you come up with a generic solution and are able to open-source it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.