在TinyMCE中添加模板项

时间:2010-09-16 09:36:53

标签: templates dynamic tinymce

我想在tineMCE编辑器中添加“模板项”。 模板项的作用类似于动态插入数据的占位符。

一个例子:

而不是写作:“嗨{firstname},你已经{年}岁了。” 我想在保存服务器时插入一个对象而不是“{firstname}”替换为“{firstname}”。在将其加载到编辑器中时,它也应该转换回来。 应该从下拉列表中选择对象(但是一旦修复了其他内容,这应该很容易)。

1 个答案:

答案 0 :(得分:0)

在这种情况下,您需要在保存时更换占位符:

tinymce.activeEditor.onSaveContent.add(function(ed, o) {
  console.debug(o.element.nodeName);
  // do your replacement here using a regular expression and the saved value from the dropdown selection
});

或从您自己的插件的下拉选择框中选择名称。

要在加载时将其返回,您还需要将替换字符串存储在satabase中,并在使用正则表达式启动tinymce时替换它。

  // Andreas from db should be placed in a custom initialisation paramter like this:
db_firstname_save: '<?php echo $value_from_db; ?>', // $value_from_db = 'Andreas'

使用正则表达式

替换DB中的值
tinymce.activeEditor.onInit.add(function(ed) {
  console.debug('Editor is done: ' + ed.id);
  // do your replacement here using a regular expression
  ed.setcontent(ed.getContent.replace(ed.getParam('db_firstname_save'),'{firstname}'));
});

要从下拉列表中选择要保存到db的名字,您需要创建自己的插件。如何做到这一点可以在the tinymce documentation wiki找到。

相关问题