Posted by Arjun on Saturday 22nd November 2014

Laravel Facebook Open Graph Macro

Here is a custom HTML macros for Facebook Open Graph meta tags. if you don't had any idea about FB open Graph meta tags , don't worry visit my previous post : Facebook Open Graph META Tags

Laravel allowing us to extend the HTML facade with additional functionality. we use the HTML::macro() method to extend the HTML facade with our own methods.

Facebook Open Graph meta tags Macro

HTML::macro('fbog', function($property, $content)
{
return '<meta property="og:'. $property .'" content="'. $content .'" />'. PHP_EOL;
});

How to use:

Place the above macro code into your app/start/global.php file to register it. Then place place this code in your views.

{{ HTML::fbOgMeta("type","profile") }}
{{ HTML::fbOgMeta("title","Website Name"); }}
{{ HTML::fbOgMeta("description","Website  Description") }}
{{ HTML::fbOgMeta("image","https://website.com/image250X250.png") }}
{{ HTML::fbOgMeta("url","https://arjunphp.com") }}
{{ HTML::fbOgMeta("site_name","Website Name") }}
{{ HTML::fbOgMeta("see_also","https://arjunphp.com") }}

Output:

you may get below output in your html source

<meta property="og:title" content="Website Name" />

<meta property="og:description" content="Website  Description" />

<meta property="og:image" content="https://website.com/image250X250.png" />

<meta property="og:url" content="https://arjunphp.com" />

<meta property="og:site_name" content="Website Name" />

<meta property="og:see_also" content="https://arjunphp.com" />