Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import jinja template from external file #74

Closed
dalepotter opened this issue Jan 10, 2017 · 1 comment
Closed

Import jinja template from external file #74

dalepotter opened this issue Jan 10, 2017 · 1 comment

Comments

@dalepotter
Copy link
Contributor

Hi, nice library!

Is it possible to import a template from an external file (for example template.html)? I've adapted the example code in the docs, but am getting a TypeError - see below.

If this is possible, I'm happy to send a pull request to extend the docs examples.

sendemail.py

import emails
from emails.template import JinjaTemplate as T

message = emails.html(
     subject=T('Payment Receipt No.{{ billno }}'),
     html=T(open('template.html')),
     mail_from=('ABC', '[email protected]')
     )

r = message.send(...)

This gives:

$ python sendemail.py
Traceback (most recent call last):
... 
TypeError: Can't compile non template nodes
@lavr
Copy link
Owner

lavr commented Jan 11, 2017

Hi!

JinjaTemplate constructor receives unicode string as first argument, so try T(open('template.html').read())

For non-ascii content you should probably decode text to unicode: T(open('template.html').read().decode('latin-1'))

@lavr lavr closed this as completed Jan 12, 2017
dalepotter added a commit to dalepotter/python-emails that referenced this issue Jan 16, 2017
dalepotter added a commit to dalepotter/python-emails that referenced this issue Feb 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants