在#if() #elseif() 语句中使用#set() 指令

Using the #set() directive in an #if() #elseif() statement

我正在尝试向电子邮件添加动态内容,并希望从电子邮件正文中删除速度语法,并通过使用 #set 指令将其替换为 $var,如下所示:

#if('x' = 'y') #set($z = 'z') #elseif( 'a' = 'b') #set($c = 'c')

这是我的尝试:

 #if( {$date_opened_5ca62c21c455a} == "" && {$firstName} != "" )
 #set($header_greeting = "{$firstName}, Community Member.")
 #elseif( {$date_opened_5ca62c21c455a} != "" && {$firstName} != "" )
 #set( $header_greeting = "{$firstName}, member since 
 {$date_opened_5ca62c21c455a}")
 #else
 #set( $header_greeting = "Building Tomorrow Together" )
 #end

我收到一条错误消息,提示关闭 ) 是预期的

我也在黑暗中拍了一张:

 #set( $header_greeting = "#if( {$date_opened_5ca62c21c455a} == "" && {$firstName} != "" ){$firstName}, Community Member.#{elseif({$date_opened_5ca62c21c455a} != "" && {$firstName} != "")}{$firstName}, member since {$date_opened_5ca62c21c455a}#{else}Building Tomorrow Together#end" )

您错过了正确的形式符号语法,它是:

${foo}

而不是

{$foo}

但作为旁注,您的示例中似乎不需要正式符号,因为它仅用于模棱两可的情况,也就是。 $foo.bar 反对 ${foo}.bar.

此外,您的第一个示例使用 = 而不是 == 来表示相等。

除其他事项外,您还必须验证缺失值是否为空字符串,而不仅仅是缺失值。 $foo 呈现为 $foo 而不是空字符串(如果缺少该值)。要检查 null 或 empty,您可以使用无提示符号:

#if("$!foo" == "") ...

有了所有这些评论,您的尝试可以重写为:

#if( "$!date_opened_5ca62c21c455a" == "" && "$!firstName" != "" )
  #set( $header_greeting = "$firstName, Community Member." )
#elseif( "$!date_opened_5ca62c21c455a" != "" && "$!firstName" != "" )
  #set( $header_greeting = "$firstName, member since $date_opened_5ca62c21c455a" )
#else
  #set( $header_greeting = "Building Tomorrow Together" )   
#end

最后但同样重要的是,如果 $foo 为 null 为空,#if($foo) 的默认行为是 return false,因此您可以写:

#if( !$date_opened_5ca62c21c455a && $firstName )
  #set( $header_greeting = "$firstName, Community Member." )
#elseif( $date_opened_5ca62c21c455a && $firstName )
  #set( $header_greeting = "$firstName, member since $date_opened_5ca62c21c455a" )
#else
  #set( $header_greeting = "Building Tomorrow Together" )   
#end

这是通过 SharpSpring crm 中的速度脚本让合并标签与 set() 指令一起运行的解决方案。问题是在字符串内部使用了合并标签,而不是将其连接起来。

#if( {$date_opened_5ca62c21c455a} == "" && {$firstName} != "" )

#set( $headerGreeting = {$firstName} + ", Community Member." )

#elseif( {$date_opened_5ca62c21c455a} != "" && {$firstName} != "" )

#set( $headerGreeting = {$firstName} + ", member since " + {$date_opened_5ca62c21c455a} )

#else

#set( $headerGreeting = "Building Tomorrow Together" )

#end