Symfony 和 Twig 模板.如何动态扩展模板或不扩展任何内容? [英] Symfony and Twig templates. How to dynamically extend a template or extend nothing?

查看:29
本文介绍了Symfony 和 Twig 模板.如何动态扩展模板或不扩展任何内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我描述一个情况.您有一个配置文件表单和一个表单,用户可以在其中上传与个人配置文件相关的文档.由于配置文件表单已经很长,因此文档已移至新表单.

一切都很好.现在我们要使用引导选项卡来做 Profile |用户友好性文档.

现在我知道了,因为如果您提交文件,我们将使用两个单独的表单,则配置文件上的更改将不会保存,反之亦然.

我已经使用

在选项卡中添加了文档表单

{{ render(controller('ManyAppBundle:Document:createDocument', {'viewOnly': true})) }}

'viewOnly': true 是查询参数,操作不需要.

我现在的问题是,如果配置文件选项卡呈现文档模板,它必须只显示上传小部件和提交,而当您直接转到文档页面时,它必须显示标题和侧栏以及所有内容.

所以我尝试了

{% 如果不是 viewOnly %}{% 扩展... %}{% 万一 %}

这带来了问题,因为您不能在 if(Causes a ("Node "1" does not exist for Node "Twig_Node".") 错误中使用扩展).

其他类似问题提供了此解决方案

{% extends viewOnly == false ?...%}

当 viewOnly 为 false 时,它​​必须扩展所有其他模板使用的基本模板,但如果为 true,我只想显示:

{{ form_start(form, { 'style': 'horizo​​ntal', 'col_size': 'sm' }) }}{% 如果定义了 form.documents %}{{ form_row(form.documents) }}{% 万一 %}{{ form_row(form.submit, { 'attr': { 'class': 'btn btn-success' } }) }}{{ form_end(form) }}

但现在顶部

{% extends viewOnly == false ?...%}

如果 viewOnly 变为 true 则失败,模板 "" can't be find.

有没有办法说扩展此特定模板与不扩展任何模板的结果相同?

或者,当 viewOnly 为 false 或什么都不做时,有没有一种方法可以扩展它?

解决方案

基本上有 3 种解决方案来确定如何在一个 twig 模板中扩展不同类型的模板.

第一个将是一个非常简单的基于真或假的逻辑.您可以在 twig 文件中轻松完成此操作,如下例所示

{% extends entity.isExpired == false ?active.html.twig : expired.html.twig %}

第二个将是@Jeet 建议在控制器中执行此操作.这也可以像布尔检查一样简单,但像以下示例中的附加实体检查一样高级

/* 用户控制器 */公共函数 showAction($id){$user = $this->get('myapp.repository.user')->findBy($id);$isUser = $user instanceof User;如果 (!$isUser) {返回 $this->redirectToBloggersPage();}/* $params 是你通常会返回到模板的内容 */$params = 数组 ('用户' =>$用户,);如果 ($user->hasPostedBlog()) {$params['blog'] = $this->get('myapp.repository.blog')->findByUserLatest($user);return $this->render('MyAppBundle:User:user_and_blog_view.html.twig', $params);}别的 {return $this->render('MyAppBundle:User:user_view.html.twig', $params);}}

第三个将是@Thomas 建议在单独的树枝函数中执行此操作.这将在 Twig 扩展中完成,您可以在其中注入服务,如下例所示

/* 树枝扩展 */公共函数extendBookForm(){如果 ($this->getSecurityContext()->isGranted('IS_AUTHENTICATED_FULLY')) {返回user_view.html.twig";}返回public_view.html.twig";}/* 树枝文件 */{% 扩展extend_book_from() %}

我最初的问题没有解决我想要的结果.我认为我希望的方式是不可能的,如果您想动态地检查要扩展的内容,您必须始终扩展多个模板,因为您必须为每个结果扩展一些内容.

这些是您可以进行动态模板扩展的不同方式.

Let me describe a situation. You have a profile form and a form where users can upload personal profile related documents. Since the profile form is already very long the documents moved to a new form.

Everything works great. Now we want to use the bootstrap tabs to do Profile | Documents for user friendliness.

Now I know because we are using two separate forms if you submit the documents the changes on the profile won't save and vice versa.

I have added the document form in the tab using

<div role="tabpanel" class="tab-pane" id="documents">
    {{ render(controller('ManyAppBundle:Document:createDocument', {'viewOnly': true})) }}
</div>

The 'viewOnly': true is a query parameter and is not required by the action.

My question now becomes if the profile tab renders the document template it must only show the upload widget and the submit where as when you go directly to the document page it must show the title and side bar and everything.

So I tried

{% if not viewOnly %}
    {% extends ... %}
{% endif %}

That gave problems because you can't use extends within a if(Causes a ("Node "1" does not exist for Node "Twig_Node".") error).

Other similar question provided this solution

{% extends viewOnly == false ? ... %}

When viewOnly is false it must extend the base template used by all other templates but if it is true I only want to show this:

{{ form_start(form, { 'style': 'horizontal', 'col_size': 'sm' }) }}
    {% if form.documents is defined %}
        {{ form_row(form.documents) }}
    {% endif %}

    {{ form_row(form.submit, { 'attr': { 'class': 'btn btn-success' } }) }}
{{ form_end(form) }}

But now with the top

{% extends viewOnly == false ? ... %}

if viewOnly becomes true it fails with Template "" can't be find.

Is there a way to say extends this specific template that will be the same result of not extending any template?

Or alternatively is there a way of saying extend this when viewOnly is false or just do nothing?

解决方案

There are basically 3 solutions to determine how to switch between extending different types of templates within a twig template.

The first will be a very simple true or false logic based. You can do this within the twig file easily as in the following example

{% extends entity.isExpired == false ? active.html.twig : expired.html.twig %}

The second will be as @Jeet suggested doing this in a controller. This can also be as simple as a boolean check but as advanced as additional entity checks as in the following example

/* User Controller */
public function showAction($id)
{
    $user = $this->get('myapp.repository.user')->findBy($id);
    $isUser = $user instanceof User;

    if (!$isUser) {
        return $this->redirectToBloggersPage();
    }

    /* $params is what you will normally return to the template */
    $params = array (
        'user' => $user,
    );

    if ($user->hasPostedBlog()) {
        $params['blog'] = $this->get('myapp.repository.blog')->findByUserLatest($user);

        return $this->render('MyAppBundle:User:user_and_blog_view.html.twig', $params);
    }
    else {
        return $this->render('MyAppBundle:User:user_view.html.twig', $params);
    }
}

The third will be as @Thomas suggested doing this in a separate twig function. This will be done in a Twig extension where you can inject services as in the following example

/* Twig extension */
public function extendBookForm()
{
    if ($this->getSecurityContext()->isGranted('IS_AUTHENTICATED_FULLY')) {
        return "user_view.html.twig";
    }

    return "public_view.html.twig";
}

/* Twig file */
{% extends extend_book_from() %}

My original question does not have a solution for the outcome I wanted. I am taking it that the way I hoped for is not possible and if you dynamically want to check what to extend you must always extend more than one template since you will have to extend something for each outcome.

These are the different ways you will be able to do dynamic template extensions.

这篇关于Symfony 和 Twig 模板.如何动态扩展模板或不扩展任何内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆