Directory Structure
The following is the content of hogan.html
var Hogan = require('hogan.js');
mytools = {
renderHtml: function(htmlTemplate, data) {
var template = Hogan.compile(htmlTemplate);
var result = template.render(data);
return result;
}
}
var htmlTpl = '<p>{{name}}</p>';
var data = '张三';
mytools.renderHtml(htmlTpl ,data);
I am npm i --save hogan.js to install the js module.
require('hogan.js') in html
will report an error require is not defined.
Usually used directly in webpack.
How to use require??
This means you don’t have this in your
node_modules
directory. Check where you installed itI just didn’t see clearly that you wrote
require
inhtml
, which only supportsrequire
in
jsSo have you solved it? Do you want to
If you want to importrequire
insidejs
orrequire
insidehtml
?js
normallyrequire
andhtml
, just go to thenode_modules
folder and find the path tohongan.js
require
is a built-in method ofnode.js
. When you run Webpack,npm run build
actually uses node to run the webpack compilation script, so require is supported. But now that you have written it natively, you are out of touch withnode.js
, which does not natively supportrequire()
loading packages.The correct way is to add files directly using
<script>
and<link>
tags:You should write the absolute path to Hogan .js