简体   繁体   English

如何通过插件在CKEditor中插入PHP代码?

[英]How can I insert a PHP code in CKEditor from a plugin?

I'm using Drupal 7 and the CKEditor module for a WYSIWYG textarea. 我将Drupal 7和CKEditor模块用于所见即所得的textarea。 I need to insert a PHP code like <?php echo "Hello World!"; ?> 我需要插入像<?php echo "Hello World!"; ?>这样的PHP代码<?php echo "Hello World!"; ?> <?php echo "Hello World!"; ?> in the Full HTML text format, it isn't needed to see the Hello World in the WYSIWYG mode, but it has to be in the source mode. <?php echo "Hello World!"; ?>为Full HTML文本格式,不需要以WYSIWYG模式查看Hello World ,但必须处于源代码模式。

The config file sites\\all\\modules\\contrib\\ckeditor\\ckeditor.config.js has the config.protectedSource.push(/<\\?[\\s\\S]*?\\?>/g); // PHP Code 配置文件sites\\all\\modules\\contrib\\ckeditor\\ckeditor.config.js具有config.protectedSource.push(/<\\?[\\s\\S]*?\\?>/g); // PHP Code config.protectedSource.push(/<\\?[\\s\\S]*?\\?>/g); // PHP Code line and the Full HTML text format has the PHP evalutaor checked. config.protectedSource.push(/<\\?[\\s\\S]*?\\?>/g); // PHP Code行和Full HTML文本格式已检查PHP evalutaor

So, if I write <?php echo "Hello World!"; ?> 因此,如果我写<?php echo "Hello World!"; ?> <?php echo "Hello World!"; ?> in the source mode it works perfectly . <?php echo "Hello World!"; ?>在源代码模式下, 它可以完美运行

Now I want to add a button in the CKEditor tool bar to add the next code when the button is clicked: <div id="phpcode"><?php echo "Hello World!"; ?></div> 现在,我想在CKEditor工具栏中添加一个按钮,以在单击按钮时添加下一个代码: <div id="phpcode"><?php echo "Hello World!"; ?></div> <div id="phpcode"><?php echo "Hello World!"; ?></div> . <div id="phpcode"><?php echo "Hello World!"; ?></div>

The code in my plugin.js is: 我的plugin.js的代码是:

editor.addCommand('phpcode', {
    exec : function() {
        code = editor.document.createElement('div');
        code.setAttribute('id', 'phpcode');
        code.setHtml('<?php echo "Hewllo World"; ?>');
        editor.insertElement(code);
    }
});

But the result is (Notice that the <?php and ?> are commented.): 但是结果是(注意<?php?>已注释。):

<div id="phpcode"><!--?php echo "Hewllo World"; ?--></div>

If I change the code.setHtml() for code.setText() the result is (Notice that the < and > are escaped): 如果我将code.setHtml()更改为code.setText()则结果为(注意<>被转义):

<div id="phpcode">&lt;?php echo "Hewllo World"; ?&gt;</div>

How can I insert a PHP code clicking in a CKEditor toolbar button? 如何单击CKEditor工具栏按钮来插入PHP代码?

The solution is so easy. 解决方案是如此简单。 Just change the insertElement function for insertHtml function: 只需将insertElement函数更改为insertHtml函数即可:

editor.addCommand('phpcode', {
    exec : function() {
        editor.insertHtml('<div id="phpcode"><?php echo "Hewllo World"; ?></div>');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM