function relativePath()
A function for determining the relative path from one file to another.
by justjohn
JavaScript
function relativePath(base, file) {
var sep_chr = '/',
base_path = base.split(sep_chr),
new_path = [],
val;
// Remove file from url
base_path.pop();
base_path = base_path.concat(file.split(sep_chr));
while (base_path.length > 0) {
val = base_path.shift();
if (val == ".") {
// Ignore
} else if (val == ".." && new_path.length > 0 && new_path[new_path.length-1] != "..") {
new_path.pop();
} else {
new_path.push(val);
}
}
return new_path.join(sep_chr);
}
console.log(relativePath("../../test/template.twig", "../path/to/awesone/view.twig"));