Tim
Tim

Reputation: 1023

bottle template: how to import a python package

I'm writing a bottle SimpleTemplate and I want to compare a date. How (and where) do you import python package inside a template? Is it possible?

Here is an example of what I'm trying to do. I want to compare data (a string) representing a date to today's date:

%if datetime.strptime(report['date'], '%m/%d/%Y') < datetime.today():
    <span class="ui-icon ui-icon-circle-close"></span>
%elif report['result']:
    <span class="ui-icon ui-icon-circle-check"></span>
%elif not report['result']:
      <span class="ui-icon ui-icon-alert"></span>
%end

To make that work in a normal python program, I'd have to say

from datetime import datetime

But is there a way to do that in the template?

Upvotes: 3

Views: 1518

Answers (3)

kgr
kgr

Reputation: 9948

No, it's not possible. Templates are not meant to include complex logic, they are part of the presentational layer. You should prepare all your data in the view and pass it on to the template already processed.

So in your case you should probably move your logic to the view - resulting in something along those lines:

from datetime import datetime
if datetime.strptime(report['date'], '%m/%d/%Y') > datetime.today():
    report["status"] = "due"
else:
    if report["result"]:
        report["status"] = "pending_check"
    else:
        report["status"] = "missing_result"

and then in the template check the flag, like so:

%if report["status"] == "due":
    <span class="ui-icon ui-icon-circle-close"></span>
%elif report["status"] == "pending_check":
    <span class="ui-icon ui-icon-circle-check"></span>
%elif report["status"] == "missing_result":
      <span class="ui-icon ui-icon-alert"></span>
%end

I have not used close, check and alert as report["status"] values on purpose, because best practice would be to decouple data from presentation and therefore not assign HTML class names in the view, but rather act on the processed data in the template like I did in the attached snippet.

Then if you decide to change the icons, you do that only in your template which is superior to doing presentational changes in the view (and e.g. having to restart your web server for them to take effect).

UPDATE (April 2016): Apparently it's now possible or became possible, since I answered, to import modules in Bottle templates, but it's still a bad idea :)

Upvotes: 1

BlenderBender
BlenderBender

Reputation: 566

Yes, importing modules from inside templates is possible. This is for instance used by bottlepy itself to generate the error pages:

ERROR_PAGE_TEMPLATE = """
%%try:
    %%from %s import DEBUG, HTTP_CODES, request, touni
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html>
        <head>
            <title>Error: {{e.status}}</title>
...

Upvotes: 2

Lars
Lars

Reputation: 1959

i just tried:

%from mymodule import *

and it seemed to work, no time to check further, i think importing is just calling a python method and therfore should work.

Upvotes: 1

Related Questions