Page 1 of 1

Simple social bookmarks with smarty

Posted: Tue Mar 04, 2014 4:02 pm
by scooper
Just needed to add share buttons for Twitter and Facebook for a site and rather than add javascript or a module for handling them I wondered if you could just use some smarty tags to build the links ... and of course you can:

Code: Select all

{capture assign="currenturl"}http://{$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI}{/capture}

<a class="twitIntent" href="https://twitter.com/intent/tweet?url={$currenturl|escape:'url'}"><span>Share on Twitter</span></a>

<a class="fbIntent" href="http://www.facebook.com/sharer.php?u={$currenturl|escape:'url'}">Share on Facebook</a>
That captures the URL of the page you're looking at (so it works for blog posts etc) then uses Smarty's escape modifier to encode the url to add to our Twitter / Facebook intent links.

Re: Simple social bookmarks with smarty

Posted: Wed Mar 05, 2014 11:29 am
by psy
V. cool :)

There was a post by Calguy1000 sometime ago (cannot find it) that said that {capture} is resource intensive and should if possible be avoided.

You could rewrite your template code to be more efficient as:

Code: Select all

{$currenturl="http://"|cat:$smarty.server.HTTP_HOST|cat:$smarty.server.REQUEST_URI} 
If you have CGSimpleSmarty installed it may even be as easy as {$cgsimple->self_url()} in your <a> tags.

Re: Simple social bookmarks with smarty

Posted: Thu Jul 31, 2014 8:28 am
by SimonJ
scooper wrote:Just needed to add share buttons for Twitter and Facebook for a site and rather than add javascript or a module for handling them I wondered if you could just use some smarty tags to build the links ... and of course you can:

Code: Select all

{capture assign="currenturl"}http://{$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI}{/capture}

<a class="twitIntent" href="https://twitter.com/intent/tweet?url={$currenturl|escape:'url'}"><span>Share on Twitter</span></a>

<a class="fbIntent" href="http://www.facebook.com/sharer.php?u={$currenturl|escape:'url'}">Share on Facebook</a>
That captures the URL of the page you're looking at (so it works for blog posts etc) then uses Smarty's escape modifier to encode the url to add to our Twitter / Facebook intent links.
This works well for me, doesn't include the gallery title but does add a thumbnail to the link share.
Thank you.