I am currently trying to help my friend with his Shopify store and I just started to learn Liquid, we want to load in a javascript file and an hmtl file but when we load the assets in and preview the website, nothing shows up except for a link.
Here is my code of the three-html.liquid
file.
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
</head>
This is the head section.
This is the body section.
<body>
<script src="three.js"></script>
<script>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
const cube = new THREE.Mesh( geometry, material );
scene.add( cube );
camera.position.z = 5;
const animate = function () {
requestAnimationFrame( animate );
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render( scene, camera );
};
animate();
</script>
</body>
And then obviously the rest of the html tags.
Now when I do {% render 'three-html' %}
in the theme.liquid
file and when I preview the store this is all I get .
We just wanna figure out how to get these files to visually display on the screen.
This is the outcome we want. The spinning cube.
Thanks.
3
Answers
It's all good 3dgifmaker.com did the job. Thanks anyways.
You need to add the
snippet
and add the content ofHTML
file content to that snippet and userender
to load it intotheme.liquid
Read more about
snippets
Link andrender
Link
short response:
three.js
andcustom.js
theme.liquid
if you need it only on a page or on some specific page, call the files on a template file.{{ 'three.js' | asset_url | script_tag }}
url filter plus html filter it render:but if you like to use some extra options like defer you should use a normal script tag plus an url filter :
Long response:
You mix some concepts, you should know the difference between the files on the different folders off a shopify theme.
directories:
assets: here go the "static" files from the site like images, fonts, css and script files, a nice function you can end the css and js files with
.liquid
and use liquid variables, it will be server rendered. to call assets you should use the asset_url filterlayout: this liquid files are the boxes containing the base of the page, like base html structure, metas, global css and script calls. It’s normal call here header and footer section too, you can create multiple layouts and use it o different pages of the site.
IMPORTANT: you shouldn’t use head and body tags anywhere else.
The base structure of layout files:
Sections: they are reusable modules of content that can be customized and re-ordered by users of the theme. Sections are very especial, because here you can set customizable parts than can be update on shopify admin, more info here.
Snippets: they’re reusable bits of code, it can be used anywhere and call by
render
you can even pass variables for extra customization.Templates: they manage the different site’s pages, (article, product, index, page, etc) you can create an different version adding an alternate template. Here you can choose witch layout file you use. the default layout is
theme.liquid
.to use an alternative layout your file should start with this line
you don’t need to add .liquid at the end, use just the layout name.