小编典典

Javascript变量替换(CK编辑器)

reactjs

如何更换{$name}{$city}{$country}
动态价值。动态值是json格式。我的要求:最初只有用户使用CKEditor创建布局设计。然后,用户将上传CSV文件。CSV中的所有数据将替换{$value}。那是概念

<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格式:

[
        {
            "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": ""
        }
    ]

阅读 333

收藏
2020-07-22

共1个答案

小编典典

正如op所说,他们正在从数据库中获取模板。我认为这是一个字符串。您可以使用它来创建模板文字。

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)
2020-07-22