Silverstripe 从自定义表单模板中访问函数
Silverstripe accessing function from inside custom form template
在 mysite/code/Connectors.php 中,我创建了一个带有 的 表单 Page_Controller中的自定义模板这里是代码:
class Connectors_Controller extends Page_Controller {
private static $allowed_actions = array (
'TestForm',
'TestFunction'
);
public function TestFunction(){
return 'Hello World!';
}
public function TestForm(){
$fields = new FieldList(
new TextField('Test', 'Test')
);
$actions = new FieldList(
new FormAction('doSubmit', 'Submit')
);
$form = new Form($this, 'TestForm', $fields, $actions);
$form->setTemplate('ContactForm');
return $form;
}
}
我创建了一个包含页面 themename/templates/Includes/ContactForm.ss
<form $FormAttributes id="contactform" action="$Link/Connectors" method="post" class="validateform AjaxForm">
<% loop $Fields %>
$Field
<% end_loop %>
$Actions.dataFieldByName(action_doSubmit)
// I want this function to print Hello World but it doesn't
$TestFunction
</form>
在我想 运行 模板中同一控制器的另一个函数之前,它工作正常。
通常我会简单地创建一个 public 函数并在模板中调用它 - 但这不起作用。
如何从自定义表单模板中访问函数?
我尝试了各种访问它的方法,例如 $Top.TestFunction
、$TestFunction()
和 $Parent.TestFunction
谢谢
- 阿什
PHP 使用箭头而不是像其他编程语言一样的点语法。如果您尝试从 php class 的 实例 访问 属性 或函数,那么您可以使用箭头 ->
之类的所以:
$tmp = new Connectors_Controller();
echo $tmp->TestFunction();
现在,如果您还没有初始化 class 的实例,您可以 Scope Resolution Operator 像这样:
echo Connectors_Controller::TestFunction();
这将直接调用该函数,而不是在对象上调用它。
这是范围问题。当 Controller 呈现模板时,将函数放入控制器中效果很好。在您的情况下,Form 正在呈现模板,您必须告诉您的表单在应该替换 $TestFunction
时使用什么,使用 customise(),例如退货时:
return $form->customise(array(
'TestFunction' => $this->TestFunction()
));
在 mysite/code/Connectors.php 中,我创建了一个带有 的 表单 Page_Controller中的自定义模板这里是代码:
class Connectors_Controller extends Page_Controller {
private static $allowed_actions = array (
'TestForm',
'TestFunction'
);
public function TestFunction(){
return 'Hello World!';
}
public function TestForm(){
$fields = new FieldList(
new TextField('Test', 'Test')
);
$actions = new FieldList(
new FormAction('doSubmit', 'Submit')
);
$form = new Form($this, 'TestForm', $fields, $actions);
$form->setTemplate('ContactForm');
return $form;
}
}
我创建了一个包含页面 themename/templates/Includes/ContactForm.ss
<form $FormAttributes id="contactform" action="$Link/Connectors" method="post" class="validateform AjaxForm">
<% loop $Fields %>
$Field
<% end_loop %>
$Actions.dataFieldByName(action_doSubmit)
// I want this function to print Hello World but it doesn't
$TestFunction
</form>
在我想 运行 模板中同一控制器的另一个函数之前,它工作正常。
通常我会简单地创建一个 public 函数并在模板中调用它 - 但这不起作用。
如何从自定义表单模板中访问函数?
我尝试了各种访问它的方法,例如 $Top.TestFunction
、$TestFunction()
和 $Parent.TestFunction
谢谢 - 阿什
PHP 使用箭头而不是像其他编程语言一样的点语法。如果您尝试从 php class 的 实例 访问 属性 或函数,那么您可以使用箭头 ->
之类的所以:
$tmp = new Connectors_Controller();
echo $tmp->TestFunction();
现在,如果您还没有初始化 class 的实例,您可以 Scope Resolution Operator 像这样:
echo Connectors_Controller::TestFunction();
这将直接调用该函数,而不是在对象上调用它。
这是范围问题。当 Controller 呈现模板时,将函数放入控制器中效果很好。在您的情况下,Form 正在呈现模板,您必须告诉您的表单在应该替换 $TestFunction
时使用什么,使用 customise(),例如退货时:
return $form->customise(array(
'TestFunction' => $this->TestFunction()
));