Inheritance 嵌套块与木材的多重继承
我一直在努力跟随,但它并没有按照(我认为的)它应该的方式工作。我想替换“headline”块中的内容或添加到其中,但我只得到single.twig中建立的标题 base.twig:Inheritance 嵌套块与木材的多重继承,inheritance,twig,block,multiple-inheritance,timber,Inheritance,Twig,Block,Multiple Inheritance,Timber,我一直在努力跟随,但它并没有按照(我认为的)它应该的方式工作。我想替换“headline”块中的内容或添加到其中,但我只得到single.twig中建立的标题 base.twig: <!-- Stuff --> {% block content %} Sorry, no content {% endblock %} <!-- More Stuff --> {%block content%} 对不起,没有内容 {%endblock%}
<!-- Stuff -->
{% block content %}
Sorry, no content
{% endblock %}
<!-- More Stuff -->
{%block content%}
对不起,没有内容
{%endblock%}
单枝:
{% extends "base.twig" %}
{% block content %}
<!-- Stuff-->
{% block headline %}
<h1 class="article-h1">
<a href="{{post.link}}">{{ post.title }}</a>
</h1>
{% endblock headline %}
<!-- More Stuff-->
{% endblock %}
{%extends“base.twig”%}
{%block content%}
{%block headline%}
{%endblock headline%}
{%endblock%}
single-post-mypost.twig:
{% extends "single.twig" %}
{% block headline %}
{{parent()}}
<h1>Custom Header</h1>
{% endblock headline %}
{%extends“single.twig”%}
{%block headline%}
{{parent()}}
自定义标题
{%endblock headline%}
我找到了它不起作用的原因
在single.php中,Timber::render查找的是$post->ID和$post->post\u类型,而不是$post->post\u名称
现在可以了
我一直在胡闹,我不记得它是否在原始Timber的入门主题中,但这里是代码,以防其他人遇到同样的问题
$context = Timber::get_context();
$post = Timber::query_post();
$context['post'] = $post;
$context['sidebar'] = Timber::get_sidebar('sidebar.php');
if ( post_password_required( $post->ID ) ) {
Timber::render( 'single-password.twig', $context );
} else {
Timber::render( array( 'single-' . $post->post_name . '.twig', 'single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single.twig' ), $context );
}
你清除缓存了吗?此代码为是,当devtools在浏览器中打开时,我禁用了缓存。