php - Symfony - Is it possible to edit twig templates using web editor -


we have website built on top of symfony framework , have been given task of allowing admins make changes email templates or twig templates via admin panel in wordpress go editor , make changes files.

i know symfony not cms , wondering if allowing admins edit email templates , other twig templates @ possible do. templates not saved in database in normal location namebundle/resources/views and know on production symfony uses cache.

while searching online came across ace editor , codemirror again stuck @ how use symfony

i appreciate if can push me in right direction.

i had same need recently.

i chose put edited content in app/resources/acmebundle/views/include/menu.html.twig file because didn't want put in bundle. file src/acmebundle/resources/views/include/menu.html.twig loaded first time page loaded, content used basis editable twig template. file can 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 form.     $file = new \arrayobject();      // check output file exists.     if (file_exists($filepath)) {         // load file content.         $file->content = file_get_contents($filepath);     } else {         // file missing, load file located in bundle instead.         $file->content = file_get_contents(             $this->container->get('kernel')->locateresource(             '@acmebundle/resources/views/include/menu.html.twig')         );     }      // declare form.     $form = $this->createformbuilder($file)         // textarea file content.         ->add('content', 'textarea', array(             'attr' => array(                 'rows' => 16,             )         ))         ->add('submit', 'submit', array(             'label' => 'save'         ))         ->getform();      $form->handlerequest($request);      if ($form->isvalid())     {         // save file in app/resources/...         file_put_contents($filepath, $file->content);          // clear cache (otherwise changes won't applied)         // patient, can take 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 contain form:

{{ form(form) }} 

usage

here how editable template used want display file edited:

{% include('acmebundle:include:menu.html.twig') %} 

thanks templates overriding, app/resources/acmebundle/views/include/menu.html.twig file used instead of 1 in bundle.

git

if use git, have add file path in .gitignore file:

# file can edited admin: /app/resources/acmebundle/views/include/menu.html.twig 

without this, update git may alter or delete file.

html editor

if want wysiwyg editor, can install tinymcebundle provide html editor. here quick guide use if have jquery. otherwise follow documentation.

composer require "stfalcon/tinymce-bundle:dev-master" 

a simple configuration add 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, , {{ tinymce_init() }} in javascript.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -