2016-11-28 3 views
0

私は自分のプロジェクトにweb.py Templatorを使い、render( 'templates'、base = "")を使っています。ベースレイアウトとページレイアウト(単純化)を組み合わせます。web.pyとセクションブロックの使い方

view.py 
render = web.template.render('templates',base='layout') 
return render.index() 

共有レイアウトファイル

layout.html 
$def with (content) 
<html> 
<head> 
<title>$content.title</title> 
</head> 
<body> 
$:content 
</body> 
</html> 

ページ特定のテンプレート

index.html 
$def with (values) 
$var title: Hello Kitty 
<p>Hello $values, how are you doin?</p> 

私が探しているソリューションは、私の質問は以下の

login.html 
$def with (values) 
$var title: Enter credentials 

<form> 
<p><input type="text" name="user_name"></p> 
<p><input type="password" name="user_pwd"></p> 
<p><button type="submit">Open the gates</button></p> 
</form> 

$block_begin 
<script> 
// When the form is submitted, check the required fields and inform user 
// if any data is missing or looks weird 
</script> 
$block_end 

</body> 
</html> 

実現する方法であります、どのように私はtを追加するのですか?彼はlogin.htmlテンプレートにスクリプトを作成しますが、index.htmlテンプレートにはスクリプトを作成しませんか?それはこの

layout.html 
$def with (content) 
<html> 
<head> 
<title>$content.title</title> 
</head> 
<body> 
$:content 

$block_begin 
$block_end 
</body> 
</html> 

$のようなlayout.htmlの下部に表示されるように、私はすべてのページに、すべてのJSのロジックを追加することに興味がないんだけど、私はこの$ block_begin/$ BLOCK_ENDを追加したいと思いますblock_begin/$ block_endは自分自身をよりよく説明するために思いついたものでした。

答えて

0

template -> defwith sectionsは例ではなく、文法です。テンプレートを使用するには、例としてhttp://webpy.org/docs/0.3/templetorをチェックしてください。ハイレベルで

、あなたは

=== templates/index.html === 
$def with(values) 
$var title: Hello Kitty 
<p>Hello $values, how are you doin?</p> 

=== templates/layout.html === 
$def with(content) 
<html> 
<head> 
    <title>$content.title</title> 
</head> 
<body> 
    $:content 
</body> 
</html> 

に類似したテンプレートは、その後、あなたのpythonでテンプレートで指定されたパラメータを渡し、テンプレートをレンダリング作成します。名前付きテンプレート(index.html)は、ベース(layout.html)を使用してレンダリングされ、発見したように、レンダリングされた内部ビット(インデックスの結果)が含まれ、基本テンプレートに挿入されます、 レイアウト。

index.html &ではなく、login.htmlにいくつかのスクリプトを手に入れる方法を尋ねています。単にjavascriptコードをlogin.htmlテンプレートに追加するだけです。

=== login.html === 
$def with (values) 
$var title: Enter credentials 
<form> 
... 
</form> 
<script> 
    window.addEventListener('load', function() { 
    // whatever javascript you want to execute on load. 
    // If using jQuery, you'll have to use $$('form') or jQuery('form') rather 
    // than $('form'), as dollar signs are special within the template. 
    }); 
</script> 

もっと賢いことがありますか? contentをもっと詳しく使用してください。テンプレートに$varを使用して定義したものは、基本レイアウトの$contentに配置されます。

login.htmlページがレンダリングされたときにのみlogin.jsを含める場合は、新しいコンテンツ属性を簡単に作成できます。 login.html:

$var extra_js: js/login.js 

次に、レイアウトファイルの下部に値を条件付きでロードします(ここではスクリプトをロードします)。

あなたは、メタデータをパラメータ、あなたのlayout.htmlはますます強力にするスクリプト、CSSファイルなどあなたが$content.titleでやったと同じように、そしてあなたの個別のテンプレートファイルは、全体のレイアウトの異なる部分を駆動させることができます
=== templates/layout.html === 
... 
<body> 
    $:content 
    $if content.get('extra_js', None): 
     <script type="text/javascript" src="$content.extra_js"></script> 
</body> 
</html> 

+0

私は何の間にImです。しかし、私が望むことは、レイアウトファイル内にブロックを定義してから、ビューローダーに応じてコンテンツを追加することです。DjangoのテンプレートやJinjaで可能なように、切り替えの代わりにweb.pyのテンプレートを利用したいと思います。だから問題は、その機能がサポートされていることです –

+0

私はそれが可能であると確信していますが、私はあなたがしていないことをしようとしていることは理解できません。 web.py Templatorは '{%%}'ブロックを必要とせず、必要としません。テンプレートに直接javascriptを追加することができます。 ( ''または ''のいずれか)。あなたは何をしようとしているのかについてもっと詳しく説明できますか? – pbuck

+0

たとえば、共通のルック・アンド・フィールは、レンダリングで 'base ='を使うことによって行われます。あなたはテンプレート内で直接 'render'を呼び出すことによってテンプレートを呼び出す/ロードすることができます(それは結局はPythonです)...あなたが探しているものかもしれません。 – pbuck

関連する問題