2
A
回答
4
終於得償所願。
我想生成bash腳本。
建立在正確的地方模板如
myBundle/Resources/views/MyController/mytemplate.sh.twig
在ScriptGenerator類
// Load the template $template = $this->twig->loadTemplate('myBundle:MyController:mytemplate.sh.twig'); // Render the whole template $script = $template->render($parameters);
如果你想使用它,把這個代碼。
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); ... } }
2
是的,這可以做到。你已經把正確格式的路線定義範圍內的文件(.txt)
你也已經確立正確的Content-Type
頭,
$response->headers->set('Content-Type', 'text/plain');
其實Response object通常與HTML代碼填充,但它也可以包含純文本(帶有文本/純文本的Content-Type標頭),圖像或其他格式。
你也應該把你的模板的名稱格式正確,
XXXYourBundle:XXXX:temlate_name.txt.twig // In case you're using Twig as a templating Engine
+1
這很好!但我想生成該文件並將其保存在用戶文件夾中。怎麼做 ?我正在嘗試這種方法來生成郵件(http://alexandre-salome.fr/blog/Generate-Mails-With-Twig)和$ template-> render函數,但我需要填充多個塊之前呈現... – user1254498 2013-04-25 15:45:53
相關問題
- 1. 在symfony2中創建事件以渲染樹枝文件
- 2. Symfony2,樹枝和JavaScript
- 3. 創建CSS使用樹枝
- 4. 如何在樹枝和Symfony2中使用或使用條件
- 5. Symfony2中有樹枝
- 6. Symfony2 KnpLabsGaufrette - 每個樹枝獲取本地文件系統(可下載的文件)
- 7. 樹枝文件:html和樹枝部分的語法高亮
- 8. 將數據傳遞到通用樹枝文件Symfony2
- 9. Symfony2創建枝條延伸
- 10. Symfony2,樹枝和複選框自定義樹枝模板
- 11. 從Python文本文件創建xml樹
- 12. 從文本文件中創建樹
- 13. Symfony2使用樹枝基地頁Inharitance
- 14. 使用CsvFileLoader的樹枝Symfony2翻譯
- 15. 如何使用樹枝創建擴展
- 16. Drupal塊樹枝文件
- 17. css和scss在樹枝文件
- 18. Gulp關鍵CSS和樹枝文件
- 19. Symfony2 - 如何使用樹枝中的{%javascripts%}從web目錄中引用文件?
- 20. Symfony2,樹枝模板和@ORM \表
- 21. symfony2和樹枝上的子陣列
- 22. Symfony2的app.user在樹枝
- 23. Symfony2樹枝安全政策
- 24. Symfony2樹枝加載模板
- 25. Symfony2的jQuery的在樹枝
- 26. CKEditor在樹枝模板+ Symfony2
- 27. 的foreach在樹枝的Symfony2
- 28. 樹枝延伸 - symfony2環境
- 29. 樹枝逃生( 'JS')Symfony2的
- 30. Symfony2樹枝無限深度
對於純文本,Twig文件可能希望利用Raw過濾器來抑制HTML轉義。 http://twig.sensiolabs.org/doc/filters/raw.html – IanMcL 2015-09-15 10:21:39