0

webapp2 と jinja2 を使用して、PHP で作成した静的サイトを Google App Engine に移植しようとしています。
私の PHP の index.php は次のようになります。

<body>
<div id="container">
    <header id="header"><?php require_once DIR_HTML."header.phtml" ?></header>
    <section id="main"><?php require_once DIR_HTML.$_GET['page'].".phtml" ?></section>
    <footer id="footer"><?php require_once DIR_HTML."footer.phtml" ?></footer>
</div>
</body>

そして、私のgoogle-app-engineコードには次のものがあります:

import webapp2, jinja2, os

jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):
        template_values = {
        }

        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))

class AboutPage(webapp2.RequestHandler):
    def get(self):
        template_values = {
        }

        template = jinja_environment.get_template('templates/about.html')
        self.response.out.write(template.render(template_values))        

app = webapp2.WSGIApplication([('/', MainPage), ('/about', AboutPage)], debug=True)

Python で同じ種類の機能を複製するにはどうすればよいですか?

4

1 に答える 1

2

テンプレートの継承

Jinja の最も強力な部分は、テンプレートの継承です。テンプレートの継承により、サイトのすべての共通要素を含み、子テンプレートがオーバーライドできるブロックを定義する基本の「スケルトン」テンプレートを構築できます。

<!DOCTYPE html>
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    {% block head %}
        <link rel="stylesheet" href="style.css" />
        <title>{% block title %}{% endblock %} - My Webpage</title>
    {% endblock %}
</head>
<body>
    <div id="content">{% block content %}{% endblock %}</div>
    <div id="footer">
        {% block footer %}
            &copy; Copyright 2008 by <a href="http://domain.invalid/">you</a>.
        {% endblock %}
    </div>
</body>
于 2013-02-07T15:19:38.953 に答える