Javascript 包含块的样式/脚本
好吧,我不确定我是否错过了一个技巧。但我在文档中或在线上找不到任何与解决方案相关的内容。 基本上,我试图用一个模板块包含一大块javascript,但样式也是如此。 我总结了模板结构:Javascript 包含块的样式/脚本,javascript,symfony,styles,twig,Javascript,Symfony,Styles,Twig,好吧,我不确定我是否错过了一个技巧。但我在文档中或在线上找不到任何与解决方案相关的内容。 基本上,我试图用一个模板块包含一大块javascript,但样式也是如此。 我总结了模板结构: {# base.html.twig %} <html> <head> <title>{% block title %}Base template{% endblock %}</title> {% block stylesheets %}
{# base.html.twig %}
<html>
<head>
<title>{% block title %}Base template{% endblock %}</title>
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('bundles/thisBundle/css/theme.css') }}" type="text/css" media="all" />
{% endblock %}
{% block scripts %}{% endblock %}
<head>
<body>
{% block content %}hello from base{% endblock %}
{% block javascripts %}
{% endblock %}
</body>
</html>
{# index.html.twig #}
{% extends 'base.html.twig %}
{% block content %}
{% include 'ThisBundleBundle::templatewithascript.html.twig' %}
<p>Some content here<p>
{% include 'ThisBundleBundle::anothertemplatewithascript.html.twig' %}
{% endblock %}
{#base.html.twig%}
{%block title%}基本模板{%endblock%}
{%块样式表%}
{%endblock%}
{%block scripts%}{%endblock%}
{%block content%}来自基的hello{%endblock%}
{%block javascripts%}
{%endblock%}
{#index.html.twig}
{%extends'base.html.twig%}
{%block content%}
{%include'ThisBundleBundle::templatewithascript.html.twig%}
这里有一些内容
{%include'ThisBundleBundle::anothertemplatewithascript.html.twig%}
{%endblock%}
现在我的问题来了。从这个模板中,我想添加到javascripts/stylesheets块的内容中,但我的include在内容块中,我不能用ascript.html.twig扩展template中的index.html.twig,基本上我希望这样做:
{# templatewithascript.html.twig #}
{% block stylesheets %}
<link href="{{ asset('bundles/thisBundle/css/blockspecificstyle.css') }}" rel="stylesheet" type="text/css" />
{% endblock %}
{% block body %}
<p>Click here and something happens</p>
{% endblock %}
{% block javascripts %}
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
{% endblock %}
{#templatewithascript.html.twig}
{%块样式表%}
{%endblock%}
{%block body%}
点击这里,事情发生了
{%endblock%}
{%block javascripts%}
$(文档).ready(函数(){
$(“p”)。单击(函数(){
$(this.hide();
});
});
{%endblock%}
或者这只是一个限制,我必须在包含内容的模板中包含样式/脚本?
谢谢
Lu您可以做的是将index.html.twig转换为另一个可扩展模板,从TemplateWithScript.html.twig扩展它,并从控制器中呈现它:
{# templatewithascript.html.twig #}
{% extends 'ThisBundleBundle::index.html.twig' %}
...
{# index.html.twig #}
{% extends 'base.html.twig %}
{% block content %}
{% block body %}{% endblock body %}
{% endblock content %}
好的,回答我自己的问题!
导入宏是我想到的答案,即:
{# index.html.twig #}
{% extends 'base.html.twig' %}
{% import 'ThisBundleBundle::templatewithascript.html.twig' as importedTemplate %}
{% block content %}
{{ importedTemplate.content }}
{% endblock %}
{% block javascripts %}
{{ importedTemplate.js }}
{% endblock %}
{# templatewithascript.html.twig #}
{% macro content %}
<p>Click here and something happens</p>
{% endmacro %}
{% macro js %}
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
{% endmacro %}
{#index.html.twig}
{%extends'base.html.twig%}
{%import'ThisBundleBundle::templatewithascript.html.twig'作为importedTemplate%}
{%block content%}
{{importedTemplate.content}
{%endblock%}
{%block javascripts%}
{{importedTemplate.js}
{%endblock%}
{#templatewithascript.html.twig}
{%macro content%}
点击这里,事情发生了
{%endmacro%}
{%js%}
$(文档).ready(函数(){
$(“p”)。单击(函数(){
$(this.hide();
});
});
{%endmacro%}
这似乎有效,但不确定我是否会为了达到目的而稍微改变一下规则。而且似乎还有一种更简单的方法可以让我在TemplateWithScript.html.twig中完成所有这些工作 谢谢Carlosz,但是我如何在同一页面中包含另一个带有script.html.twig的模板呢?为了本次对话,可以使用与templatewithascript.html.twig完全相同的内容