Beispiel #1
0
def test_attr_has_attr():
    app = Flask(__name__)
    init_htmlbuilder(app)

    @app.route('/a')
    def view_a():
        g.attrs['description'] = 'A description'
        return render_template()

    @root_block()
    def site_root():
        return [
            html.doctype('html'),
            html.html(
                html.head(
                    html.has_attr('description')(
                        html.meta(content=Attr('description'))
                    ),
                    html.has_attr('author')(
                        html.meta(content=Attr('author'))
                    )
                ),
                html.body()
            )
        ]

    client = app.test_client()
    result = client.get('/a')

    assert result.data.decode('utf-8') == """<!doctype html>
Beispiel #2
0
def test_deep_inheritance():
    app = Flask(__name__)
    init_htmlbuilder(app)

    @app.route('/a')
    def view_a():
        return render_template()

    @root_block()
    def site_root():
        return [
            html.doctype('html'),
            html.html(
                html.head(),
                html.body(
                    g.blocks['body']
                )
            )
        ]

    @block('body', site_root)
    def site_body():
        return html.div(class_='container')(g.blocks['container'])

    @block('container', site_body)
    def container_body():
        return html.div('Container content.')

    client = app.test_client()
    result = client.get('/a')

    assert result.data.decode('utf-8') == """<!doctype html>
Beispiel #3
0
def test_alternative_definition():
    app = Flask(__name__)
    init_htmlbuilder(app)

    @app.route('/a')
    def view_a():
        return render_template()

    @app.route('/b')
    def view_b():
        return render_template()

    def site_root():
        return [
            html.doctype('html'),
            html.html(
                html.head(),
                html.body(
                    g.blocks['body']
                )
            )
        ]

    def default_body():
        return html.p('Default body.')

    def view_a_body():
        return html.div(class_='container')(g.blocks['container'])

    def view_a_container():
        return html.div('Container content.')

    RootBlock(site_root)(
        Context('body')(
            Block(default_body),
            Block(view_a_body, view_a)(
                Context('container')(
                    Block(view_a_container)
                )
            )
        )
    )

    client = app.test_client()

    result = client.get('/a')
    assert result.data.decode('utf-8') == """<!doctype html>
<html>
  <head></head>
  <body>
    <div class="container">
      <div>Container content.</div>
    </div>
  </body>
</html>
"""

    result = client.get('/b')
    assert result.data.decode('utf-8') == """<!doctype html>
Beispiel #4
0
def test_root_block_decorator():
    app = Flask(__name__)
    init_htmlbuilder(app)

    @app.route('/')
    def return_root():
        g.blocks['body'] = 'Hello, World!'
        return render_template()

    @root_block()
    def site_root():
        return [html.doctype('html'), html.html(html.head(), html.body(g.blocks['body']))]

    client = app.test_client()
    result = client.get('/')
    assert result.mimetype == 'text/html'
    assert result.data.decode('utf-8') == """<!doctype html>
Beispiel #5
0
def test_default_block():
    app = Flask(__name__)
    init_htmlbuilder(app)

    @app.route('/a')
    def view_a():
        return render_template()

    @app.route('/b')
    def view_b():
        return render_template()

    @root_block()
    def site_root():
        return [html.doctype('html'), html.html(html.head(), html.body(g.blocks['body']))]

    @block('body', site_root, view_a)
    def view_a_body():
        return html.p('Hello, View A!')

    @block('body', site_root)
    def default_body():
        return html.p('Hello, Default Block!')

    client = app.test_client()

    result = client.get('/a')
    assert result.data == """<!doctype html>
<html>
  <head></head>
  <body>
    <p>Hello, View A!</p>
  </body>
</html>
"""
    result = client.get('/b')
    assert result.data == """<!doctype html>
Beispiel #6
0
def test_default_block():
    app = Flask(__name__)
    init_htmlbuilder(app)

    @app.route('/a')
    def view_a():
        return render_template()

    @app.route('/b')
    def view_b():
        html.block('title')('New Title')
        return render_template()

    @root_block()
    def site_root():
        return html.title(
            html.block('title')(
                'Default Title'
            )
        )

    client = app.test_client()
    result = client.get('/a')
    assert result.data.decode('utf-8') == """<title>
  Default Title
</title>
"""

    result = client.get('/b')
    assert result.data.decode('utf-8') == """<title>
  New Title
</title>
"""

    result = client.get('/a')
    assert result.data.decode('utf-8') == """<title>
Beispiel #7
0
def test_multiple_contexts():
    app = Flask(__name__)
    init_htmlbuilder(app)

    @app.route('/a')
    def view_a():
        return render_template()

    @root_block()
    def site_root():
        return [
            html.doctype('html'),
            html.html(
                html.head(
                    html.title(
                        g.blocks['title']
                    )
                ),
                html.body(
                    g.blocks['body']
                )
            )
        ]

    @block('body', site_root)
    def site_body():
        return html.p('This is the body.')

    @block('title', site_root)
    def site_title():
        return 'Title'

    client = app.test_client()
    result = client.get('/a')

    assert result.data.decode('utf-8') == """<!doctype html>