CKEditor:如何在编辑器中自动应用/加载模板?

发布于 2024-09-25 01:34:28 字数 580 浏览 1 评论 0原文

我在我的一个网站中使用所见即所得编辑器 CKeditor。为了更加用户友好,我想在每次使用编辑器时自动加载特定的模板。客户必须应用此模板,以便与网站的其余部分(其设计内容显示在两列中)良好集成,因此我想简化他的任务。

现在,我正在使用我在 CKeditor 论坛上找到的以下代码:

CKEDITOR.replace("newsContent", {
    customConfig : "/ckeditor/config/news.js",
    on:{ instanceReady : function( ev ) { 
           this.execCommand('templates');
       }
    },
});

这几乎很好,因为它允许打开模板选择页面,因此用户可以在编辑之前直接选择模板(他只有一个选择)。

但我想应用(唯一)模板,而不要求用户在模板选择页面中选择它。

所以我想知道是否存在像“execCommand”这样的函数来在编辑器中应用模板?我认为这是我在模板选择页面中单击模板时执行的操作,但我在文档中找不到它。

感谢您的帮助。

I'm using the WYSIWYG editor CKeditor in one of my websites. In order to be more user friendly, I would like to load automatically a specific template each time the editor is used. The customer has to apply this template in order to have a good integration with the rest of the website (which is designed with content displayed in two columns), so I would like to easy his task.

Right now, I'm using the following code that I found on CKeditor forum :

CKEDITOR.replace("newsContent", {
    customConfig : "/ckeditor/config/news.js",
    on:{ instanceReady : function( ev ) { 
           this.execCommand('templates');
       }
    },
});

That's almost good because it allows to open the template selection page, and so the user can choose directly the template before editing (he has only one choice).

But I would like to apply the (only) template without asking the user to choose it in the template selection page.

So I'm wondering if there exist a function like "execCommand" to apply the template in the editor ? I think this is the action executed when I click on the template in the template selection page, but I can't find it in the documentation.

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦途 2024-10-02 01:34:28

这个对我有用。

CKEDITOR.replace("newsContent", {
    customConfig : "/ckeditor/config/news.js",
    on:{ instanceReady : function( ev ) { 
           insert = CKEDITOR.getTemplates('your_required_template_name');
           this.setData(insert.templates[0].html);
       }
    },
});

this one works for me.

CKEDITOR.replace("newsContent", {
    customConfig : "/ckeditor/config/news.js",
    on:{ instanceReady : function( ev ) { 
           insert = CKEDITOR.getTemplates('your_required_template_name');
           this.setData(insert.templates[0].html);
       }
    },
});
恋竹姑娘 2024-10-02 01:34:28

我尝试了完全相同的事情,但我必须报告当前版本的 ckeditor ork 的解决方案不起作用。经过几个小时的时间弄清楚如何强制 ckeditor 加载模板插件(ckeditor 在打开模板对话框后完全加载模板插件),我放弃并决定尝试其他解决方案。如果您想直接使用 .setData 方法,解决方案非常简单:

  1. 在 html 中的某个位置定义模板,例如在 div 或 script 标记中,然后使用 jQuery 复制模板,
  2. 因为 ckeditor 模板的功能非常有限。应该检查 jsrender,它是一个成熟的模板引擎。

一旦您拥有所需的 html 模板,只需将其传递给 .setData。

I tried the exact same thing and i have to report that with the current version of ckeditor ork's solution does not work. After hours of spending time figuring out how to force ckeditor loading the template plugin (ckeditor loads the template plugin completely after you opened the templates dialog) i gave up and decided to try other solutions. If you get the idea to use the .setData method directly the solutions are quite simple:

  1. define the template somewhere in your html, for example in a div or script tag and copy your template with jQuery
  2. since the capabilities of ckeditor templates are quite limited you should ckeck out jsrender which is a full-flegded template engine.

As soon as you have your desired html template, simply pass it over to .setData.

相守太难 2024-10-02 01:34:28

Zima 是对的,你必须首先加载模板,否则你最终会得到 getTemplates 返回未定义的

所以像这样的东西可以解决问题:

CKEDITOR.loadTemplates(CKEDITOR.config.templates_files, '');
        var ck = CKEDITOR.replace(elm[0], {               
            on: {
                 instanceReady: function (ev) {                       
                    var insert = CKEDITOR.getTemplates('default');
                    this.setData(insert.templates[0].html);
                }
            }
        });

Zima is right you must first load the templates otherwise you end up with getTemplates returning undefined

So something like this will solve the problem:

CKEDITOR.loadTemplates(CKEDITOR.config.templates_files, '');
        var ck = CKEDITOR.replace(elm[0], {               
            on: {
                 instanceReady: function (ev) {                       
                    var insert = CKEDITOR.getTemplates('default');
                    this.setData(insert.templates[0].html);
                }
            }
        });
二货你真萌 2024-10-02 01:34:28

仅供参考,在获取模板之前需要加载模板

**CKEDITOR.loadTemplates(CKEDITOR.config.templates_files,'')**
insert = CKEDITOR.getTemplates('default');
editor.setData(insert.templates[11].html);

希望这对某人有帮助

FYI needed to load templates before getting them

**CKEDITOR.loadTemplates(CKEDITOR.config.templates_files,'')**
insert = CKEDITOR.getTemplates('default');
editor.setData(insert.templates[11].html);

hope this helps someone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文