使用symfony2和twig创建文件文本

use*_*498 2 php text file symfony twig

我想从带有twig的symfony2控制器生成文件文本.

像普通的html模板但使用纯文本,并将文件保存在某处.

可能吗 ?

use*_*498 5

终于明白了.

我想生成bash脚本.

  1. 在正确的位置创建模板,例如

    myBundle/Resources/views/MyController/mytemplate.sh.twig
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在ScriptGenerator类中

    // Load the template
    $template = $this->twig->loadTemplate('myBundle:MyController:mytemplate.sh.twig');
    // Render the whole template
    $script = $template->render($parameters);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 如果要使用它,请放置此代码.

    namespace myproject\myBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    
    use myproject\myBundle\Generator\ScriptGenerator;
    
    class myController extends Controller
    {
        public function myAction()
        {
            $twig = $this->get('twig');
            $generator = new ScriptGenerator($twig);
            $parameters = array(
                'a parameter' => "whatever"
            );
            $script = $generator->setScript($parameters);
    
            file_put_contents('whereyouwant',$script);
    
            ...
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)