Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Absolute Path of Jekyll Bootstrap Page

I tried to add a facebook share link to a jekyll bootstrap page, however when adding the href that FB will use in the share I used {{ HOME_PATH }}. This turns out to be the relative path of /, however (which FB does not understand). Does anybody know how to get the absolute path without having to hard-code it?

Code that gives a /

<a name="fb_share" type="button"
           share_url="{{ HOME_PATH }}">Share this event on Facebook</a>
like image 395
Justin Pihony Avatar asked Apr 11 '12 05:04

Justin Pihony


People also ask

How do you do absolute path?

To find the full absolute path of the current directory, use the pwd command. Once you've determined the path to the current directory, the absolute path to the file is the path plus the name of the file.

Does Jekyll work with Bootstrap?

Import Bootstrap into the projectThe empty front matter at the beginning of the file allows Jekyll to precompile the SCSS into CSS automatically. ---@import "main"; @import 'bootstrap/bootstrap'; Finally, tell the application to compile the CSS from the assets folder.

How do you convert an absolute path to a relative path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

What is the difference between path and absolute path?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).


1 Answers

Unfortunately, not via Jekyll. Jekyll is a static site generator, when it runs, it generates the whole site as HTML/CSS/etc pages, no server-side dynamic content.

However, there are solutions: hard-code the url, but only in one place so it is easy to change; or, use javascript to set it appropriately client-side.

Pure Jekyll/Liquid

Add a line like

host: example.com

to _config.yml, and then refer to it like

<a name="fb_share" type="button"
           share_url="http://{{ site.host }}/blah">Share this event on Facebook</a>

Javascript

If you are using jQuery, put this somewhere in your start-up scripts (obviously you can do this without jQuery, but this illustrates the technique):

$('[name=fb_share]').each(function() {
    var $this = $(this);
    $this.attr('share_url', 
               $this.attr('share_url').replace('HOME_PATH', 
                                               document.location.host));
}

and have the page like

<a name="fb_share" type="button"
           share_url="http://HOME_PATH/blah">Share this event on Facebook</a>

(The JS solution may require some trickery (like delaying loading the Facebook JS) depending on when/how the scripts provided by Facebook run.)

like image 167
huon Avatar answered Oct 06 '22 01:10

huon