So after part 2, you should feel fairly comfortable with getting the information you need into your template. Now that you can get the info in, we can work on getting only some of the information in. Or only showing it in certain situations. Or substituting something else in other situations. To do all that, we've got some more helpers:
- join-distinct
- if
- if + else
- unless + else
- in and notin
join-distinct
join-distinct is used when you have two fields showing a range but if the fields are the same, you only want to see the first one. The usual scenario for this is date ranges.
Many of the documents you build will need to show the overall event range. In the TemplateEventModel, this would look like this:
{{date Event.Start}} - {{date Event.End}}
But if the event is a one day event, you'll end up with matching start and end dates and it just doesn't look good. So we're going to use the join-distinct helper. This tells the template to join the two dates if they are distinct (different). This looks like this:
{{join-distinct (date Event.Start) (date Event.End) ' - '}}
And now in a video! Just the fields, and then the fields with the join-distinct:
- Add your handlebars and helper.
- Add the two fields. Use parenthesis rather than curly quotes!
- Add how you would like them joined inside single quotes - in this case space dash space.
if
if is a way to add a condition -- If a field is true do this. If it is false, skip this. The expression (in a nutshell) is:
{{#if this thing in here is true }} this stuff {{/if}}
There are lots of things that you can put in the first part, but you have to be able to get a true or false from what you put in there. Some notes:
- If a field is optional (it can be empty), then you can use it. A great example is notes - if there is a note, show the field. Or if there is a decision date, show the decision date.
- Be sure to test this though! Since it is looking for a false statement, zero may register as false. (I'll update this as I get more information).
- Lists are trickier, because the list actually will exist in the database even if it's empty, so it's always going to return a true (not helpful!). This is why you'll see the boolean fields -- those were created to give you the correct true/false you need.
Some examples of when/how you would use this:
- On invoices, scheduled payments if there are any
- On event orders, the if conditional is key to only show sections if there is information:
- if Room Setup
- if Attendance
- if Department Requirements (items/instructions)
- If items
- If Instructions
if + else
If statements can be paired with else statements -- this allows you to say if this is true do this, otherwise do this.
For example, in the last section, we learned how you can use an if statement to only show the time if there is one entered. You would use this on items and instructions, where a time is optional.
{{#if HasTime}} {{join-distinct (time StartTime) (time EndTime) ' - '}} {{/if}}
On booked spaces, though, it will be all day or have times. By pairing if and else, you can display either "all day" or the start and end time.
{{#if IsAllDay}}All Day{{else}}{{join-distinct (time Start) (time End) ‘ - ‘}}{{/if}}
Note the formatting:
- if has a hash and must be closed.
- else does not have a hash and is not closed.
Another scenario where this is useful is with inventory items. F&B and event services items just have a quantity; Labor has quantity and item count. You can use if and else to show both fields if it's a labor item but only the quantity field if it is f&b or event services.
{{number Quantity}} {{ItemName}} {{#if IsLaborItem}} - {{number UnitCount}} {{UnitName}}{{/if}}
VIDEO
unless
Unless is the opposite of if. Its block will be shown if the expression returns a false value.
This will only return function names if the functions are NOT event-wide.
{{#unless IsEventWide}}{{Name}}{{/unless}}
unless + else
unless and else (unless is labor item, show quantity else show quantity and unit count)
{{#if Date}}{{date Date.Date "MM/dd/yy"}}{{#unless Date.IsAllDay}}
{{join-distinct (time Date.StartTime) (time Date.EndTime) ' - '}}{{/unless}}{{/if}}
from Brian's doc:
in and notin
in / notin - use with if or unless - used with arrays, if you don't want to include all items this allows you to filter things out. we have to do it this way, because of custom items.
{{#each Event.StaffAssignments}}{{#if (in StaffAssignmentName 'Sales Manager')}}{{StaffMemberName}}{{/if}}{{/each}} -- a way to pull in only the Sales Manager, not all users assigned to the event.
Additional notes
- This article will hopefully get you started and help you understand the theory behind handlebars and the templates. I highly recommend studying various templates and trying to understand the language and handlebar structure. Feel free to copy a template into the QA account on staging so you can play with it, try typing out the fields yourself, etc.
- Part four will cover the various different ways of grouping event requirements (items and instructions). Understanding the helpers in parts two and three will help you as you build the layers needed to group things as you wish.
- In the templates sidebar helper, click the ? icon to see a list of custom helpers we've built into our handlebars, but please note that it has to be updated manually and may be out of date. There's also a link to the standard handlebars helpers.
Comments
0 comments
Article is closed for comments.