I had the same need recently.
I chose to put the edited content in a app/Resources/AcmeBundle/views/Include/menu.html.twig
file because I didn't want to put this in a Bundle. A file src/AcmeBundle/Resources/views/Include/menu.html.twig
is loaded the first time this page is loaded, its content will be used as a basis for the editable Twig template. The file can also be empty.
Controller's 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
only contain the form:
{{ form(form) }}
Usage
Here is how the editable template is used where you want to display the file previously edited:
{% include('AcmeBundle:Include:menu.html.twig') %}
Thanks to templates overriding, the app/Resources/AcmeBundle/views/Include/menu.html.twig
file will be used instead of the one in the Bundle.
Git
If you use git, you have to add the file path in the .gitignore
file:
# File that can be edited from the admin:
/app/Resources/AcmeBundle/views/Include/menu.html.twig
Without this, an update with git may alter or delete this file.
HTML editor
If you want a WYSIWYG editor, you can install TinymceBundle which provide an HTML editor. Here is a quick guide to use it if you already have jQuery. Otherwise follow the documentation.
composer require "stfalcon/tinymce-bundle:dev-master"
A simple configuration to add to 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.