如何在smarty中搜索给定字符串中的字符串

时间:2017-06-28 14:40:12

标签: smarty string-function

我正在使用下面的代码来搜索smarty中给定字符串中的字符串,但是代码不能正常工作

from dateutil.relativedelta import relativedelta
from datetime import datetime

class Person(model.Model)
     unique_id = models.CharField(max_length=25)
     age = models.IntegerField()
     last_name = models.CharField(max_length=25)
     birth_date = models.DateField()
     city_of_birth = models.CharField(max_length=25)

     @property
     def get_unique_id(self):
         a = self.last_name[:2].upper()     #First 2 letters of last name
         b = self.birth_date.strftime('%d')     #Day of the month as string
         c = self.city_of_birth[:2].upper()     #First 2 letters of city
         return a + b + c 

     @property
     def get_age(self):
         return relativedelta(self.birth_date.days, datetime.date.now()).years


     def save(self, *args, **kwargs):
          self.unique_id = self.get_unique_id()
          self.age = self.get_age()
          super(Person, self).save(*args, **kwargs)

1 个答案:

答案 0 :(得分:1)

在Smarty中,您可以使用PHP函数 strpos 搜索给定字符串中的字符串:

{if strpos('Attribute Registration Number with value', $MyError) !== false}
  <p>Welcome</p>
{/if}

以下是an other post