How do you pass values to a template

65 views
Skip to first unread message

Tomas Schertel

unread,
Jul 12, 2012, 4:53:10 PM7/12/12
to we...@googlegroups.com
I'd like to know how you guys pass values to a template.
If I have a form with, let's say, 30 fields on it, it would be ugly and complicated to have a "def with" statement with 30 variables.
I think a good way to solve this is passing a dictionary to my template. Isn't it?
Who can show me what do you use in this situation?

Thanks.

Zagfai Kwong

unread,
Jul 13, 2012, 5:06:47 AM7/13/12
to we...@googlegroups.com
Something I wrote days ago...

inside an app... 
        # make topics for udata
        udata[ 'topics' ] = []
        tmp = client.get.statuses__friends_timeline( count = 50, page = 1 )
        for topic in tmp['statuses']:
            retweet = {}
            try:
                retweet = { \
                    'screen_name':  topic['retweeted_status']['user']['screen_name'],
                    'text'       :  topic['retweeted_status']['text'],
                    }
            except: pass
            udata[ 'topics' ].append({ \
                'screen_name':  topic['user']['screen_name'],
                'avatar_url':   topic['user']['profile_image_url'],
                'text':         topic['text'],
                'thumbnail_pic':topic.get('thumbnail_pic'),
                'retweet':      retweet,
                })
        return render.index( udata )


index.html
$def with (user)
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>$user['screen_name'] 的微新報</title>
    <style type="text/css">
    body {
    background-color:#b0c4de;
    }
    div {
        display: block;
    }
    </style>
</head>
<body>
    <div id="head">
      <img src="$(user['avatar_url'])" />
        $user['screen_name'] : $user['lats_weibo']
    </div>
    <div id="topic_container">
    $for topic in user['topics']:
        <dl class="$loop.parity">
            <dt id="avatar">
                <span><img src="$(topic['avatar_url'])" /></span> 
                <span>
                    $topic['screen_name'] :$topic['text']
                    <img src="$(topic['thumbnail_pic'])" />
                </span>
            </dt>
            </dd>
            $if topic['retweet']:
                <dd id="retweet">
                    $topic['retweet']['screen_name'] :$topic['retweet']['text']
                </dd>
        </dl>
    </div>
</body>
</html>

Tomas Schertel於 2012年7月13日星期五UTC+8上午4時53分10秒寫道:

Tomas Schertel

unread,
Jul 13, 2012, 7:06:31 AM7/13/12
to we...@googlegroups.com
Nice!
Thanks for sharing this, Zagfai.
Reply all
Reply to author
Forward
0 new messages