【问题标题】:How can I Override HWIOAuthBundle twig file如何覆盖 HWIOAuthBundle 树枝文件
【发布时间】:2015-09-24 23:46:28
【问题描述】:

我是 Symfony2.3 +FosUserBundle 的 HWIOAuthBundle 新手。 我在我的项目中使用这个捆绑包登录 facebook、twitter、googleplus。

我已经成功安装了这个并且工作正常。 但我想覆盖 login.html.twig 因为我想在我们的 twig 文件中显示 facebook、twitter、google 以及图像,但我不知道如何在 HWIOAuthBundle 中执行此操作。

我的 login.html.twig

{% block content %}
  {# Bonus: Show all available login link in HWIOAuthBundle #}
  {% render(controller('HWIOAuthBundle:Connect:connect')) %}
{% endblock %}

基础 HWIOAuthBundle login.html.twig

{% extends 'HWIOAuthBundle::layout.html.twig' %}

{% block hwi_oauth_content %}
{% if error %}
    <span>{{ error }}</span>
{% endif %}
{% for owner in hwi_oauth_resource_owners() %}
<a href="{{ hwi_oauth_login_url(owner) }}">{{ owner | trans({}, 'HWIOAuthBundle') }}</a>     <br />
{% endfor %}
{% endblock hwi_oauth_content %}

在 Html 页面中显示该类型的是哪一个:

Facebook
Google Plus
Twitter

当点击任何一个然后重定向到他的页面(Facebook、Twitter、Google Plus)时默认显示。

但我想展示这种类型的 HTML:

    <!-- socials -->
       <ul class="top-socials">
           <li><a class="facebook" href="#">Facebook</a></li>
           <li><a class="twitter" href="#">Twitter</a></li>
           <li><a class="google-plus" href="#">Google+</a></li>
       </ul>

我该怎么做?

【问题讨论】:

    标签: html facebook symfony twig hwioauthbundle


    【解决方案1】:

    要更具体地了解您的案例,您应该创建 2 个新视图:

    app/Resources/HWIOAuthBundle/views/layout.html.twig

    {# extends your own base template #}
    {% extends 'MyBundle::layout.html.twig' %}
    
    {% block title %}{{ 'Login' | trans }}{% endblock %}
    
    {% block body %}
        {% block hwi_oauth_content %}
        {% endblock hwi_oauth_content %}
    {% endblock %}
    

    app/Resources/HWIOAuthBundle/views/Connect/login.html.twig

    {% extends 'HWIOAuthBundle::layout.html.twig' %}
    
    {% block hwi_oauth_content %}
    
        {# Display oauth errors (here using Bootstrap) #}
        {% if error is defined and error %}
            <div class="row">
                <div class="col-md-12 alert alert-danger text-center">
                    <span class="error">{{ error }}</span>
                </div>
            </div>
        {% endif %}
    
        {# HWIOAuthBundle integration #}
    
        <ul class="top-social">
           <li><a class="#" href="{{ hwi_oauth_login_url('facebook') }}">Facebook</a></li>
           <li><a class="#" href="{{ hwi_oauth_login_url('twitter') }}">Twitter</a></li>
           <li><a class="#" href="{{ hwi_oauth_login_url('google') }}">Google+</a></li>
        </ul>
    
    {% endblock hwi_oauth_content %}
    

    不要试图将此登录页面放在第一个文件中,因为 OAUthBundle 使用其他几个视图(以确认配置文件等)。

    这个例子取自symfony-quickstart项目。

    【讨论】:

      【解决方案2】:

      你有两种解决方案:

      1. 使用包继承并使用父路径优化模板
      2. 在您的 app/Ressources/ 中声明一个模板,如下所示:app/Ressources/AcmeBundle/Directory/template.html.twig(其中/AcmeBundle/Directory/template.html.twig 与您的供应商中的模板路径完全相同)

      文档:

      【讨论】:

      • 谢谢,但我已经继承了 fos 用户包和模板,那么我该如何继承另一个呢?
      • 使用第二种方式 ;)
      • 谢谢,我扩展了控制器,然后覆盖了树枝文件。那么这对我来说是有效的。
      猜你喜欢
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多