In Handlebars.Net, if there is no matching field, it just places a blank there.
string source = #"My name is {{Name}} and I work at {{Job}}";
var template = Handlebars.Compile(source);
var data = new {
Job = "Walmart"
};
var result = template(data);
Results in this because the {{Name}} is not in the data.
My name is and I work at Walmart
Is there a setting to say, just don't replace it if the data field does not exist?
I'd like for it to return:
My name is {{Name}} and I work at Walmart
There are two options:
Supported in 1.x: use UnresolvedBindingFormatter
handlebars.Configuration.UnresolvedBindingFormatter = "('{0}' is undefined)";
Supported starting from 2.0.0-preview-1: use helperMissing hook
handlebars.RegisterHelper("helperMissing", (context, arguments) =>
{
var name = arguments.Last().ToString();
return "{{" + name.Trim('[', ']') + "}}";
});
For more details see this GitHub issue.
I think you would have to use an #if, as in:
My name is {{#if Name}}{{Name}}{{else}}\{{Name}}{{/if}} and I work at {{Job}}
Note: I owe thanks to this answer for how to tell Handlebars to render the braces.
Related
I am using YDN-DB for my web POS, library using clone from following source:
https://raw.githubusercontent.com/yathit/ydn-db/master/jsc/ydn.db-dev.js
All is going well, incorporation with indexed db.
The only concern is, i want to use LIKE for my query as shows below, but it does not work and return sql parsing error, it looks LIKE does not work in YDN-DB, can you please advise any solution to achieve this?
APP.db.executeSql("SELECT * FROM products WHERE name LIKE '%test%' ").then(function(results) {
console.log(results);
});
However following works good.
APP.db.executeSql("SELECT * FROM products WHERE name = 'test'").then(function(results) {
console.log(results);
});
i have tried the other way aswell, but available operator only supports the start of string search, as shows below:
var q = APP.db.from( 'products' ).where('name', '^', 'New');
var limit = 10000;
var result = [];
q.list( limit ).done( function( objs ) {
result = objs;
console.log(result);
});
Can any of you please help me out to achieve the nieche.
Is there a simple way to modify this code to return only records where LocationID matches the id I'm trying to pass as a parameter? Needless to say, this doesn't compile. I thought Entity Framework was meant to make things easier, but I've searched online and can't find an understandable example of how to assign a simple query where a field in a single table/entity matches a number.
public async Task<List<PC>> GetPCsAsync(int id)
{
// Get our data. Don't yet know how to feed the variable to EF/Linq
PCList = await (from p in db.PC
select new PC {p.LocationID = id}).ToListAsync();
return PCList;
}
Thanks.
And also if you want to do it using Query Syntax it would be something like this:
PCList = await (from p in db.PC
where p.LocationID == id
select p).ToListAsync();
Here's a link to understand the differences between Query and Method syntax.
var list = db.PC.Where(x=>x.LocationID == id).ToList();
for async
var listAsync = await db.PC.Where(x=>x.LocationID == id).ToListAsync();
I hope it's help you!
I have a list of company names I'm populating from a collection
the helper function I have is:
Template.companyList.helpers({
companies: function () {
return Companies.find({owner: Meteor.userId()}, {sort: {a: 1}, name:1, createdAt:1});
}
});
It's looped through using a
{{#each companies}}
which outputs
<LI> Company Name </LI>
Above this I have a text box, and would like to filter the list of companies by what I type in the textbox - I'd prefer to have a "containing" filter as opposed to "starting with" filter, but i'll take either one - is there an established way of doing this in Meteor? If not, is there a plugin that someone wrote that does this?
Also, whatever answer you give, please consider the fact that I've been using Meteor for, oh, 5 days now, and i'm still learning it, so, a Newbie style answer would be great.
Thanks for Reading!
edit
This is the updated answer I came up with - combining David's answer with my previous companies helper:
Template.companyList.helpers({
companies: function () {
var query = Session.get('query');
selector = {owner: Meteor.userId()};
options = {sort: {a: 1}, companyName:1, createdAt:1};
if (query && query.length) {
var re = new RegExp(query, 'i');
selector.companyName = re;
}
return Companies.find(selector, options);
}
});
Here is the outline for a simple search-as-you-type interface:
Template.myTemplate.helpers({
companies: function() {
// build a regular expression based on the current search
var search = Session.get('search');
var re = new RegExp(search, 'i');
selector = {owner: Meteor.userId()};
// add a search filter only if we are searching
if (search && search.length)
selector.name = re;
options = {sort: {createdAt: -1}};
return Companies.find(selector, options);
}
});
Template.myTemplate.events({
'keyup #search': function() {
// save the current search query in a session variable as the user types
return Session.set('search', $('#search').val());
}
});
This assumes:
You are trying to search Companies by name.
You have an input with an id of search.
Please modify as needed for your use case. Let me know if you have any questions.
In jinja2(python) template engine there is a simple thing for truncating strings:
{{ fooText|truncate(200) }}
Does meteor(handlebars) provides something like this?
I am using values as options, starting value as well as ending value passed as arguments form template. Try this:
Handlebars.registerHelper('trimString', function(passedString, startstring, endstring) {
var theString = passedString.substring( startstring, endstring );
return new Handlebars.SafeString(theString)
});
In template:
<p>{{{trimString value 0 300}}}</p>
it'll print first 300 characters of the value. Hope this help you.
I never use | on spacebars (the engine used on meteor template), but you can do a helper to accomplish this(for example a global Template.registerHelperr).
Template.registerHelper('text', function(passedString) {
var fooText = passedString.substring(0,1); //same as truncate.
return new Spacebars.SafeString(fooText)
});
And use it like {{ text myString}}
Here we are using some Blaze and the substring method.
I use Places library to autocomplete address input. Search is limited to only one city, and I get output like this:
"Rossiya, Moskva, Leninskiy prospekt 28"
How to hide "Rossiya, Moskva"? ...
My query:
function() {
// Search bounds
var p1 = new google.maps.LatLng(54.686534, 35.463867);
var p2 = new google.maps.LatLng(56.926993, 39.506836);
self.options = {
bounds : new google.maps.LatLngBounds(p1, p2),
componentRestrictions: {country: 'ru'},
};
var elements = document.querySelectorAll('.address');
for ( var i = 0; i < elements.length; i++) {
var autocomplete = new google.maps.places.Autocomplete(elements[i],
self.options);
}
You can but you have to replace the value of the input field in two places.
Example:
var autocomplete = new google.maps.places.Autocomplete(input, placesOptions);
var input = document.getElementById('searchTextField');
inside the 'place_changed' event you need to do the following:
placeResult = autocomplete.getPlace();
//This will get only the address
input.value = placeResult.name;
This will change the value in the searchtextfield to the street address.
The second place is a bit tricky:
input.addEventListener('blur', function(){
// timeoutfunction allows to force the autocomplete field to only display the street name.
if(placeResult){ setTimeout(function(){ input.value = placeResult.name; }, 1); } });
The reason why we have to do this is because if you only add the event listener for blur, google places will populate the input field with the full address, so you have to 'wait' for google to update and then force your change by waiting some miliseconds.
Try it without the setTimeout function and you will see what I mean.
EDIT
You can't. I had it the other way around, that you were just looking for a city. There is no way to only print out the street name (I'm assuming that's a street name) from the address component.
OPPOSITE OF WHAT WAS ASKED
From the docs:
the (cities) type collection instructs the Place service to return results that match either locality or administrative_area3.
var input = document.getElementById('searchTextField');
var options = {
bounds: defaultBounds,
types: ['(cities)']
};
autocomplete = new google.maps.places.Autocomplete(input, options);
in result u have hash and from it u can get part what u want:
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
now from "place" u can get it
place.geometry.location.lat()
and for address
place.address_components[0] or place.address_components[1] ...
depends on what u want to get
I had a very similar problem which indeed was solvable. This in an Angular 2 project but it should be applicable elsewhere as well. I filter my results for establishments, and wanted to show only the name and hide the address part of the result. This did the trick for me, a function executing once you select a suggestion:
getAddress(place: Object) {
this.zone.run(() => {
this.establishment = place['name'];
});
where zone is an NgZone component injected in the constructor and this.establishment is the variable tied to [(NgModel)] in the input field.
Inside place_changed set a timeout function:
var streetString = place.address_components[0] or place.address_components[1];
window.setTimeout(function() {
$('input').val(streetString);
}, 200);
This solution worked for me.