Page 1 of 1

Get Scriptlet HeaderData so that it parses nVelocity?

Posted: Wed Feb 01, 2012 3:16 pm
by gjaros
Is it possible to get the HeaderData in a scriptlet to parse nVelocity code? I'd like to put a meta tag on pages that contains the URL of the product image for use with the Facebook Open Graph tags. Basically, I'd like this to display in the header:

Code: Select all

#if($Product.ImageUrl != '')
<meta property="og:image" content="${Page.ResolveUrl(Product.ImageUrl)}"/>
#else
<meta property="og:image" content="Store_Logo.png"/>
#end
But it just outputs exactly what is entered without parsing the nVelocity...

Thanks!

Re: Get Scriptlet HeaderData so that it parses nVelocity?

Posted: Thu Feb 02, 2012 10:58 am
by david-ebt
I'm not sure if nVelocity has access to the header. Here's a simple conlib that will add many of the Facebook Open Graph tags. I've called it FaceBookTags.ascx and then call it in the Show Product scriptlet. If you have thumbnail images, you might want to change the ImageUrl to ThumbnailUrl - it should display better on Facebook.

Code: Select all

<%@ Control Language="C#" ClassName="FaceBookTags" %>

<script runat="server">

    private string _newTitle;
    public string NewTitle 
    {
        get { return (String.IsNullOrEmpty(_newTitle)) ? Page.Title : _newTitle; }
        set { _newTitle = value; }
    }

    protected void Page_Load( object sender, EventArgs e )
    {
        int _ProductId = AlwaysConvert.ToInt(Request.QueryString["ProductId"]);
        Product _Product = ProductDataSource.Load(_ProductId);

        if (_Product != null)
        {
            HtmlMeta ogImage = new HtmlMeta();
            HtmlMeta ogUrl = new HtmlMeta();
            HtmlMeta ogTitle = new HtmlMeta();
            HtmlMeta ogSite = new HtmlMeta();

            ogImage.Name = "og:image";
            ogUrl.Name = "og:url";
            ogTitle.Name = "og:title";
            ogSite.Name = "og:site_name";

            if (!string.IsNullOrEmpty(_Product.ImageUrl))
                ogImage.Content = _Product.ImageUrl;
            else
                ogImage.Content = "storelogo.png";
            
            ogUrl.Content = Token.Instance.Store.StoreUrl + _Product.NavigateUrl.Replace("~/",null);
            ogTitle.Content = _Product.Name;
            ogSite.Content = Token.Instance.Store.Name;

            Page.Header.Controls.Add(ogImage);
            Page.Header.Controls.Add(ogUrl);
            Page.Header.Controls.Add(ogTitle);
            Page.Header.Controls.Add(ogSite);
        }
    }

</script>
There are other open graph tags. Another that is often useful is the og:type which tells what it is you're talking about, something like "product".

Here's a link to a page that describes many of the other tags:
http://ogp.me/

Re: Get Scriptlet HeaderData so that it parses nVelocity?

Posted: Fri Feb 03, 2012 7:31 pm
by gjaros
Thanks! That worked great!