@@ -38,6 +38,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
3838 public static readonly activityAttachmentFunctionName = 'ActivityAttachment' ;
3939 public static readonly fromFileFunctionName = 'fromFile' ;
4040 public static readonly templateFunctionName = 'template' ;
41+ public static readonly isTemplateFunctionName = 'isTemplate' ;
4142
4243 public constructor ( templates : LGTemplate [ ] , expressionEngine : ExpressionEngine ) {
4344 super ( ) ;
@@ -399,6 +400,10 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
399400 return new ExpressionEvaluator ( Evaluator . activityAttachmentFunctionName , BuiltInFunctions . apply ( this . activityAttachment ( ) ) , ReturnType . Object , this . validateActivityAttachment ) ;
400401 }
401402
403+ if ( name === Evaluator . isTemplateFunctionName ) {
404+ return new ExpressionEvaluator ( Evaluator . isTemplateFunctionName , BuiltInFunctions . apply ( this . isTemplate ( ) ) , ReturnType . Boolean , this . validateIsTemplate ) ;
405+ }
406+
402407 return baseLookup ( name ) ;
403408 }
404409
@@ -419,6 +424,22 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
419424 } ) ;
420425 }
421426
427+ private readonly isTemplate = ( ) : any => ( args : readonly any [ ] ) : boolean => {
428+ const templateName = args [ 0 ] . toString ( ) ;
429+ return templateName in this . templateMap ;
430+ }
431+
432+ private readonly validateIsTemplate = ( expression : Expression ) : void => {
433+ if ( expression . children . length !== 1 ) {
434+ throw new Error ( `isTemplate should have one parameter` ) ;
435+ }
436+
437+ const children0 : Expression = expression . children [ 0 ] ;
438+ if ( children0 . returnType !== ReturnType . Object && children0 . returnType !== ReturnType . String ) {
439+ throw new Error ( `${ children0 } can't be used as a template name, must be a string value` ) ;
440+ }
441+ }
442+
422443 private readonly fromFile = ( ) : any => ( args : readonly any [ ] ) : any => {
423444 const filePath : string = path . normalize ( ImportResolver . normalizePath ( args [ 0 ] . toString ( ) ) ) ;
424445 const resourcePath : string = this . getResourcePath ( filePath ) ;
0 commit comments