简体   繁体   English

Javascript 变量替换(CK 编辑器)

[英]Javascript variable replacement(CK Editor)

How to replace {$name} , {$city} , {$country} with dynamic value.如何用动态值替换{$name}{$city}{$country} Dynamic value are json format.动态值是 json 格式。 My requirement: Initially only users create layout design using CKEditor.我的要求:最初只有用户使用 CKEditor 创建布局设计。 Then the user will upload CSV file.然后用户将上传 CSV 文件。 All data in CSV will replace {$value} . CSV 中的所有数据都将替换{$value} That is the concept这就是概念

<p>This is the <strong>default </strong>CKEditor installation.{$name} Source editing is provided by the Source Editing Area</a>&nbsp;plugin.</p><p>Follow the{$city}steps below to try it{$country}out:</p><ul><li>Click the <strong>Source </strong>button to display the HTML source of this tex {$website} {$email}in the source editing area.</li><li>Click the <strong>Source </strong>button again to return to the WYSIWYG view.</li></ul><form>First name:<br>

JSON format: JSON 格式:

[
        {
            "name": "Lijo",
            "city": "Banglaore",
            "country": "India",
            "website": "",
            "email": ""
        },
        {
            "name": "Thoams",
            "city": "Chennai",
            "country": "India",
            "website": "",
            "email": ""
        },
        {
            "name": "Maria",
            "city": "Mumbai",
            "country": "India",
            "website": "",
            "email": ""
        },
        {
            "name": "Dravid",
            "city": "New York",
            "country": "US",
            "website": "",
            "email": ""
        },
        {
            "name": "Sachin",
            "city": "London",
            "country": "UK",
            "website": "",
            "email": ""
        },
        {
            "name": "John",
            "city": "Canbera",
            "country": "AUS",
            "website": "",
            "email": ""
        }
    ]

You can use template literals您可以使用模板文字

 const arr = [ {name: 'foo', city: 'cityFoo', country: 'countryFoo'}, {name: 'bar', city: 'cityBar', country: 'countryBar'} ] console.log(arr.map( ({name, city, country}) => `<p>This is the <strong>default </strong>CKEditor installation.${name} Source editing is provided by the Source Editing Area</a>&nbsp;plugin.</p><p>Follow the${city}steps below to try it${country}...` ))

As op said they are getting the template from a database.正如op所说,他们正在从数据库中获取模板。 I would assume it is a string.我会假设它是一个字符串。 You could use this to create a template literal.您可以使用它来创建模板文字。

 const template = "<p>This is the <strong>default </strong>CKEditor installation.${name} Source editing is provided by the Source Editing Area</a>&nbsp;plugin.</p><p>Follow the${city}steps below to try it${country}out:</p><ul><li>Click the <strong>Source </strong>button to display the HTML source of this tex ${website} ${email}in the source editing area.</li><li>Click the <strong>Source </strong>button again to return to the WYSIWYG view.</li></ul><form>First name:<br>" const replacements = [{ "name": "Lijo", "city": "Banglaore", "country": "India", "website": "", "email": "" }, { "name": "Thoams", "city": "Chennai", "country": "India", "website": "", "email": "" } ] const inset = new Function('name', 'city', 'country', 'website', 'email', 'return `' + template + '`') const filledIn = replacements .map(({ name, city, country, website, email }) => inset(name, city, country, website, email) ) console.log(filledIn)

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

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