/// <summary> /// Process the specified input template and return the result. /// </summary> /// <param name="inputTemplate"></param> /// <returns></returns> public LavaRenderResult GetTemplateRenderResult(ILavaEngine engine, string inputTemplate, LavaRenderParameters parameters = null, LavaTestRenderOptions options = null) { inputTemplate = inputTemplate ?? string.Empty; var context = engine.NewRenderContext(); if (parameters == null) { parameters = LavaRenderParameters.WithContext(context); } // If options are specified, replace the render parameters. if (options != null) { context.SetEnabledCommands(options.EnabledCommands, options.EnabledCommandsDelimiter); context.SetMergeFields(options.MergeFields); if (options.ExceptionHandlingStrategy != null) { parameters.ExceptionHandlingStrategy = options.ExceptionHandlingStrategy; } } var result = engine.RenderTemplate(inputTemplate.Trim(), parameters); return(result); }
/// <summary> /// Use Lava to resolve any merge codes within the content using the values in the merge objects. /// </summary> /// <param name="content">The content.</param> /// <param name="mergeObjects">The merge objects.</param> /// <param name="currentPersonOverride">The current person override.</param> /// <param name="enabledLavaCommands">A comma-delimited list of the lava commands that are enabled for this template.</param> /// <returns>If lava present returns merged string, if no lava returns original string, if null returns empty string</returns> public static string RenderLava(this string content, IDictionary <string, object> mergeObjects, Person currentPersonOverride, string enabledLavaCommands) { try { // If there have not been any EnabledLavaCommands explicitly set, then use the global defaults. if (enabledLavaCommands == null) { enabledLavaCommands = GlobalAttributesCache.Value("DefaultEnabledLavaCommands"); } var context = LavaService.NewRenderContext(); context.SetEnabledCommands(enabledLavaCommands, ","); context.SetMergeField("CurrentPerson", currentPersonOverride); context.SetMergeFields(mergeObjects); var result = LavaService.RenderTemplate(content, LavaRenderParameters.WithContext(context)); if (result.HasErrors) { throw result.GetLavaException(); } return(result.Text); } catch (Exception ex) { ExceptionLogService.LogException(ex, System.Web.HttpContext.Current); return("Error resolving Lava merge fields: " + ex.Message); } }
/// <summary> /// Use Lava to resolve any merge codes within the content using the values in the merge objects. /// </summary> /// <param name="content">The content.</param> /// <param name="mergeObjects">The merge objects.</param> /// <param name="currentPersonOverride">The current person override.</param> /// <param name="enabledLavaCommands">A comma-delimited list of the lava commands that are enabled for this template.</param> /// <param name="encodeStringOutput">if set to <c>true</c> [encode string output].</param> /// <returns>If lava present returns merged string, if no lava returns original string, if null returns empty string</returns> private static LavaRenderResult ResolveMergeFieldsWithCurrentLavaEngine(string content, IDictionary <string, object> mergeObjects, Person currentPersonOverride, string enabledLavaCommands, bool encodeStringOutput) { // If there have not been any EnabledLavaCommands explicitly set, then use the global defaults. if (enabledLavaCommands == null) { enabledLavaCommands = GlobalAttributesCache.Value("DefaultEnabledLavaCommands"); } var context = LavaService.NewRenderContext(); context.SetEnabledCommands(enabledLavaCommands, ","); if (currentPersonOverride != null) { context.SetMergeField("CurrentPerson", currentPersonOverride); } context.SetMergeFields(mergeObjects); var parameters = LavaRenderParameters.WithContext(context); parameters.ShouldEncodeStringsAsXml = encodeStringOutput; var result = LavaService.RenderTemplate(content, parameters); if (result.HasErrors && LavaService.ExceptionHandlingStrategy == ExceptionHandlingStrategySpecifier.RenderToOutput) { // If the result is an error, encode the error message to prevent any part of it from appearing as rendered content, and then add markup for line breaks. result.Text = result.Text.EncodeHtml().ConvertCrLfToHtmlBr(); } return(result); }
/// <summary> /// Process the specified input template and return the result. /// </summary> /// <param name="inputTemplate"></param> /// <returns></returns> public string GetTemplateOutput(ILavaEngine engine, string inputTemplate, ILavaRenderContext context) { inputTemplate = inputTemplate ?? string.Empty; var result = engine.RenderTemplate(inputTemplate.Trim(), LavaRenderParameters.WithContext(context)); return(result.Text); }
/// <summary> /// Merges the lava. /// </summary> /// <param name="lavaTemplate">The lava template.</param> /// <param name="context">The context.</param> /// <returns></returns> private string MergeLava(string lavaTemplate, ILavaRenderContext context) { // Resolve the Lava template contained in this block in a new context. var engine = context.GetService <ILavaEngine>(); var newContext = engine.NewRenderContext(); newContext.SetMergeFields(context.GetMergeFields()); newContext.SetInternalFields(context.GetInternalFields()); // Resolve the inner template. var result = engine.RenderTemplate(lavaTemplate, LavaRenderParameters.WithContext(newContext)); return(result.Text); }
/// <summary> /// Process the specified input template and return the result. /// </summary> /// <param name="inputTemplate"></param> /// <returns></returns> public string GetTemplateOutput(ILavaEngine engine, string inputTemplate, LavaTestRenderOptions options) { inputTemplate = inputTemplate ?? string.Empty; var context = engine.NewRenderContext(); var parameters = LavaRenderParameters.WithContext(context); if (options != null) { context.SetEnabledCommands(options.EnabledCommands, options.EnabledCommandsDelimiter); context.SetMergeFields(options.MergeFields); parameters.ExceptionHandlingStrategy = options.ExceptionHandlingStrategy; } var result = engine.RenderTemplate(inputTemplate.Trim(), parameters); return(result.Text); }
/// <summary> /// Process the specified input template and verify against the expected output. /// </summary> /// <param name="expectedOutput"></param> /// <param name="inputTemplate"></param> public void AssertTemplateOutput(ILavaEngine engine, string expectedOutput, string inputTemplate, LavaDataDictionary mergeValues = null, bool ignoreWhitespace = false) { var context = engine.NewRenderContext(mergeValues); AssertTemplateOutput(engine, expectedOutput, inputTemplate, LavaRenderParameters.WithContext(context), ignoreWhitespace); }
/// <summary> /// For each of the currently enabled Lava Engines, process the specified input template and verify against the expected output. /// </summary> /// <param name="expectedOutput"></param> /// <param name="inputTemplate"></param> public void AssertTemplateOutput(string expectedOutput, string inputTemplate, LavaDataDictionary mergeValues = null, bool ignoreWhitespace = false) { var parameters = LavaRenderParameters.WithContext(LavaRenderContext.FromMergeValues(mergeValues)); AssertTemplateOutput(expectedOutput, inputTemplate, parameters, ignoreWhitespace); }
/// <summary> /// Renders the specified context. /// </summary> /// <param name="context">The context.</param> /// <param name="result">The result.</param> public override void OnRender(ILavaRenderContext context, TextWriter result) { var rockContext = LavaHelper.GetRockContextFromLavaContext(context); // Get enabled security commands _enabledSecurityCommands = context.GetEnabledCommands().JoinStrings(","); using (TextWriter writer = new StringWriter()) { bool filterProvided = false; base.OnRender(context, writer); var parms = ParseMarkup(_markup, context); var lookAheadDays = parms[LOOK_AHEAD_DAYS].AsInteger(); var scheduleCategoryId = parms[SCHEDULE_CATEGORY_ID].AsIntegerOrNull(); var asAtDate = parms[AS_AT_DATE].AsDateTime(); var now = asAtDate ?? RockDateTime.Now; var scheduleIds = new List <int>(); var requestedSchedules = parms[SCHEDULE_ID].StringToIntList(); var schedulesQry = new ScheduleService(rockContext).Queryable().AsNoTracking() .Where(s => s.IsActive == true); if (requestedSchedules.Count() > 0) { schedulesQry = schedulesQry.Where(s => requestedSchedules.Contains(s.Id)); filterProvided = true; } if (scheduleCategoryId.HasValue) { schedulesQry = schedulesQry.Where(s => s.CategoryId == scheduleCategoryId.Value); filterProvided = true; } // If neither a schedule id nor a schedule category id was provided stop if (!filterProvided) { return; } // Get the schedules are order them by the next start time var schedules = schedulesQry.ToList() .Where(s => s.GetNextStartDateTime(now) != null) .OrderBy(s => s.GetNextStartDateTime(now)); if (schedules.Count() == 0) { return; } var nextSchedule = schedules.FirstOrDefault(); var nextStartDateTime = nextSchedule.GetNextStartDateTime(now); var isLive = false; DateTime?occurrenceEndDateTime = null; // Determine if we're live if (nextSchedule.WasScheduleActive(now)) { isLive = true; var occurrences = nextSchedule.GetICalOccurrences(now, now.AddDays(lookAheadDays)).Take(2); var activeOccurrence = occurrences.FirstOrDefault(); occurrenceEndDateTime = (DateTime)activeOccurrence.Period.EndTime.Value; // Set the next occurrence to be the literal next occurrence (vs the current occurrence) nextStartDateTime = null; if (occurrences.Count() > 1) { nextStartDateTime = occurrences.Last().Period.EndTime.Value; } } // Determine when not to show the content if ((parms[SHOW_WHEN] == "notlive" && isLive) || (parms[SHOW_WHEN] == "live" && !isLive)) { return; } // Check role membership var roleId = parms[ROLE_ID].AsIntegerOrNull(); if (roleId.HasValue) { var currentPerson = GetCurrentPerson(context); var isInRole = new GroupMemberService(rockContext).Queryable() .Where(m => m.GroupId == roleId && m.PersonId == currentPerson.Id ) .Any(); if (!isInRole) { return; } } var mergeFields = context.GetMergeFields(); mergeFields.Add("NextOccurrenceDateTime", nextStartDateTime); mergeFields.Add("OccurrenceEndDateTime", occurrenceEndDateTime); mergeFields.Add("Schedule", nextSchedule); mergeFields.Add("IsLive", isLive); var engine = context.GetService <ILavaEngine>(); var renderContext = engine.NewRenderContext(mergeFields, _enabledSecurityCommands.SplitDelimitedValues()); var results = engine.RenderTemplate(_blockMarkup.ToString(), LavaRenderParameters.WithContext(renderContext)); result.Write(results.Text.Trim()); } }