symfony学习笔记20170117

来源:互联网 发布:陕西网络广告公司 编辑:程序博客网 时间:2024/05/17 06:04

twig


{# app/Resources/views/base.html.twig #}
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>{% block title %}Test Application{% endblock %}</title>
    </head>
    <body>
        <div id="sidebar">
            {% block sidebar %}
                <ul>
                      <li><a href="/">Home</a></li>
                      <li><a href="/blog">Blog</a></li>
                </ul>
            {% endblock %}
        </div>


        <div id="content">
            {% block body %}{% endblock %}
        </div>
    </body>
</html>


{{ ... }}
“Says something”: prints a variable or the result of an expression to the template.
{% ... %}
“Does something”: a tag that controls the logic of the template; it is used to execute statements such as for-loops for example.
{# ... #}
“Comment something”: it’s the equivalent of the PHP /* comment */ syntax. It’s used to add single or multi-line comments. The content of the comments isn’t included in the rendered pages.





{# app/Resources/views/blog/index.html.twig #}
{% extends 'base.html.twig' %}


{% block title %}My cool blog posts{% endblock %}


{% block body %}
    {% for entry in blog_entries %}
        <h2>{{ entry.title }}</h2>
        <p>{{ entry.body }}</p>
    {% endfor %}
{% endblock %}




<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>My cool blog posts</title>
    </head>
    <body>
        <div id="sidebar">
            <ul>
                <li><a href="/">Home</a></li>
                <li><a href="/blog">Blog</a></li>
            </ul>
        </div>


        <div id="content">
            <h2>My first post</h2>
            <p>The body of the first post.</p>


            <h2>Another post</h2>
            <p>The body of the second post.</p>
        </div>
    </body>
</html>


二 创建链接

(1)

# app/config/routing.yml_welcome:    path:     /    defaults: { _controller: AppBundle:Welcome:index }
在twig里,使用path Twig方法来到达上面的路由
<a href="{{ path('_welcome') }}">Home</a>
(2)
# app/config/routing.ymlarticle_show:    path:     /article/{slug}    defaults: { _controller: AppBundle:Article:show }
{% for article in articles %}    <a href="{{ path('article_show', {'slug': article.slug}) }}">        {{ article.title }}    </a>{% endfor %}
三 链接静态文件
<img src="{{ asset('images/logo.png') }}" alt="Symfony!" /><link href="{{ asset('css/blog.css') }}" rel="stylesheet" type="text/css" />
(1)twig中引入css和js
{# app/Resources/views/base.html.twig #}<html>    <head>        {# ... #}        {% block stylesheets %}            <link href="{{ asset('css/main.css') }}" rel="stylesheet" />        {% endblock %}    </head>    <body>        {# ... #}        {% block javascripts %}            <script src="{{ asset('js/main.js') }}"></script>        {% endblock %}    </body></html>
三 模板全局变量
app.security 安全上下文
app.user 当前登录用户对象
app.request request对象
app.session session对象
app.environment 当前环境
app.debug 是否使用的是debug模式
<p>Username: {{ app.user.username }}</p>{% if app.debug %}    <p>Request method: {{ app.request.method }}</p>    <p>Application Environment: {{ app.environment }}</p>{% endif %}


0 0
原创粉丝点击