/// <summary> /// Set a merge field value within the specified scope. /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="scopeReference">root|parent|current</param> public override void SetMergeField(string key, object value, LavaContextRelativeScopeSpecifier scope = LavaContextRelativeScopeSpecifier.Current) { var localScope = _contextScopeInternalField.GetValue(_context) as Scope; if (scope == LavaContextRelativeScopeSpecifier.Current) { _context.SetValue(key, value); } else if (scope == LavaContextRelativeScopeSpecifier.Parent) { var parentScope = _scopeParentInternalField.GetValue(localScope) as Scope ?? localScope; parentScope.SetValue(key, FluidValue.Create(value, _context.Options)); } else if (scope == LavaContextRelativeScopeSpecifier.Root) { var parentScope = _contextScopeInternalField.GetValue(_context) as Scope; var outerScope = _scopeParentInternalField.GetValue(parentScope) as Scope; while (outerScope != null) { parentScope = outerScope; outerScope = _scopeParentInternalField.GetValue(outerScope) as Scope; } parentScope.SetValue(key, FluidValue.Create(value, _context.Options)); } else { throw new LavaException($"SetMergeFieldValue failed. Scope reference \"{ scope }\" is invalid."); } }
/// <summary> /// Sets a named value that is available to be resolved in the Lava Template. /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="scope"></param> public override void SetMergeField(string key, object value, LavaContextRelativeScopeSpecifier scope = LavaContextRelativeScopeSpecifier.Current) { int scopeIndex; // DotLiquid Scopes are ordered with the current level first. if (scope == LavaContextRelativeScopeSpecifier.Root) { scopeIndex = _context.Scopes.Count - 1; } else if (scope == LavaContextRelativeScopeSpecifier.Parent && _context.Scopes.Count > 1) { scopeIndex = 1; } else { scopeIndex = 0; } var fieldValue = GetDotLiquidCompatibleValue(value); // Set the variable in the specified scope. _context.Scopes[scopeIndex][key] = fieldValue; }
/// <summary> /// Sets a named value that is available to be resolved in the Lava Template. /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="scope"></param> public abstract void SetMergeField(string key, object value, LavaContextRelativeScopeSpecifier scope = LavaContextRelativeScopeSpecifier.Current);
/// <summary> /// Set a merge field value within the specified scope. /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="scopeReference">root|parent|current</param> private void SetFieldPrivate(string key, object value, bool allowInternalFieldAccess, LavaContextRelativeScopeSpecifier scope = LavaContextRelativeScopeSpecifier.Current) { if (!allowInternalFieldAccess && key.StartsWith(_InternalFieldKeyPrefix)) { throw new Exception("SetFieldValue failed. Invalid key."); } var localScope = _contextScopeInternalField.GetValue(_context) as Scope; if (scope == LavaContextRelativeScopeSpecifier.Current) { _context.SetValue(key, value); } else if (scope == LavaContextRelativeScopeSpecifier.Parent) { var parentScope = localScope.Parent ?? localScope; parentScope.SetValue(key, FluidValue.Create(value, _context.Options)); } else if (scope == LavaContextRelativeScopeSpecifier.Root) { var parentScope = _contextScopeInternalField.GetValue(_context) as Scope; var outerScope = parentScope.Parent; while (outerScope != null) { parentScope = outerScope; outerScope = outerScope.Parent; } parentScope.SetValue(key, FluidValue.Create(value, _context.Options)); } else { throw new LavaException($"SetMergeFieldValue failed. Scope reference \"{ scope }\" is invalid."); } }
/// <summary> /// Set a merge field value within the specified scope. /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="scopeReference">root|parent|current</param> public override void SetMergeField(string key, object value, LavaContextRelativeScopeSpecifier scope = LavaContextRelativeScopeSpecifier.Current) { SetFieldPrivate(key, value, allowInternalFieldAccess: false, scope); }
public override void SetMergeField(string key, object value, LavaContextRelativeScopeSpecifier scope = LavaContextRelativeScopeSpecifier.Current) { _mergeFields.SetValue(key, value); }