Symfony - is it possible to edit branch templates using a web editor

We have a website that is built on top of the Symfony framework, and I was given the opportunity to allow administrators to make changes to email templates or even branch templates using the admin panel, for example, in WordPress, where you go to the editor and make changes to files.

I know that Symfony is not a CMS, and I am wondering if it is possible for administrators to edit letter templates and other branch templates in general . Templates are not stored in the database, they are in their normal location NameBundle/Resources/views , and, as you know, Symfony uses a cache in production.

While searching the web I came across Ace and CodeMirror , but again I was stuck on how to use something similar with Symfony

I would really appreciate it if anyone could push me in the right direction.

+5
source share
2 answers

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:

 # File that can be edited from the admin: /app/Resources/AcmeBundle/views/Include/menu.html.twig 

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.

+7
source

Twig allows you to use multiple template loaders , where you can store templates as a string instead of files. One example shows perhaps what you need. They use strings as patterns .

I think this function has changed recently because I remember that there used to be a Twig_Loader_String class . It looks like it has been removed from 1.15 .

In your case, you store the templates in the database and then add them to the filter chain, as here: http://twig.sensiolabs.org/doc/api.html#twig-loader-chain

Symfony configuration also added twig.loader to add new bootloaders.

+3
source

Source: https://habr.com/ru/post/1244175/


All Articles