我试图将包含的内容添加到字符串中.那可能吗?
例如,如果我有一个test.php文件:
echo 'a is equal to '.$a;
Run Code Online (Sandbox Code Playgroud)
我需要一个函数,比如说include_to_string包含test.php并返回字符串输入的内容.
就像是:
$a = 4;
$string = include_to_string(test.php); // $string = "a is equal to 4"
Run Code Online (Sandbox Code Playgroud) 问题:有时在我们的zend控制器中,我们不希望脚本直接输出,而是想要该脚本的内容.一个例子:当我们需要结果时,视图脚本的html输出被包含在另一个结构中,如JSON或XML,以便在客户端进行处理.
我在堆栈溢出时发现了结果,但不是那么快,因为它处于不同的上下文中.我现在已经为此困难了2天了.事实证明这很简单:
// in our controllers' action method
$this->_helper->layout()->setLayout('empty'); // disable layout
$this->_helper->viewRenderer->setNoRender(true); // make sure the script is not being rendered
// any of your code here
$html = $this->view->render('projects/climate.phtml'); // return the view script content as a string
$json = array('html'=>$html, 'initData'=>'my other needed data');
echo json_encode($json);
Run Code Online (Sandbox Code Playgroud)
我希望这很清楚,对某人有用.