我们有一个建立在Symfony框架之上的网站,我的任务是允许管理员通过管理面板对电子邮件模板甚至小树枝模板进行更改,比如在WordPress中,您可以到编辑器那里对文件进行更改。
我知道Symfony不是CMS,我想知道是否允许管理员编辑电子邮件模板和其他树枝模板是否可以做。模板没有保存在数据库中,它们位于正常位置NameBundle/Resources/views ,而且正如我们在产品上所知道的,Symfony使用缓存。
在网上搜索的时候,我遇到了王牌编辑器和CodeMirror,但是我又一次被困在了如何使用这样的东西上
如果有人能把我推向正确的方向,我会非常感激的。
发布于 2016-03-01 10:42:14
我最近也有同样的需要。
我选择将经过编辑的内容放在app/Resources/AcmeBundle/views/Include/menu.html.twig文件中,因为我不想将其放入包中。第一次加载文件src/AcmeBundle/Resources/views/Include/menu.html.twig时,它的内容将用作可编辑Twig模板的基础。文件也可以是空的。
控制器的功能:
/**
* @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文件editMenu.html.twig仅包含以下表单:
{{ form(form) }}用法
下面是在要显示以前编辑过的文件的地方如何使用可编辑模板:
{% include('AcmeBundle:Include:menu.html.twig') %}多亏了重写模板,app/Resources/AcmeBundle/views/Include/menu.html.twig文件将被使用而不是绑定中的文件。
Git
如果使用git,则必须在.gitignore文件中添加文件路径:
# File that can be edited from the admin:
/app/Resources/AcmeBundle/views/Include/menu.html.twig否则,使用git的更新可能会更改或删除此文件。
HTML编辑器
如果您想要一个所见即所得编辑器,您可以安装TinymceBundle,它提供了一个HTML。如果您已经拥有了jQuery,这里有一个使用它的快速指南。否则,请按照文档操作。
composer require "stfalcon/tinymce-bundle:dev-master"添加到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"并在Javascript中的'class' => 'tinymce',后面加上'rows' => 16,和{{ tinymce_init() }}。
发布于 2016-03-01 08:21:50
Twig允许您使用多模板加载器,您可以将模板存储为字符串而不是文件。其中一个例子可能正好显示了您所需要的。他们用的是字符串作为模板。
我认为这个特性最近发生了变化,因为我记得曾经有过班级。从1.15开始它就被移除了。
在您的示例中,您将模板存储在数据库中,然后将它们添加到过滤器链中,如下所示:http://twig.sensiolabs.org/doc/api.html#twig-loader-chain
Symfony配置还带有twig.loader标记,用于添加新的加载器。
https://stackoverflow.com/questions/35715610
复制相似问题