简体   繁体   English

通过浏览器/服务器语言拥有html元素结构的最佳方法?

[英]Best way to have html element structure through browser/server languages?

Currently I'm echoing html elements using php. 目前,我正在使用php 回显 html元素。 Other times, I need to use javascript and create this elements dinamically (for example, some functions in mobile work differently and need some tweaks in the DOM). 其他时候,我需要使用javascript并动态创建此元素(例如,移动设备中的某些功能工作方式不同,并且需要在DOM中进行一些调整)。 Unfortunately I'm spanning two different dimensions of elements (or more) in the code and need to update all of them if I want to change something. 不幸的是,我在代码中跨越了元素(或更多)的两个不同维度,如果我想更改某些内容,则需要更新所有元素。 As an example: 举个例子:

Streaming long polling with javascript (jQuery): 使用javascript(jQuery)进行长轮询流式传输:

...append("<div></div><div></div><div></div><div></div>");

PHP echo PHP的回声

echo "<div></div><div></div><div></div><div></div>";

Something other in php with for each echo php中的其他东西与每个回声

foreach(...){
    echo "<div></div>";echo "<div></div>";
    (...)
    foreach(...){
       echo "<div></div>";
       echo "<div></div>"
    }
}

If I want to change the structure of this element I would have to change ALL of the 3 situations, from 4 divs to 2 divs or 1 for example. 如果要更改此元素的结构,则必须更改所有 3种情况,例如从4 div更改为2 div或1。 How exactly could I centralize all this elements throughout php and javascript? 我到底如何在php和javascript中集中所有这些元素? I thought of having a string in php and spanning as templates to javascript but it gets messed up when making dynamic classes and other properties because of concatenation. 我想到了在php中有一个字符串,并将其作为javascript的模板进行扩展,但是由于串联,在创建动态类和其他属性时会弄乱。 Does anyone know of another way to do this? 有人知道这样做的另一种方法吗?

PHP PHP

The most efficient way to output HTML with PHP is aggregating everything in a single variable and printing out once. 用PHP输出HTML的最有效方法是将所有内容汇总到一个变量中,然后打印一次。

$html = '';

foreach ($rows as $key => $value) {
  $html .= $value;
  // You may nest loops as long as you want.
}

print $html;

Here is an article about creating a simple templates How to make a simple HTML template engine in PHP 这是有关创建简单模板的文章, 如何在PHP中创建简单的HTML模板引擎

JavaScript I can't relate to jsperf.com , since it's down. JavaScript我无法与jsperf.com联系 ,因为它已关闭。 But the most efficient way to append HTML Nodes is by creating them before append. 但是,附加HTML节点最有效的方法是在附加之前创建它们。 The approach is described in What is the most efficient way to create HTML elements using jQuery? 使用jQuery创建HTML元素的最有效方法是什么中描述了该方法

// The fastest you could get.
$(document.createElement('div'));

// That's for your case.
$("<div></div><div></div><div></div><div></div>").appendTo(target);

It's hard to recommend on which technology you should focus, it mostly depends on your priorities. 很难建议您应该关注哪种技术,这主要取决于您的优先级。 Some websites are static and store their HTML in static HTML files, eg. 一些网站是静态的,并且将它们的HTML存储在静态HTML文件中,例如。 Jekyll 杰奇

Some sites are so dynamic and change a lot with user input, one of the best libraries for rendering HTML on client is React.js 有些站点是如此动态,并且随着用户输入而发生很大变化, React.js是在客户端上呈现HTML的最佳库之一。

If your website is static, then make simple template engine in PHP, if your website relies on user input a lot, then go with React.js , but don't even think of making front-end render with jQuery, you will waste your time. 如果您的网站是静态的,则可以使用PHP创建简单的模板引擎,如果您的网站非常依赖用户输入,则可以使用React.js ,但甚至不要考虑使用jQuery进行前端渲染,这会浪费您的时间时间。

If React.js looks too complex, I would recommend using Mustache Templates 如果React.js看起来过于复杂,我建议您使用Mustache模板

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

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