TinyMCE 返回模板(分配给变量或来自 API)
TinMCE returning templates (assigned to variable or from API)
我正在使用 TinyMCE,并尝试通过 API 动态加载模板。但是,即使使用基本的 return,也不会加载模板(例如将模板列表设置为变量)。它们确实从外部 JSON 文件(硬编码)加载。那么我的问题是:如何 return 或呈现自定义 TinyMCE 模板?
例如:
这有硬编码模板,所以它有效:
templates: "/Content/data/templates.json"
但我正在努力实现(在基础水平上):
templates:
function () {
var test =
{ title: 'Test template 1', content: 'Test 1' };
return tinymce.util.JSON.parse(test); // doesn't work
//return JSON.stringify(templates); // doesn't work
},
原比例(代码不全):
templates:
function () {
$.getJSON('/Template', function (result) {
var data = {};
$.each(result.ResponseObject, function (index, value) {
data.title = value.Name;
data.description = value.Name;
data.content = value.Description;
// can't figure out how to return variable
});
});
模板选项需要 Array
个模板或 URL
(作为 String
)- 您正在向它发送一个函数,这样它就不会工作。
https://www.tinymce.com/docs/plugins/template/#templates
根据文档...
"If this option is a string then it will be requested as a URL that
should produce a JSON output in the same format the option accepts."
...所以我会将您的服务器端代码更新为 return 编辑器所期望的,然后将 URL 传递到 TinyMCE 配置中。
我正在使用 TinyMCE,并尝试通过 API 动态加载模板。但是,即使使用基本的 return,也不会加载模板(例如将模板列表设置为变量)。它们确实从外部 JSON 文件(硬编码)加载。那么我的问题是:如何 return 或呈现自定义 TinyMCE 模板?
例如:
这有硬编码模板,所以它有效:
templates: "/Content/data/templates.json"
但我正在努力实现(在基础水平上):
templates:
function () {
var test =
{ title: 'Test template 1', content: 'Test 1' };
return tinymce.util.JSON.parse(test); // doesn't work
//return JSON.stringify(templates); // doesn't work
},
原比例(代码不全):
templates:
function () {
$.getJSON('/Template', function (result) {
var data = {};
$.each(result.ResponseObject, function (index, value) {
data.title = value.Name;
data.description = value.Name;
data.content = value.Description;
// can't figure out how to return variable
});
});
模板选项需要 Array
个模板或 URL
(作为 String
)- 您正在向它发送一个函数,这样它就不会工作。
https://www.tinymce.com/docs/plugins/template/#templates
根据文档...
"If this option is a string then it will be requested as a URL that should produce a JSON output in the same format the option accepts."
...所以我会将您的服务器端代码更新为 return 编辑器所期望的,然后将 URL 传递到 TinyMCE 配置中。