If statement 模板中的SilverStripe条件

If statement 模板中的SilverStripe条件,if-statement,conditional,silverstripe,If Statement,Conditional,Silverstripe,我正在尝试修改我的一个模板,以便在除两种页面类型之外的所有页面类型上调用某个include 我使用的代码是: <% if ClassName != BusinessPage || ClassName != BusinessSubPage %> ... some code here <% end_if %> ... 这里有一些代码 这不起作用,因为if语句会传递所有页面类型,并且会触发if语句中的代码 如果我将代码缩短为一种页面类型,它将在页面类型上给出预期结

我正在尝试修改我的一个模板,以便在除两种页面类型之外的所有页面类型上调用某个include

我使用的代码是:

<% if ClassName != BusinessPage || ClassName != BusinessSubPage %>
    ... some code here
<% end_if %>

... 这里有一些代码
这不起作用,因为if语句会传递所有页面类型,并且会触发if语句中的代码

如果我将代码缩短为一种页面类型,它将在页面类型上给出预期结果:

<% if ClassName != BusinessPage %>
    ... some code here
<% end_if %>  

... 这里有一些代码

在if语句中使用
ClassName
作为条件是否存在一些意外问题,我需要注意这些问题,以解释我的问题

您的if语句逻辑不太正确。您想要的是使用
&&
)而不是
|
):


列显示,无论
类名是什么,结果总是正确的。这就是为什么if语句总是通过的原因。

另一种方法是将逻辑放入
页\u控制器中。这使得当逻辑变得复杂时,模板更容易阅读

Page.php

public function BusinessTemplate() {
  return in_array($this->ClassName, array('BusinessPage', 'BusinessSubPage'));
}
Page.ss

<% if not $BusinessTemplate %>
  enter code here
<% end_if %>

在这里输入代码
public function BusinessTemplate() {
  return in_array($this->ClassName, array('BusinessPage', 'BusinessSubPage'));
}
<% if not $BusinessTemplate %>
  enter code here
<% end_if %>