我确定我必须遗漏某些内容,是否有办法列出string.Template
个实例中的所有模板?或者我真的必须恢复到正则表达式才能找到它们吗?
答案 0 :(得分:1)
您可以使用模板使用的正则表达式并提取名称:
$details = $this->getDetails();
$conferance_name = $details['product_name'];
$hotel = $details['hotelval'];
$datefor = $details['dateval'];
$ogtitel = $details['navn_title'];
$virksomhed = $details['virksomhed'];
$adresse = $details['adresse'];
$postnr = $details['postnr'];
$by = $details['by'];
$telefon = $details['telefon'];
$email = $details['email'];
$kommentarer = $details['kommentarer'];
$ean = $details['ean'];
$entries = $details['entries'];
$newdate = date('Y-m-d')
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "INSERT INTO mage_emailorder (emailorder_id, order_date, product_name, location, date_for, name_title, email, company, adress, zipcode, city, phone, ean, comment, order_done, number_of_entries)
VALUES (NULL, '$newdate', '$conferance_name', '$hotel', '$datefor', '$ogtitel', '$email', '$virksomhed', '$adresse', '$postnr', '$by', '$telefon', '$ean', '$kommentarer', '0', '$entries')";
文档说正则表达式有4个捕获组:
答案 1 :(得分:0)
这是一种完全不同的方式(由Lutz Horn的答案触发,但更简单):
from string import Template
from collections import defaultdict
d = defaultdict(str)
s = Template('$who likes $what $$ ${another}')
s.substitute(d)
print(d.keys())
给出预期的输出。