开发者

CKEditor : How to apply/load automatically a template in the editor?

开发者 https://www.devze.com 2023-01-17 08:23 出处:网络
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 t

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 tem开发者_JAVA技巧plate 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.


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);
       }
    },
});


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.


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);
                }
            }
        });


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

0

精彩评论

暂无评论...
验证码 换一张
取 消