繁体   English   中英

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

[英]How search and replace content inside {{:example}}

如果位于以下内容中,则可以找到替换页面上的所有内容的方法:{{: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
}

这样,然后使用以下方法渲染值:

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

只是一个主意。

是的,你可以做到。

使用占位符保存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);

然后执行switcheroo:

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

在javascript中,您可以将元素添加到#id或.class(用于组元素),以html为目标,在PHP上下文中,我们可以讨论细枝语法。

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

暂无
暂无

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

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