简体   繁体   English

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

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

Is possible find a replace everything on a page if this is inside something like this: {{:replace_this}} 如果位于以下内容中,则可以找到替换页面上的所有内容的方法:{{:replace_this}}

Example

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

to end with: 以:结尾

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

I hope to do this with query and PHP where depending on the value I can change this content: 我希望使用查询和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
}

Something like that and render the values using maybe: 这样,然后使用以下方法渲染值:

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

Just an idea. 只是一个主意。

Yeah you can do that. 是的,你可以做到。

Save your HTML file with the placeholders in, then retrieve in in php like this: 使用占位符保存HTML文件,然后像这样在php中进行检索:

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

Then build an array of your search terms: 然后构建一个搜索词数组:

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

And one of you replacement terms: 其中一个替换条款:

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

Then perform the switcheroo: 然后执行switcheroo:

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

In javascript you can target you element html adding #id or .class (for groups elements), here in PHP context we can talk about twig syntax. 在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 https://codepen.io/anon/pen/EwmywJ?editors=1111

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

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