from string import Template
def main():
cart = []
cart.append(dict(item='coke',price=11,qty= 1))
cart.append(dict(item='cake',price=12,qty=6))
cart.append(dict(item='fish',price = 1,qty =4))
t = Template("$qty * $item = $price")
total = 0
print "Cart"
for data in cart:
print t.substitute(data)
total += data["price"]
print "Total: %s"%(total,)
if __name__ == "__main__":
main()
from string import Template
class MyTemplate(Template):
delimiter = '&'
def main():
cart = []
cart.append(dict(item='coke',price=11,qty= 1))
cart.append(dict(item='cake',price=12,qty=6))
cart.append(dict(item='fish',price = 1,qty =4))
t = MyTemplate("&qty * &item = &price")
total = 0
print "Cart"
for data in cart:
print t.substitute(data)
total += data["price"]
print "Total: %s"%(total,)
if __name__ == "__main__":
main()
Cart 1 * coke = 11 6 * cake = 12 4 * fish = 1 Total: 24
>>> t = Template(“$you owe me $$0.”) >>> t.substitute(dict(you=’James’)) “James owe me $0.”
>>> t = Template(“The ${back}yard is far away.”) >>> t.substitute(dict(back=’ship’)) “The shipyard is far away.”