5

拡張する場合doLayout、サブテンプレートは 1 つしか持てませんが、include複数持つことができます。

違いは何ですか?ベストプラクティスは何ですか?

4

2 に答える 2

6

それらは互いに逆のようなものです。

doLayoutextendsタグの一部として使用されます。extends タグは拡張するテンプレートをdoLayout指定し、タグは拡張テンプレート内のどこにコードを挿入するかを指定します。

このincludeタグは、指定された時点で別のテンプレートを挿入する必要があることを単に指定します。

そのため、doLayout タグが挿入されるテンプレートを指定しないことを除いて、doLayout はインクルードと同様の方法で機能します。これは extends タグによって行われ、テンプレート (通常はヘッダー、フッター、および一般的な css と javascript を含む) を、拡張しているテンプレートについて何も知らなくても拡張できることを意味します。

Include、コードの単なるばかげた注入です。

インクルードを使用して doLayout 機能を実現したい場合 (これは、PHP などで行う方法です)、次のようにすることができます。

#{include 'header.html'}
your template code
#{include 'footer.html'}

これは、テンプレートのすべてのページで複製する必要があります。extendsand を使用doLayoutすると、簡単に実行できます

#{extends 'template.html'}

そして、コードが template.html に挿入される場所は、doLayout タグによって管理されます。

extends アプローチは、単により適切な方法です。また、ページのレイアウトを変更する場合は、1 つのファイルを更新するだけで済み、拡張テンプレート内のコンテンツの場所をより柔軟に指定できます。

于 2010-12-18T10:20:36.283 に答える
4

From the google-group I noticed the differences between extends (doLayout), include and a tag:

extend and include are similar, the difference is mainly in the way that you pass around variables

With extend, the parent template provides the boilerplate, and the child template provides the "body". For example the parent template could render a header and footer, and the child template could render the main content of the page. You typically set variables in the child template that are read and applied in the parent template eg #{set title:'Pet shop' /} or #{set showLoginBox:true /}

You use include when you want to do the same thing many times within a single parent template. For example #{include 'formStatusFields.html' /} The variables in the parent template are available to the included template.

If you have a piece of template code that is executed from multiple different parent templates, you should use a tag. You can pass variables to a tag. eg #{button label:'Ok', id:'ok-button'}

于 2010-12-19T09:04:02.920 に答える