如何搜索和替换{{:example}}中的内容

时间:2017-09-28 01:02:01

标签: javascript jquery html replace

如果在这样的内容中,可以找到替换页面上的所有内容:{{:replace_this}}

示例

<a href="#">{{:main_link}}</a>
<div>{{:some_content}}</div>
<p>{{:body_text}}</p>

结束于:

<a href="#">Home</a>
<div>Header 1</div>
<p>This is a body content</p>

我希望通过查询和PHP来实现这一点,具体取决于我可以更改此内容的值:

var X = [{main_link: "Home"},{some_content: "Header 1"}];
var Y = [{main_link: "Welcome"},{some_content: "Header 2"}];
var selector = true;
if(selector){
// change {{:this}} with X values
}else{
// change {{:this}} with Y values
}

类似的东西,并使用may:

渲染值
$("body *").each(function(){
   $(this).html(value);
});

只是一个想法。

2 个答案:

答案 0 :(得分:1)

是的,你可以做到。

使用占位符保存HTML文件,然后在php中检索,如下所示:

$html = file_get_contents('/var/www/mywebsite.com/htdocs/html.html');

然后构建一系列搜索字词:

$search = array('{{:main_link}}', '{{:some_content}}', '{{:body_text}}');

还有一个替换术语:

$replace = array($main, $content, $body);

然后执行切换器:

echo str_replace($search, $replace, $html);

答案 1 :(得分:1)

在javascript中你可以定位元素html添加#id或.class(对于groups元素),在PHP上下文中我们可以讨论twig语法。

var value = ["Home", "Header 1", "This is a body content"];
var i = 0;
$("#title_page, #content_page, #paragraph_page").each(function(){
   $(this).html(value[i]);
  i++;
});

https://codepen.io/anon/pen/EwmywJ?editors=1111