rezozero / social-links
提供社交网络网址用于分享。
v2.0.0
2023-09-26 13:46 UTC
Requires
- php: >=7.4
- twig/twig: 2.* || 3.*
Requires (Dev)
- phpstan/phpstan: ^1.10
This package is auto-updated.
Last update: 2024-09-26 15:46:18 UTC
README
用法
使用 Composer 安装 SocialLinks
composer require rezozero/social-links
使用你的数据源和一些输出设置配置你的 SocialLinks
实例。 在 HTML 页面中加载 fontawesome CSS(基础、常规和品牌最小化)文件。
// Construct a new SocialLinks $share = new \RZ\SocialLinks\SocialLinks(array( 'url' => 'http://www.rezo-zero.com', 'title' => 'REZO ZERO website homepage', // Optional image source url for pinterest. must be at least 200px by 200px, if you intent to use for facebook 'imageUrl' => 'http://www.rezo-zero.com/templates/rezo-zero/img/apple-icon.png', // Optional status for overriding title for twitter, whatsapp and emails body 'status' => 'Hey! Look at this awesome website.' )); // Set link class prefix $share->setClassPrefix('social-link'); // Set social icons class prefix // Use fa for Font Awesome or an // other for a custom icon set. $share->setIconPrefix('fa'); // Optional: // Set link additional classes, for example // to add "btn" bootstrap classes. $share->setLinkClasses('btn btn-default');
单个网址
echo $share->getUrl('twitter'); // https://twitter.com/intent/tweet?text=Hey%21%20Look%20at%20this%20awesome%20website.%20%E2%80%94%20http%3A%2F%2Fwww.rezo-zero.com
带有图标的单个链接
$share->setClassPrefix('social-link'); $share->setIconPrefix('fa'); echo $share->getLink('facebook', $share->getIcon('facebook')); // <a class="social-link social-link-facebook" target="_blank" rel="nofollow" href="https://#/sharer/sharer.php?u=http%3A%2F%2Fwww.rezo-zero.com"><i class="social-link-icon fa fa-brands fa-square-facebook"></i><span class="social-link-name">Facebook</span></a>
带有 SVG 标签的单个链接
$share->setClassPrefix('social-link'); $share->setIconPrefix('fa'); echo $share->getLink('facebook', $share->getUseSVG('facebook')); // <a class="social-link social-link-facebook" target="_blank" rel="nofollow" href="https://#/sharer/sharer.php?u=http%3A%2F%2Fwww.rezo-zero.com"><svg class="social-link-icon fa fa-brands fa-square-facebook"><use xlink:href="#fa-facebook"></use></svg><span class="social-link-name">Facebook</span></a>
带图标的一组链接
无图标
echo $share->getLinks(array('facebook', 'twitter', 'linked-in'));
带有 <i>
图标
echo $share->getLinksWithIcon(array('facebook', 'twitter', 'linked-in'));
带有 <svg>
图标
echo $share->getLinksWithSVG(array('facebook', 'twitter', 'linked-in'));
你也可以选择一个非空的分隔符,例如破折号
echo $share->getLinksWithIcon(array('facebook', 'twitter', 'linked-in'), ' - ');
可用网络
- delicious
- digg
- evernote
- facebook(默认为 sharer,或如果你提供了
facebookAppId
,则为 /dialog/feed),如果你使用默认的 font-awesome 图标前缀,图标类将是facebook-official
。对于 SVG 图标或 非 font-awesome 前缀,它仍然是facebook
。 - friendfeed
- google-plus
- linked-in
- newsvine
- scoop-it
- slashdot
- stumbleupon
- tumblr
Twig 扩展
$twig->addExtension(new \RZ\SocialLinks\Twig\SocialLinksExtension());
SocialLinksExtension
Twig 扩展引入了 3 个新过滤器,以便能够在不使用任何 PHP 代码的情况下生成你的社交链接。
social_links
icon_social_links
svg_social_links
tweet_links
首先,你需要在一个关联数组中收集你的社交数据,或者简单地 set
一个字符串变量(它将被用作 URL)。
{% set social_data = { 'url': 'http://www.rezo-zero.com', 'title': 'REZO ZERO website homepage', } %} {# or #} {% set social_data = 'http://www.rezo-zero.com' %}
然后,你可以使用 3 个 SocialLinks 过滤器之一,带有或不带可选参数。所选网络可以设置为一个数组或一个简单的字符串。
<nav class="social-links"> {{ social_data|social_links(['facebook', 'twitter']) }} </nav> <nav class="social-links"> {{ social_data|social_links('twitter') }} </nav> <nav class="social-links"> {{ social_data|icon_social_links( ['facebook', 'twitter'], 'icon-prefix', 'class-prefix', 'link-classes', 'Share on %s' ) }} </nav>
奖励: tweet_links
是一个用于解析你的推文内容的 Twig 过滤器。
翻译分享操作标签
我们引入了 shareActionLabel
参数,以提供社交链接的 title
以提高可访问性。你可以在自己的 Twig 模板中覆盖它以更改 title
并进行翻译。
<nav class="social-links"> {{ social_data|icon_social_links( ['facebook', 'twitter'], 'icon-prefix', 'class-prefix', 'link-classes', ('share_on_%s'|trans) ) }} </nav>