CKEditor in Twig Template + Symfony2

旧城冷巷雨未停 提交于 2019-12-08 07:33:15

问题


I want to use CK Editor in my Symfony 2 project. I downloaded the zip file, unzipped and put it in my js folder, I included it in the page where I want the Editor. I am following this Question Click Here now I am stuck at step 6. How can I include that php in my twig template.

The php that is in step 6 that needs to be included is

<?php

// Make sure you are using a correct path here.
include_once 'ckeditor/ckeditor.php';

$ckeditor = new CKEditor();
$ckeditor->basePath = '/ckeditor/';
$ckeditor->config['filebrowserBrowseUrl'] = '/ckfinder/ckfinder.html';
$ckeditor->config['filebrowserImageBrowseUrl'] = '/ckfinder/ckfinder.html?type=Images';
$ckeditor->config['filebrowserFlashBrowseUrl'] = '/ckfinder/ckfinder.html?type=Flash';
$ckeditor->config['filebrowserUploadUrl'] = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
$ckeditor->config['filebrowserImageUploadUrl'] = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images';
$ckeditor->config['filebrowserFlashUploadUrl'] = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash';
$ckeditor->editor('CKEditor1');

?>

Can I replace the name with any variable that contains the html and i pass it from my controller to view ?

Thanks


回答1:


CKEditor only requires HTML and Javascript. The PHP files are simply for use as utility helpers to emit the proper HTML and Javascript.

Just include the link to the ckeditor.js file and insert a textarea with the ckeditor class in your twig template. (I also found that I needed an id tag, but it didn't matter what it's value was) CKEditor will handle the rest.

<html>
<head>
    <title>Simple CKEditor Example</title>
    <script type="text/javascript" src="ckeditor.js"></script>
</head>
<body>
    <form action="" method="post">
        <textarea class="ckeditor" cols="80" id="editor1" name="editor1" rows="10"></textarea>
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

In the example you quoted, they are also using the CKFinder file manager. It does require PHP in order to handle the file manager functions. Integration instructions for that item are beyond the scope of your original question.




回答2:


You can create a form type

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CkeditorType extends AbstractType
{
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    $resolver->setDefaults(array(
      'attr' => array('class' => 'ckeditor')
    ));
  }

  public function getParent()
  {
    return 'textarea';
  }

  public function getName()
  {
    return 'ckeditor';
  }
}

add this to your service

services:
    ckeditor:
        class: Bundle\myBundle\Form\Type\CkeditorType
        tags:
            - { name: form.type, alias: ckeditor }

and use it un form like this

->add('myfield', 'ckeditor')



回答3:


Why don't you use a bundle for this feature ? There's plenty of it !

I've tried TrsteelCkeditorBundle and it works quite well ! Easy setup and configuration.



来源:https://stackoverflow.com/questions/12961884/ckeditor-in-twig-template-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!