I have had the same need lately.
I decided to put the edited content in the app/Resources/AcmeBundle/views/Include/menu.html.twig
because I did not want to put it in the Bundle. The src/AcmeBundle/Resources/views/Include/menu.html.twig
loaded the first time this page is loaded, its contents will be used as the basis for the Twig editable template. The file may also be empty.
Controller function:
/** * @Route( * "/edit/menu", * name = "page_edit_menu" * ) */ public function editMenuAction(Request $request) { $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Access denied.'); // Output file. $filepath = $this->container->getParameter('kernel.root_dir'). '/Resources/AcmeBundle/views/Include/menu.html.twig'; // Dummy object used in the form. $file = new \ArrayObject(); // Check that the output file exists. if (file_exists($filepath)) { // Load the file content. $file->content = file_get_contents($filepath); } else { // The file is missing, load the file located in the Bundle instead. $file->content = file_get_contents( $this->container->get('kernel')->locateResource( '@AcmeBundle/Resources/views/Include/menu.html.twig') ); } // Declare the form. $form = $this->createFormBuilder($file) // A textarea for the file content. ->add('content', 'textarea', array( 'attr' => array( 'rows' => 16, ) )) ->add('submit', 'submit', array( 'label' => 'Save' )) ->getForm(); $form->handleRequest($request); if ($form->isValid()) { // Save the file in app/Resources/... file_put_contents($filepath, $file->content); // Clear cache (otherwise the changes won't be applied) // Be patient, it can take about 15 seconds. $console = 'php '.$this->container->getParameter('kernel.root_dir'). '/console'; exec($console.' cache:clear --env=prod'); exec($console.' cache:warmup --env=prod'); $this->get('session')->getFlashBag()->add('notice', 'Content saved succesfully.'); return $this->redirect($this->generateUrl('page_edit_menu')); } return $this->render('editMenu.html.twig', array( 'form' => $form->createView(), ); }
Twig file
The associated Twig file editMenu.html.twig
contains only the form:
{{ form(form) }}
Using
Here's how the editable template is used, where you want to display the previously edited file:
{% include('AcmeBundle:Include:menu.html.twig') %}
Thanks to the templates that override , the app/Resources/AcmeBundle/views/Include/menu.html.twig
file will be used instead of the file in the set.
Git
If you are using git, you need to add the file path in the .gitignore
file:
Without it, updating with git can modify or delete this file.
HTML editor
If you need a WYSIWYG editor, you can install the TinymceBundle , which provides an HTML editor. Here is a quick guide to using it if you already have jQuery. Otherwise, follow the documentation.
composer require "stfalcon/tinymce-bundle:dev-master"
Simple configuration to add in app/config/config.yml
:
stfalcon_tinymce: selector: ".tinymce" language: %locale% theme: simple: theme: "modern" plugins: "link" toolbar1: "undo redo | bold italic | bullist numlist outdent indent | link"
And add 'class' => 'tinymce',
after 'rows' => 16,
and {{ tinymce_init() }}
in your Javascript.