Simple social bookmarks with smarty

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Locked
scooper
Forum Members
Forum Members
Posts: 242
Joined: Fri Dec 09, 2005 12:36 pm
Location: Marlow, UK

Simple social bookmarks with smarty

Post 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.
psy
Power Poster
Power Poster
Posts: 463
Joined: Sat Jan 22, 2005 11:19 am

Re: Simple social bookmarks with smarty

Post 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.
SimonJ

Re: Simple social bookmarks with smarty

Post 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.
Locked

Return to “Tips and Tricks”