【发布时间】:2012-12-29 11:31:44
【问题描述】:
Django 和 Angular 遵循 MVC(这种或另一种..)模式,因此应该将 HTML 与代码隐藏分开。
但是在浏览 Django 源代码时你可以很容易地发现:
class ClearableFileInput(FileInput):
....
template_with_initial = u'%(initial_text)s: %(initial)s %(clear_template)s<br />%(input_text)s: %(input)s'
template_with_clear = u'%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label>'
而不是编写模板并使用 Context 渲染它。
或
def as_table(self):
"Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
return self._html_output(
normal_row = u'<tr%(html_class_attr)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>',
error_row = u'<tr><td colspan="2">%s</td></tr>',
row_ender = u'</td></tr>',
help_text_html = u'<br /><span class="helptext">%s</span>',
errors_on_separate_row = False)
不要使用模板标签,或者使用外部模板。
与 AngularJS 相同,您可以在主页上的示例中找到:
this.addPane = function(pane) {
if (panes.length == 0) $scope.select(pane);
panes.push(pane);
}
},
template:
'<div class="tabbable">' +
'<ul class="nav nav-tabs">' +
'<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
'<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
'</li>' +
'</ul>' +
'<div class="tab-content" ng-transclude></div>' +
'</div>',
replace: true
而不是仅仅使用 templateUrl 而不是将模板写在一个单独的文件中,而不是在代码中。
这样做有充分的理由吗?还是其他一些合理的原因?
我自己没有找到,在编写小部件/指令时,我设法将 html 与代码分开,一切都按预期工作。
【问题讨论】:
-
作为一个例子,如果所有代码都捆绑在一起,看起来会更清晰。仅提供示例。
-
@asgoth 我认为将 html 放入 javascript 字符串中永远不清楚,但感谢您的评论。
-
我知道你的意思。我只是为了展示特定功能的小示例,有时最好将它们一起查看,否则学习曲线可能会更加困难。我同意这不应该用于生产代码。
标签: django model-view-controller design-patterns angularjs