Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added link_to_if_not_current() function similar to rails.

display a <a> tag only if not the current page. if current page display text without <a> tag.
  • Loading branch information...
commit 2d3c46ea16afffd4821f0774ea0ba4813308c163 1 parent e88323d
authored

Showing 1 changed file with 17 additions and 1 deletion. Show diff stats Hide diff stats

  1. +17 1 lib/helpers.js
18 lib/helpers.js
@@ -3,7 +3,8 @@
3 3 */
4 4 var path = require('path'),
5 5 fs = require('fs'),
6   - crypto = require('crypto');
  6 + crypto = require('crypto'),
  7 + _url = require('url');
7 8
8 9 /**
9 10 * Import utilities
@@ -223,6 +224,21 @@ HelperSet.prototype.linkTo = function linkTo(text, url, params) {
223 224 HelperSet.prototype.link_to = HelperSet.prototype.linkTo;
224 225
225 226 /**
  227 + * Link helper if not in the current url
  228 + *
  229 + * @param {String} text
  230 + * @param {String} url
  231 + * @param {Object} params - set of html params (class, style, etc..)
  232 + *
  233 + * <a href="url">text</a>
  234 + */
  235 +HelperSet.prototype.linkToIfNotCurrent = function linkTo(text, url, params) {
  236 + if (url && url[0]=='/') url = url.substring(1); //trim first '/' if exists
  237 + return (url == _url.parse( this.controller.request.url ).pathname.substring(1) ) ? text : HelperSet.prototype.link_to(text, url, params) ;
  238 +};
  239 +HelperSet.prototype.link_to_if_not_current = HelperSet.prototype.linkToIfNotCurrent;
  240 +
  241 +/**
226 242 * Form tag helper
227 243 */
228 244 HelperSet.prototype.formTag = function (params, block) {

1 comment on commit 2d3c46e

Owner

creates a 'a href' tag only if not in the current page. if the link and the current page are the same- returns the link text.

Please sign in to comment.
Something went wrong with that request. Please try again.