symfony学习笔记20170117

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.yml
article_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 %}


 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值