mpalourdio/twitter-widget-bundle

一个简单的扩展包,便于在twig视图中显示twitter时间轴小部件

0.2.0 2017-04-08 19:55 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:49:42 UTC


README

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight PHP 7.0+ MIT Licensed

TwitterWidgetBundle

一个Twig扩展,便于在Symfony项目中显示twitter嵌入时间轴小部件。基于此库:https://github.com/mpalourdio/TwitterWidgets

要求

PHP 7.0+ - 只支持Composer安装

安装

运行以下命令通过Composer进行安装

composer require mpalourdio/twitter-widget-bundle

new TwitterWidgetBundle\TwitterWidgetBundle() 添加到您的 AppKernel.php

使用方法

    1. 在此创建时间轴小部件:https://twitter.com/settings/widgets/new
    1. 在生成的javascript代码中,获取URL和数据-widget-id(所需的最基本信息)
    1. 最后,在twig模板中使用以下方式
{{ 
  tw({
         'dataWidgetId' : '1245687955000', => the id must be a string (quotes), because of long integer converted to float
         'href'         : 'https://twitter.com/NickName',
         'hrefText'     : 'Here type a title'
     },
     true/false
}}

以下所有选项都受支持:https://dev.twitter.com/web/embedded-timelines#options

它们在PHP中的等效数组键如下

'class'           => 'A css class, by default it will be twitter-timeline',
'href'            => 'The link to the timeline',
'hrefText'        => 'A title for your timeline to display',
'dataWidgetId'    => 'Your data widget ID : must be a string (!)',
'dataTheme'       => 'ex: dark',
'dataLinkColor'   => 'ex: #cc0000',
'width'           => 300 (integer),
'height'          => 400 (integer),
'dataChrome'      => 'noheader nofooter noborders noscrollbar transparent', => a string with options separated by a single space
'dataBorderColor' => 'border color used by the widget',
'language'        => 'The widget language detected from the page, based on the HTML lang attribute of your content. You can also set the HTML lang attribute on the embed code itself.',
'dataTweetLimit'  => 20,
'dataRelated'     => 'benward,endform',
'dataAriaPolite'  => 'polite or assertive',

您可以使用 TwitterWidgets\Options\WidgetOptions 的实例而不是数组(或任何实现 TwitterWidgets\Timeline\WidgetOptionsInterface 的类)。

$options = new TwitterWidgets\Options\WidgetOptions();
$options->setDataWidgetId('1245687955000');
$options->setHref('https://twitter.com/NickName');
$options->setHrefText('Here type a title');

{{ tw(options) }}

函数的第二个参数是一个布尔值(默认为true),它表示是否需要为小部件渲染javascript代码。如果您页面上有多个小部件,请使用 OneTimeJs 扩展只添加一次javascript代码,就在您的 </body> 之前。这将避免一些开销。请参阅 https://dev.twitter.com/web/javascript/loading

{% block javascripts %}
        <script>{{ twJs() }}</script>
{% endblock %}