Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
Sagiv Ofek authored
Showing with 17 additions and 1 deletion.
  1. +17 −1 lib/helpers.js
18 lib/helpers.js
View
@@ -3,7 +3,8 @@
*/
var path = require('path'),
fs = require('fs'),
- crypto = require('crypto');
+ crypto = require('crypto'),
+ _url = require('url');
/**
* Import utilities
@@ -223,6 +224,21 @@ HelperSet.prototype.linkTo = function linkTo(text, url, params) {
HelperSet.prototype.link_to = HelperSet.prototype.linkTo;
/**
+ * Link helper if not in the current url
+ *
+ * @param {String} text
+ * @param {String} url
+ * @param {Object} params - set of html params (class, style, etc..)
+ *
+ * <a href="url">text</a>
+ */
+HelperSet.prototype.linkToIfNotCurrent = function linkTo(text, url, params) {
+ if (url && url[0]=='/') url = url.substring(1); //trim first '/' if exists
+ return (url == _url.parse( this.controller.request.url ).pathname.substring(1) ) ? text : HelperSet.prototype.link_to(text, url, params) ;
+};
+HelperSet.prototype.link_to_if_not_current = HelperSet.prototype.linkToIfNotCurrent;
+
+/**
* Form tag helper
*/
HelperSet.prototype.formTag = function (params, block) {

1 comment on commit 2d3c46e

Sagiv Ofek
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.