How do I evaluate a helper in Jade without outputting its result? - node.js

I have a Jade template that needs to call a helper, but not display its output:
// views/foo.html.jade:
p
Some content...
#{ someHelperSetterMethod('bar'); }
Unfortunately, since someHelperSetterMethod returns nothing, I get "undefined" output in my template. Is there a way to do non-outputting evaluation?

p
some content
- someHelperSetterMethod('bar')

Related

getting the value of an array of object in handlebar

I'm working with expressJS and handlebar as an engine template in my index.hbs I have a JS script in which I need to get a value of an array of object, here is the code of my script
<script >
new Morris.Line({
element: 'myfirstchart',
parseTime:false,
data: {{graph}},
xkey: 'version',
ykeys: ['success'],
labels: ['Success']
});
</script>
but the array graph does not pass, in my log console it is shown like
this
What should I do to get the value of my {{graph}} ?
Please add a sample of your data in {{graph}} and also what you expect instead of [object Object]. You'll need to format your data with the handlebar template {{graph}} contains one array of 5 elements containing objects.
If your data is let say {'x': '100', 'y':200} and that you want the same output with handlebar then you should put this instead of {{graph}}:
[{{#each graph}}{'x': {{x}}, 'y': {{y}} }{{/each}}]
If you put your data and the expected output format I'll may give a more accurate answer.
You need to stringify your array object i.e. JSON.stringify(graph) from server side and while accessing it use triple brackets {{{graph}}} in your javascript script tag code.

How do we use "lookup" in Nodejs to include partials?

We use this syntax to include partial during run time:
{{> (lookup . 'file') }}
file is a var name from the parent file.
I tried to add a prefix to the file name, So I tried:
{{> lookup . 'path/file'}}
{{> (lookup . (strmerge 'path/' 'file')) }}
Note: I made a helper method to merge strings
I tried those and others but nothing worked for me.
Does any one know how to do this?
Thanks
In the code {{> (lookup . 'file') }} we are telling Handlebars that the name of our partial is to be found at the file property of the current context object.
Assuming a context object like { file: 'myPartial' }, the result of the lookup is {{> myPartial }}, which tells Handlebars to render a partial called "myPartial".
If we want to add a prefix to our partial, so that Handlebars will register a partial called "path/myPartial", the simplest way to do this would be to add that path to the value of the file property in the context object. The context object would become: { file: 'path/myPartial' }.
If, for some reason, the "path/" prefix must be added to the template and not the data, then we will need to determine a way to produce the String "path/myPartial" from our current data.
Both of your attempts put "file" in the name of the property to be looked-up. Your code will try to find the property path/file on the context object and this will fail. We will definitely need a helper to concatenate Strings, but it must concatenate "path/" with the value of file, not the literal String, "file".
To achieve our goal we will no longer require the lookup helper. The lookup was needed only because you can't write {{> (file) }} in Handlebars, because Handlebars will treat file as a helper instead of as a variable. However, since we are using a concatenation helper, strmerge, we can use the String it returns as our partial name, without any need for a lookup. The correct code becomes:
{{> (strmerge 'path/' file) }}
It's important to note that file in this example is not in quotes. It is a variable, not a String.
I have created a fiddle for your reference.

Angular JS ng-click action function as string

I am creating an application where the site menu would be dynamically loaded from JSON file. Each menu may correspond to an action that would be defined inside the ng-click directive. This would look something like this
<li ng-repeat="menuItem in menuContainer.menus" class="{{menuItem.cssClass}}">
<a href="{{menuItem.url}}" ng-click="{{menuItem.clickAction}}">
<i class="{{menuItem.iconClass}}"></i>{{menuItem.name}}
<span class="badge">{{menuItem.subMenus.length}}</span>
</a>`enter code here`
<li>
Now the problem is ng-click does not recognize the clickAction as a function, I believe this is due to linking process. I want to know is there any way to evaluate a string to method. I tried do $eval but it executes the function on load.
How do I do this?
Define methods not as strings, but as functions and replace ng-click="{{menuItem.clickAction}}" to ng-click="menuItem.clickAction()". Another way to define function on $scope, like:
$scope.executeString = function(body){
eval(body);
};
and replace your ng-click to ng-click="executeString(menuItem.clickAction)". Anyway, use eval is antipattern;)
Remember, that ng-click and other directives, like that, takes angular expression as parameter. And if body of you expression is a = b + c than angular convert it in javascript like $scope.a = $scope.b + $scope.c

Better way to ucfirst in a Jade template?

Is there a better way to capitalize the first character of a string in Jade than this?
for list in project.lists
- list.name = list.name.charAt(0).toUpperCase() + list.name.slice(1);
li #{list.name}
Doing this every time I want to capitalize a variable is ugly, is there any way in Jade that I can define a custom function that I have available in every template like:
for list in project.lists
li #{ucfirst(list.name)}
Thanks in advance!
The contents of #{} are executed as standard JS, so you can pass in helper functions for use with things like that. You haven't specified, but assuming you are using Jade along with Express, you can do something like this:
app.locals.ucfirst = function(value){
return value.charAt(0).toUpperCase() + value.slice(1);
};
That will expose a function called ucfirst within the Jade template. You could also pass it in as part of locals every time you render, but if you are using Express it will do it automatically.
If you're willing to resort to CSS, you can create a class that capitalizes the first letter of every word within the target element.
CSS
.caps {
text-transform: capitalize;
}
Jade
div.caps
each foo in ['one', 'two', 'three']
span #{foo}
Resulting HTML
<div class="caps"><span>one</span><span>two</span><span>three</span>
Resulting view
One Two Three
If you are using pug with gulp, this can be helpful:
mixin ucfirst(text)
- text = text.charAt(0).toUpperCase() + text.slice(1);
.
#{text}
Simply call this as any other mixin:
li
+ucfirst(list.name)

How do I form the gstring below ?

I am getting an error on forming the gstring below, can someone suggest the right way to form this gstring?
for(File fileToUnarchive: filesToUnarchive)
{
antBuilder.mkdir(dir:"${destinationDirectory}/${getSequenceNumber(${fileToUnarchive.name})}")
}
The problem is you're trying to double template the last section
Instead of
antBuilder.mkdir(dir:"${destinationDirectory}/${getSequenceNumber(${fileToUnarchive.name})}")
just do
antBuilder.mkdir(dir:"${destinationDirectory}/${getSequenceNumber( fileToUnarchive.name )}")

Resources