php - Twig 模板不能包含 php 模板

标签 php templates symfony twig

我遇到了一个问题,这个问题对我来说不是很清楚也很难理解。我试图制作日历小部件,它应该显示在我网站的每个页面上。所以,我认为它应该是普通模板(没有参数,没有每个站点)。我试着把它作为 Twig 模板来做。我设法渲染了日历,但是我在获取日期对象时遇到了问题(这是获取渲染日历的正确参数所必需的)。过了一会儿,我尝试制作一个 php 模板,它将包含在主 Twig 模板 (layout.html.twig) 中。它没有成功。我在 config.yml 中启用了 php 引擎,但没有帮助 - 实际上包含了 php 模板,但作为普通文件,而不是 php 文件(未解析为 php 脚本),(但 php 引擎正在运行,我尝试渲染来自它工作的 Controller 的 php 模板)。我也读过可以在模板中包含来自另一个 Controller 的结果,但对我来说这不是正确的解决方案,我还没有尝试过(php 模板应该适合这个问题)。

我应该怎么做才能解决这个问题?

主config.yml

imports:
    - { resource: parameters.ini }
    - { resource: security.yml }

framework:
    #esi:             ~
    translator:      { fallback: %locale% }
    secret:          %secret%
    charset:         UTF8
    router:          { resource: "%kernel.root_dir%/config/routing.yml" }
    form:            true
    csrf_protection: true
    validation:      { enable_annotations: true }
    templating:      { engines: ['twig', 'php'] }
    session:
        default_locale: %locale%
        auto_start:     true

# Twig Configuration
twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    # java: /usr/bin/java
    filters:
        cssrewrite: ~
        # closure:
        #     jar: %kernel.root_dir%/java/compiler.jar
        # yui_css:
        #     jar: %kernel.root_dir%/java/yuicompressor-2.4.2.jar

# Doctrine Configuration
doctrine:
    dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true

# Swiftmailer Configuration
#swiftmailer:
#    transport: %mailer_transport%
#    host:      %mailer_host%
#    username:  %mailer_user%
#    password:  %mailer_password%

jms_security_extra:
    secure_controllers:  true
    secure_all_services: false

# services:
#    TpsaMailer:

#        class: Tpsa\TestBundle\Controller\MailerController

layout.html.twig

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

{% block stylesheets %}
<link rel="stylesheet" type="text/css"
href="{{ asset('bundles/tpsablog/css/main.css') }}">
{% endblock %}

{% block javascripts %}
<!-- empty javascripts -->
{% endblock %}

<title>
{% block title %}
{% trans %}blog.programisty.duga{% endtrans %}
{% endblock %}
</title>

</head>
<body>
<div id="all">
    <div id="top">
        {% block top %}
            <div style="float: left" class="right">
                            <img style="float: left;
                            vertical-align: middle; margin: 8px
                            8px 8px 0px" src="{{ asset('bundles/tpsablog/images/glider.png') }}">
                <h4>{% trans %}blog.programisty.duga{% endtrans %}</h4>
                <div style="font-size: 8px">
                    {% trans %}ciekawosc.wiedza.niewygodne{% endtrans %}
                    <!-- Ciekawość i wiedza... To, co jest niewygodne dla
                    władzy -->
                </div>

            </div>
            <div style="float: right">
                <a href="{{ path('HomePage') }}">{% trans %}strona.glowna{% endtrans %}</a>
                <a href="{{ path('AboutPage') }}">{% trans %}o.mnie{% endtrans %}</a>
                <a href="{{ path('TBB_mess_add') }}">{% trans %}napisz.do.mnie{% endtrans %}</a>
                {% if is_granted('IS_AUTHENTICATED_FULLY') %}
                {{ app.user.username }}
                <a href="{{ path('TBB_tag_list_homepage') }}">{% trans %}tagi{% endtrans %}</a>
                <a href="{{ path('TBB_mess_list_homepage') }}">{% trans %}wiadomosci{% endtrans %}</a>
                <a href="{{ path('logout') }}">{% trans %}wyloguj{% endtrans %}</a>
                {% else %}
                <a href="{{ path('login') }}">{% trans %}zaloguj{% endtrans %}</a>
                {% endif %}
                <div style="text-align: center; margin: 10px 0px">
                <a href="{{ path('TBB_rss') }}">
                <img src="{{ asset('bundles/tpsablog/images/rss.png')
                }}" alt="rss channel">  
                </a>
                <a href="http://www.facebook.com/duga.chernobyl"
                target="_blank">
                <img src="{{ asset('bundles/tpsablog/images/facebook.png') }}"
                alt="facebook"> 
                </a>
                <a href="http://www.youtube.com/user/DugaEye"
                target="_blank">
                <img src="{{ asset('bundles/tpsablog/images/youtube.png') 
                }}" alt="youtube">
                </a>
                </div>
            </div>
            <div style="clear:both"></div>
        {% endblock %}
    </div>

    <div id="frame">
        <div id="left">
        {% block content %}
            {% trans %}TODO{% endtrans %}
        {% endblock %}
        </div>
        <div id="right">
        {% block panel %}
            <div style="text-align: left">
                <div style="text-align: center">    
                <h4>{% trans %}profil.duga.eye{% endtrans %}</h4>
                <img style="width: 100px" src="{{
                asset('bundles/tpsablog/images/photo.jpg')
                }}">
                </div>
                <div style="font-weight:900; margin-top: 10px">
                <ul>
                <li>{% trans %}wiek{% endtrans %}: 21</li>
                <li>{% trans %}miejsce{% endtrans %}: /dev/null</li>
                <li>{% trans %}zainteresowania{% endtrans %}: {% trans %}programowanie.hacking.filozofia{% endtrans %}</li>
                <li>{% trans %}email{% endtrans %}: <a
                href="mailto:duga(dot)eye(at)gmx(dot)com">Mail</a>
                </ul>
                </div>
            </div>
            <h3>{% trans %}reklamy{% endtrans %}</h3>
            {% include '::calendar.html.php' %}
        {% endblock %}
        </div>
    </div>

    <div id="footer">
        {% block footer %}
        {% trans %}footer{% endtrans %}
        {% endblock %}
    </div>


</div>
</body>
</html>

如何从 php Date 对象中获取正确的参数 offset、number、koniec、aktualny? (现在它是硬编码的)

日历.html.twig

{% include '::calendar.html.php' %}

{% set offset = 1 %}
{% set number = 28 %}
{% set koniec =  7 - ((offset + number) % 7) %}
{% set aktualny = 13 %}
<table border="0" style="text-align: center">
<thead>
<tr>
<td>{% trans %}pn{% endtrans %}</td>
<td>{% trans %}wt{% endtrans %}</td>
<td>{% trans %}sr{% endtrans %}</td>
<td>{% trans %}czw{% endtrans %}</td>
<td>{% trans %}pt{% endtrans %}</td>
<td>{% trans %}sob{% endtrans %}</td>
<td>{% trans %}nie{% endtrans %}</td>
</tr>
<tbody>
{% if offset % 7 != 0 %}
<tr>
{% for i in range(0,offset-1,1) %}<td><br></td>{% endfor %}
{% endif %}
{% for i in 1..number %}
{% if (i+offset)%7 == 1 %}<tr>{% endif %}
<td>
{% if i == aktualny %}
<span style="color: red">{{ i }}</span>
{% else %}
{{ i }}

{% endif %} 

</td>
{% if (i+offset)%7 == 0  %}</tr>{% endif %}
{% endfor %}
{% if koniec  < 7 %}
{% for i in 1..koniec %}
<td><br></td>
{% endfor %}
</tr>
{% endif %}
</tbody>
</table>

php 模板应该通过将其包含在 twig 模板中作为 php 模板执行,但它不会作为一个模板被解析和执行。

日历.html.php

ppp<?php echo  ('ala') ?>ooo

只是在源代码中包含'pppooo',不可见被处理为 html 标签。

deps文件如果需要的话

[symfony]
    git=http://github.com/symfony/symfony.git
    version=v2.0.9

[twig]
    git=http://github.com/fabpot/Twig.git
    version=v1.5.1

[monolog]
    git=http://github.com/Seldaek/monolog.git
    version=1.0.2

[doctrine-common]
    git=http://github.com/doctrine/common.git
    version=2.1.4

[doctrine-dbal]
    git=http://github.com/doctrine/dbal.git
    version=2.1.5

[doctrine]
    git=http://github.com/doctrine/doctrine2.git
    version=2.1.5

[swiftmailer]
    git=http://github.com/swiftmailer/swiftmailer.git
    version=v4.1.5

[assetic]
    git=http://github.com/kriswallsmith/assetic.git
    version=v1.0.2

[twig-extensions]
    git=http://github.com/fabpot/Twig-extensions.git

[metadata]
    git=http://github.com/schmittjoh/metadata.git
    version=1.0.0

[SensioFrameworkExtraBundle]
    git=http://github.com/sensio/SensioFrameworkExtraBundle.git
    target=/bundles/Sensio/Bundle/FrameworkExtraBundle
    version=origin/2.0

[JMSSecurityExtraBundle]
    git=http://github.com/schmittjoh/JMSSecurityExtraBundle.git
    target=/bundles/JMS/SecurityExtraBundle
    version=origin/1.0.x

[SensioDistributionBundle]
    git=http://github.com/sensio/SensioDistributionBundle.git
    target=/bundles/Sensio/Bundle/DistributionBundle
    version=origin/2.0

[SensioGeneratorBundle]
    git=http://github.com/sensio/SensioGeneratorBundle.git
    target=/bundles/Sensio/Bundle/GeneratorBundle
    version=origin/2.0

[AsseticBundle]
    git=http://github.com/symfony/AsseticBundle.git
    target=/bundles/Symfony/Bundle/AsseticBundle
    version=v1.0.1

最佳答案

您不能在单个响应中混合搭配 twig 和 php(这样做会绕过 twig 的部分要点,这是为了防止设计人员在 View 中创建过多的逻辑)。

我认为 Symfony 文档应该/可以对此更清楚(目前它基本上说“启用它们并做你喜欢的事”)。

如果您嵌入另一个 Controller ,那么您应该能够提供不同的响应,并且那个响应可以是基于 php 的。

关于php - Twig 模板不能包含 php 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14264466/

相关文章:

php - 视频不流mysql php

c++ - 将 MPL vector 转换为静态数组

以派生类为参数的 C++ 基类构造函数(?)

客户端未知的 MySQL 8.0 请求的身份验证方法(caching_sha2_password)

php - 我的 Symfony2 应用程序出现语法错误或访问冲突

php - 更改的 CSS 未显示在已安装的论坛包上

php - 页面在 Wamp 上显示但在真实服务器上出错

php - 如何获取 Laravel 删除/更新/插入语句的原始 SQL?

具有灵活返回类型的 C++ 函数模板

forms - 如何为选择标签元素设置类?