我遇到了一个问题,对我来说这不是很清楚也很难理解。我试图制作日历小部件,它应该显示在我网站的每个页面上。所以,我认为它应该是平均模板(没有参数,没有每个站点)。我试着把它作为树枝模板。我设法渲染了日历,但是在获取日期对象时遇到了问题(这是获取渲染日历的正确参数所必需的)。过了一会儿,我尝试制作一个 php 模板,它将包含在主 twig 模板(layout.html.twig)中。它没有成功。我在 config.yml 中启用了 php 引擎,但没有帮助 - 实际上包含 php 模板,但作为普通文件,而不是 php 文件(不解析为 php 脚本),(但 php 引擎正在工作,我试图渲染来自控制器的 php 模板,它可以工作)。
我应该怎么做才能解决这个问题?
主配置.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
布局.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 对象中获取正确的参数偏移量、数字、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