Reputation: 10089
I would like to create a function in jade but I cant' find how
I found something in the javadoc about finctions https://github.com/visionmedia/jade ,but I don't understand
I would like to try if a variable exist if not return the var name, a little like this php script:
function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
{
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
if(!isset($var)){echo vname($var)}
Thanks
Upvotes: 2
Views: 641
Reputation: 34632
It basically comes down to adding a function to the locals object. You can do this in a few different places.
Route:
app.get('/test', function (req, res) {
res.locals.someFunction = function () { /* do stuff */ };
});
General Middleware:
app.use(function (req, res, next) {
res.locals.someFunction = function () { /* do stuff */ };
next();
});
Application:
app.locals.someFunction = function () { /* do stuff */ };
Is this super basic stuff and is normally documented on the Express.js website.
Upvotes: 5