Using the Template System
การทดสอบการใช้งาน Template ในบทความนี้ต้องทำการสร้างโปรเจคของ Django เสียก่อนจากนั้นให้ไปที่ directory ที่มีไฟล์ manage.py ให้ทำการ shell โดยใช้ command : python manage.py shell
python manage.py shell
จากนั้นพิพม์โค้ดตามด้านล่างนี้
In [1]: from django import template # import template
In [2]: t = template.Template('My name is {{ name }}.') # ให้ object t = ข้อความที่ประกาศ
In [3]: c = template.Context({'name': 'Adrian'}) # กำหนด Context name = Adrian
In [4]: print t.render(c) # แสดงผลที่ได้ หลังจากการกำหนดชื่อ
My name is Adrian.
In [5]: c = template.Context({'name': 'Fred'}) # กำหนด Context name = Fred
In [6]: print t.render(c) # แสดงผลที่ได้ หลังจากการกำหนดชื่อ
My name is Fred.
Creating Template Objects
มันเป็นเรื่องง่ายในการจะสร้าง object ของ Template เพราะ Template class จะอยู่ใน django.template module ทดสอบการสร้างจากโค้ดข้างล่างนี้
In [1]: from django.template import Template
In [2]: t = Template('My name is {{ name }}.')
In [3]: print t
<django.template.base.Template object at 0x974dd4c>
ค่าที่ได้จากการแสดงผลนั้น 0x974dd4c มันจะแตกต่างกันไปในทุกๆครั้งและไม่มีสิ่งที่เกี่ยวข้องซึ้งกันและกัน โดย python ต้องการให้เป็นเช่นนั้น
เมื่อคุณสร้าง Template object ระบบ template จะทำการคอมไพล์โค้ดอยู่ภายในโปรแกรม และพร้อมที่จะนำไปสำหรับการ rendering แต่กรณีที่ template โค้ดเกิด syntax errors ระบบ template จะทำการเรียก TemplateSyntaxError exception ดังตัวอย่างที่ได้แสดงด้านล่าง
In [1]: from django.template import Template
In [2]: t = Template('{% notatag %}')
---------------------------------------------------------------------------
TemplateSyntaxError Traceback (most recent call last)
<ipython-input-2-fdd5f59f5346> in <module>()
----> 1 t = Template('{% notatag %}')
.....
TemplateSyntaxError: Invalid block tag: 'notatag'
Rendering a Template
คุณมี template object ที่สามารถส่งข้อมูลให้กับ template นี้ได้โดยการให้ context แก่มัน context ก็คือ set ชื่อของตัวแปรใน template ค่าอื่น ๆ ที่เกี่ยวข้อง Template ใช้ context ในการ populate ตัวแปรและประมวลผล tag
context ได้อยู่ใน Django ด้วย class context ที่อยู่ในโมดูล django.template โครงสร้างของมันใช้ argument แบบ optional ซึ่งก็คือ dictionary ที่มีการจับคู่ชื่อตัวแปรกับค่าตัวแปร เรียกว่า Template object’s render() ด้วย context เพื่อเติมเต็ม (fill) ให้กับ template ดังตัวอย่างที่ได้แสดงด้านล่าง
In [1]: from django.template import Context, Template
In [2]: t = Template('My name is {{ name }}.')
In [3]: c = Context({'name': 'Stephane'})
In [4]: t.render(c)
Out[4]: u'My name is Stephane.'
จากโค้ดด้านบน name จะถูกจะคู่กับ Stephane
In [1]: from django.template import Template, Context
In [2]: raw_template = """<p>Dear {{ person_name }},</p>
...:
...: <p>Thanks for placing an order from {{ company }}. It's scheduled to
...: ship on {{ ship_date|date:"F j, Y" }}.</p>
...:
...: {% if ordered_warranty %}
...: <p>Your warranty information will be included in the packaging.</p>
...: {% else %}
...: <p>You didn't order a warranty, so you're on your own when
...: the products inevitably stop working.</p>
...: {% endif %}
...:
...: <p>Sincerely,<br />{{ company }}</p>"""
In [3]: t = Template(raw_template)
In [4]: import datetime
In [5]: c = Context({'person_name': 'John Smith',
...: 'company': 'Outdoor Equipment',
...: 'ship_date': datetime.date(2009, 4, 2),
...: 'ordered_warranty': False})
In [6]: t.render(c)
Out[6]: u"<p>Dear John Smith,</p>\n <p>Thanks for placing an order from Outdoor Equipment.
It's scheduled to\n ship on April 2, 2009.</p>\n \n <p>You didn't order a warranty,
so you're on your own when\n the products inevitably stop working.</p>\n \n
<p>Sincerely,<br />Outdoor Equipment</p>"
การจับคู่ของตัวแปร'person_name': 'John Smith'
'company': 'Outdoor Equipment'
'ship_date': datetime.date(2009, 4, 2)
'ordered_warranty': False
Multiple Contexts, Same Template
เมื่อมี Template object คุณสามารถ render context ได้หลายอัน ๆผ่าน template นั้น ๆ
In [1]: from django.template import Template, Context
In [2]: t = Template('Hello, {{ name }}')
In [3]: print t.render(Context({'name': 'John'}))
Hello, John
In [4]: print t.render(Context({'name': 'Julie'}))
Hello, Julie
In [5]: print t.render(Context({'name': 'Pat'}))
Hello, Pat
เมือไรก็ตามที่คุณใช้ template source อันเดิม ในการ render context หลาย ๆ อันอย่างนี้แล้ว มันก็จะมีประสิทธิภาพมากขึ้นในการสร้าง template object ในครั้งนั้น และจากนั้นค่อ เรียก render() หลาย ๆ ครั้ง
# Bad
for name in ('John', 'Julie', 'Pat'):
t = Template('Hello, {{ name }}')
print t.render(Context({'name': name}))
# Good
t = Template('Hello, {{ name }}')
for name in ('John', 'Julie', 'Pat'):
print t.render(Context({'name': name}))
การตัด tag ใน template ของ Django จะค่อนข้างเร็ว แต่เบื้องหลังนั้น การตัดแทกส่วนใหญ่เกิดขึ้นหลังจากการเรียก single regular expression ซึ่งเหตุการณ์ที่ว่านี่อยู่ใน engine ระดับ stark contrast จนถึง XML-based ที่ก่อให้เกิด overhead ในตัวตัด tag xml และ แนวโน้มในการจัดเรียง magnitude ช้าลงกว่า ตัว template rendering
Reference
http://www.djangobook.com/en/2.0/chapter04.html
ไม่มีความคิดเห็น:
แสดงความคิดเห็น