我在Glitch中使用a-frame
,我想将我的HTML文档拆分为更易于管理的块。举个例子,当我在<a-assets>
标签中有很多资产时,将它放在一个单独的文件中会很好,这只是一个例子而且我正在寻找一个通用的解决方案来分离可能相当的东西。大文件。
通常(即在Glitch之外)我会通过将文件名从.html
更改为.php
来实现此目的,然后使用PHP include来引用我保存在不同文件中的一大块HTML 。例如,我会有一个HTML文件,只有像这样的资产;
<a-assets>
<!-- all my images and mixins -->
...
</a-assets>
将其保存在一个名为components的文件夹中,然后在我的主文件中引用它,如此
<?php
include 'components/assets.html';
?>
然而,我无法在Glitch中实现这一点。当我将index.html
更改为index.php
然后查看应用时,我会显示文件目录而不是应用。我应该在这里说我根本不熟悉PHP并且几年前在网上发现了这个解决方案,我不会以任何其他方式使用它。
所以,这可能是因为Glitch不可能(我已经在他们的支持论坛中提出过)或者可能是这样,我做错了什么?
如果不可能,还有其他方法(可能使用js?)可以实现同样的原则吗?我已经尝试了this w3 solution;
<!DOCTYPE html>
<html>
<head>
<title>Aframe - JS include test</title>
<script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>
<script src="js/include.js"></script>
</head>
<body>
<a-scene>
<a-entity w3-include-html="components/test.html"><a-entity>
<a-entity w3-include-html="components/test2.html"></a-entity>
</a-scene>
<script>
includeHTML();
</script>
</body>
</html>
将这两个文件作为测试引用;
组件/的test.html
<a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E" shadow></a-sphere>
<a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D" shadow></a-cylinder>
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4" shadow></a-plane>
<a-sky color="#ECECEC"></a-sky>
组件/ test2.html
<a-sphere position="-3 1.25 -5" radius="1.25" color="#EF2D5E" shadow></a-sphere>
然后是js / include.js文件,如下所示
function includeHTML() {
var z, i, elmnt, file, xhttp;
/*loop through a collection of all HTML elements:*/
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
/*search for elements with a certain atrribute:*/
file = elmnt.getAttribute("w3-include-html");
if (file) {
/*make an HTTP request using the attribute value as the file name:*/
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {elmnt.innerHTML = this.responseText;}
if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
/*remove the attribute, and call this function once more:*/
elmnt.removeAttribute("w3-include-html");
includeHTML();
}
}
xhttp.open("GET", file, true);
xhttp.send();
/*exit the function:*/
return;
}
}
}
但这不可靠,它似乎只加载一个文件,甚至那似乎有点儿。我知道这与a-frame加载页面/画布的方式有关,所以我真的没想到它会起作用。它不像我在其他地方使用的PHP解决方案那样干净,可靠或简单。
让其他人遇到过这个问题吗?理解人们如何处理这个问题会很好。
如果您需要更多信息,请告知我们。
答案 0 :(得分:1)
在故障上,你可以使用类似的功能来使用带有把手的服务器端模板进行php模板化。这是一个简单的例子:
https://glitch.com/edit/#!/a-frame-ss-handlebars-templates
主要文件是views / index.hbs,它包含“head”和“assets”等模板,可以让你分解你的代码。
<!DOCTYPE html>
<html>
{{> head }}
<body>
<a-scene>
{{> assets }}
<a-entity id="poly" poly-api-obj="obj: #cycle-obj; mtl: #cycle-mtl" position="0 0 -5" scale="0.3 0.3 0.3"></a-entity>
<a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9" shadow></a-box>
<a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E" shadow></a-sphere>
<a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D" shadow></a-cylinder>
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4" shadow></a-plane>
<a-sky color="#ECECEC"></a-sky>
</a-scene>
</body>
</html>