TurboGears-含まれています

別のXMLドキュメント(特にHTMLドキュメント)のコンテンツは、現在のドキュメントの包含タグを使用して含めることができます。このような包含を有効にするには、HTMLドキュメントのルート要素でXInclude名前空間を宣言する必要があります。

<html xmlns = "http://www.w3.org/1999/xhtml" xmlns:xi = "http://www.w3.org/2001/XInclude >

上記の宣言は、includeディレクティブに次のものが含まれることを指定しています ‘xi’プレフィックス。現在のドキュメントに別のhtmlページのコンテンツを追加するには、次のようにxi:includeディレクティブを使用します-

<xi:include href = "somepage.html" />

次の例では、root.pyにはinclude.htmlを公開するinclude()コントローラーが含まれています。

from hello.lib.base import BaseController
from tg import expose, request

class RootController(BaseController):
   @expose('hello.templates.include')
   def include(self):
      return {}

見出しとフッターのHTML

include.htmlでは、include名前空間が宣言され、heading.htmlとfooter.htmlのコンテンツが追加されます。これがtemplates \ include.htmlのHTMLスクリプトです-

<html xmlns = "http://www.w3.org/1999/xhtml" 
   xmlns:xi = "http://www.w3.org/2001/XInclude">
	
   <head>
      <title>TurboGears Templating Example</title>
   </head>
	
   <body>
      <xi:include href = "heading.html" />
      <h2>main content </h2>
      <xi:include href = "footer.html" />
   </body>
	
</html>

これがtemplates \ heading.htmlコードです-

<html>
   <head>
      <title>TurboGears Templating Example</title>
   </head>
	
   <body>
      <h1>This is page Header</h1>
   </body>
</html>

以下はtemplates \ footer.htmlです。

<html>
   <head>
      <title>TurboGears Templating Example</title>
   </head>
	
   <body>
      <h3>This is page footer</h3>
   </body>
</html>

ギアボックスを使用して開発を開始し、 http://localhost:8080/includeブラウザで。レンダリングされる出力は次のようになります-

このようにして、ビューのモジュール構造を実現できます。xi:includeディレクティブに記載されているリソースが利用できない場合、エラーが発生します。このような場合、xi:fallbackを使用して代替リソースをロードできます。

<xi:include href = “main.html”>
   <xi:fallback href = ”default.html”/>
</xi.include>

式を含めることができるhref属性として、コンテンツの包含を動的にすることができます。

root.pyに次のコントローラーを追加します。

@expose('hello.templates.ref-include')
   def refinclude(self):
      return {'pages':['heading','main','footer']}

次のコードをref-include.htmlとしてテンプレートフォルダーに保存します。

<html xmlns = "http://www.w3.org/1999/xhtml"
   xmlns:py = "http://genshi.edgewall.org/"
   xmlns:xi = "http://www.w3.org/2001/XInclude">
	
   <head>
      <title>TurboGears Templating Example</title>
   </head>
	
   <body>
      <xi:include href = "${name}.html" py:for = "name in pages" />
   </body>
	
</html>

サーバーを起動する前に、templatesフォルダーにheading.html、main.html、およびfooter.htmlがあることを確認してください。入るhttp://localhost:8082/refinclude 次の出力を取得するには、ブラウザで