我对Handlebars.js完全不熟悉,而且几乎是JavaScript本身的新手。我在控制台上收到此错误" ReferenceError:未定义Handlebars"。我已经尝试将参考文献放在" x-handlebars-templates"但它没有帮助。
index.tmpl.php:
<html>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<link rel="stylesheet" type="text/css" href="style.css">
<title>PHP and jquery</title>
</head>
<body>
<div class="container">
<h1>Search films by title:</h1>
<form id="film-selection" action="index.php" method="post">
<select name="q" id="q">
<?php
$alphabet = str_split('abcdefghijklmnopqrstuvwxyz');
foreach ($alphabet as $letter) {
echo "<option value='$letter'>$letter</option>";
}
?>
</select>
<button type="submit">Go!</button>
</form>
<?php if(isset($film)): ?>
<ul class="film-list">
<?php
foreach ($film as $a) {
echo " <li data-film_id='{$a->film_id}'>
<a href='film.php?film_id={$a->film_id}'>{$a->title} </a>
</li>";
}
?>
</ul>
<?php endif; ?>
<script id="film_list_template" type="text/x-handlebares-templates">
{{#each this}}
<li data-film_id="{{film_id}}">
<a href="film.php?film_id={{film_id}}">{{fulName this}}</a>
</li>
{{/each}}
</script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/handlebars-v2.0.0.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</div>
</body>
</html>
&#13;
的script.js:
var Film = {
init: function(config){
this.config = config;
this.setupTemplates();
this.bindEvents();
},
bindEvents: function(){
this.config.letterSection.on('change', this.fetchFilm);
},
setupTemplates: function(){
//The error is in this line below.
this.config.filmListTemplate = Handlebars.compile(this.config.filmListTemplate); //here is the error
Handlebars.registerHelper('fulName', function(film){
return film.title;
});
},
fetchFilm: function(){
alert('Test2');
var self = Film;
$.ajax({
url: 'index.php',
type: 'POST',
data: self.config.form.serialize(),
dataType: 'json',
success: function(results){
//console.log(results[0].title);
self.config.filmList.append(self.config.filmListTemplate(results));
}
});
}
};
Film.init({
letterSection: $('#q'),
form: $('#film-selection'),
filmListTemplate: $('#film_list_template').html(),
filmList: $('ul.film-list')
});
&#13;
答案 0 :(得分:2)
我发现我没有文件&#34; handlebars-v2.0.0.js&#34;在我的js目录中。