Example #1
0
 public string Evaluate(Template template, Dictionary<string, object> variables)
 {
     using (var scope = new TemplateScope(this, template, variables))
     {
         EvaluateInternal(scope, null, null);
         return scope.GetContentsOfSection(TemplateScope.DefaultSection);
     }
 }
Example #2
0
        public void TestTrueFalseNoneAreImmutableConstants()
        {
            var Scope = new TemplateScope(new Dictionary <String, object>()
            {
                { "true", false },
                { "false", true },
                { "none", 1 },
            });

            Assert.AreEqual("1", TemplateCodeGen.CompileTemplateCodeByString("{% if true %}1{% else %}0{% endif %}").RenderToString(Scope));
            Assert.AreEqual("0", TemplateCodeGen.CompileTemplateCodeByString("{% if false %}1{% else %}0{% endif %}").RenderToString(Scope));
            Assert.AreEqual("0", TemplateCodeGen.CompileTemplateCodeByString("{% if none %}1{% else %}0{% endif %}").RenderToString(Scope));
        }
Example #3
0
        public void TestExecForElse()
        {
            var Params1 = new TemplateScope(new Dictionary <String, object>()
            {
                { "List", new int[] { 1, 2, 3, 4, 5 } }
            });
            var Params2 = new TemplateScope(new Dictionary <String, object>()
            {
                { "List", new int[] { } }
            });

            Assert.AreEqual("12345", TemplateCodeGen.CompileTemplateCodeByString("{% for Item in List %}{{ Item }}{% else %}Empty{% endfor %}").RenderToString(Params1));
            Assert.AreEqual("Empty", TemplateCodeGen.CompileTemplateCodeByString("{% for Item in List %}{{ Item }}{% else %}Empty{% endfor %}").RenderToString(Params2));
        }
Example #4
0
        public void InitializeContextArray(string slotName, TemplateScope templateScope, int size)
        {
            referencedContexts = new UIElement[size + 1];

            if (templateScope.slotInputs != null)
            {
                int idx = 1;
                for (int i = templateScope.slotInputs.size - 1; i >= 0; i--)
                {
                    if (templateScope.slotInputs.array[i].slotName == slotName)
                    {
                        referencedContexts[idx++] = templateScope.slotInputs.array[i].context;
                    }
                }
            }

            referencedContexts[0] = templateScope.innerSlotContext;
        }
Example #5
0
        public void Evaluate(TemplateScope scope)
        {
            var executable = scope.Template.Executable;
            ScriptSource source;

            if (debug)
                // have to pass in this bogus file name so that IronRuby will give us line numbers
                // in the backtrace when exceptions occur at runtime. stupid!
                source = rubyEngine.CreateScriptSourceFromString(executable, "omg_rly.rb", SourceCodeKind.File);
            else
                source = rubyEngine.CreateScriptSourceFromString(executable, SourceCodeKind.Statements);

            // for some reason the engine keeps track of these instances (wtf)...
            ScriptScope scriptScope = runtime.CreateScope();
            // ...so we have to unset this later.
            scriptScope.SetVariable("scope", scope);

            if (scope.Variables != null)
                foreach (var pair in scope.Variables)
                    scriptScope.SetVariable(pair.Key, pair.Value);

            try
            {
                // latest DLR/IronRuby breaks the executing of compiled code somehow.
                //if (!(scope.Template.CompiledExecutable is CompiledCode))
                //    scope.Template.CompiledExecutable = source.Compile();
                //((CompiledCode)scope.Template.CompiledExecutable).Execute(scriptScope);

                source.Execute(scriptScope);

            }
            catch (Exception e)
            {
                // need to worry about ThreadAbortException?
                FlatironExceptionData.AssociateInstance(e, scope.Template, GetExecutableLine(e));
                throw;
            }
            finally
            {
                scriptScope.RemoveVariable("scope");
            }
        }
        public ProvisioningTemplateInformation[] GetLocalProvisioningTemplates(string siteUrl, TemplateScope scope)
        {
            List <ProvisioningTemplateInformation> result =
                new List <ProvisioningTemplateInformation>();

            // Retrieve the Root Site Collection URL
            siteUrl = PnPPartnerPackUtilities.GetSiteCollectionRootUrl(siteUrl);

            // Connect to the Infrastructural Site Collection
            using (var context = PnPPartnerPackContextProvider.GetAppOnlyClientContext(siteUrl))
            {
                // Get a reference to the target library
                Web web = context.Web;

                try
                {
                    List list = web.Lists.GetByTitle(PnPPartnerPackConstants.PnPProvisioningTemplates);

                    // Get only Provisioning Templates documents with the specified Scope
                    CamlQuery query = new CamlQuery();
                    query.ViewXml =
                        @"<View>
                        <Query>
                            <Where>
                                <And>
                                    <Eq>
                                        <FieldRef Name='PnPProvisioningTemplateScope' />
                                        <Value Type='Choice'>" + scope.ToString() + @"</Value>
                                    </Eq>
                                    <Eq>
                                        <FieldRef Name='ContentType' />
                                        <Value Type=''Computed''>PnPProvisioningTemplate</Value>
                                    </Eq>
                                </And>
                            </Where>
                        </Query>
                        <ViewFields>
                            <FieldRef Name='Title' />
                            <FieldRef Name='PnPProvisioningTemplateScope' />
                            <FieldRef Name='PnPProvisioningTemplateSourceUrl' />
                        </ViewFields>
                    </View>";

                    ListItemCollection items = list.GetItems(query);
                    context.Load(items,
                                 includes => includes.Include(i => i.File,
                                                              i => i[PnPPartnerPackConstants.PnPProvisioningTemplateScope],
                                                              i => i[PnPPartnerPackConstants.PnPProvisioningTemplateSourceUrl]));
                    context.ExecuteQueryRetry();

                    web.EnsureProperty(w => w.Url);

                    foreach (ListItem item in items)
                    {
                        // Configure the XML file system provider
                        XMLTemplateProvider provider =
                            new XMLSharePointTemplateProvider(context, web.Url,
                                                              PnPPartnerPackConstants.PnPProvisioningTemplates);

                        item.File.EnsureProperties(f => f.Name, f => f.ServerRelativeUrl);

                        ProvisioningTemplate template = provider.GetTemplate(item.File.Name);

                        result.Add(new ProvisioningTemplateInformation
                        {
                            Scope             = (TemplateScope)Enum.Parse(typeof(TemplateScope), (String)item[PnPPartnerPackConstants.PnPProvisioningTemplateScope], true),
                            TemplateSourceUrl = item[PnPPartnerPackConstants.PnPProvisioningTemplateSourceUrl] != null ? ((FieldUrlValue)item[PnPPartnerPackConstants.PnPProvisioningTemplateSourceUrl]).Url : null,
                            TemplateFileUri   = String.Format("{0}/{1}/{2}", web.Url, PnPPartnerPackConstants.PnPProvisioningTemplates, item.File.Name),
                            TemplateImageUrl  = template.ImagePreviewUrl,
                            DisplayName       = template.DisplayName,
                            Description       = template.Description,
                        });
                    }
                }
                catch (ServerException ex)
                {
                    // In case of any issue, ignore the local templates
                }
            }

            return(result.ToArray());
        }
 public ProvisioningTemplateInformation[] GetGlobalProvisioningTemplates(TemplateScope scope)
 {
     return(GetLocalProvisioningTemplates(PnPPartnerPackSettings.InfrastructureSiteUrl, scope));
 }
Example #8
0
 /// <summary>
 /// Finds template descriptions that match the given scope.
 /// </summary>
 /// <returns>A sequence containing all registered template descriptions that match the given scope.</returns>
 public static IEnumerable <TemplateDescription> FindTemplates(TemplateScope scope, PackageSession session = null)
 {
     return(FindTemplates(session).Where(x => x.Scope == scope));
 }
Example #9
0
 /// <summary>
 /// Finds template descriptions that match the given scope.
 /// </summary>
 /// <returns>A sequence containing all registered template descriptions that match the given scope.</returns>
 public static IEnumerable <TemplateDescription> FindTemplates(TemplateScope scope)
 {
     return(FindTemplates().Where(x => x.Scope == scope));
 }
Example #10
0
        internal void EvaluateInternal(TemplateScope scope, TemplateScope includer, TemplateScope child)
        {
            Template template = scope.Template;

            if (template.NeedsParsing)
            {
                Console.WriteLine("reparsing " + template);
                template.Parse(support.CreateCommandWriter());
            }
            else Console.WriteLine("not reparsing " + template);

            // if we were included, we want to write to the includer's output.
            if (includer != null)
                scope.SetSectionWriter(TemplateScope.DefaultSection, includer.Output);

            support.Evaluate(scope);

            if (scope.Parent != null)
            {
                scope.Parent.Child = scope;
                EvaluateInternal(scope.Parent, null, scope);
                // set our output to whatever the parent's output was, since the parent is 'wrapping' us.
                scope.SetSectionWriter(TemplateScope.DefaultSection, scope.Parent.GetSectionWriter(TemplateScope.DefaultSection));
                scope.Parent.Child = null;
            }
        }
Example #11
0
 /// <summary>
 /// Override to turn a string into a Template instance. Requester is non-null if we are resolving
 /// from a call to TemplateScope.SetParentTemplate/Include.
 /// Default implementation instantiates a new Template with a backing file relative to the requesting 
 /// Template's backing file, or relative to Root if requester is null.
 /// </summary>
 public virtual Template ResolveTemplate(string template, TemplateScope requester)
 {
     string relativeTo = requester == null ? TemplateRoot : Path.GetDirectoryName(requester.Template.BackingFile);
     return GetTemplate(Path.Combine(relativeTo, template));
 }
Example #12
0
        public static LinqBindingNode GetSlotNode(Application application, UIElement rootElement, UIElement element, UIElement innerContext, int createdId, int enabledId, int updatedId, int lateId, string slotName, TemplateScope templateScope, int slotContextSize)
        {
            LinqBindingNode node = new LinqBindingNode(); // todo -- pool

            node.root           = rootElement;
            node.element        = element;
            node.innerContext   = innerContext;
            element.bindingNode = node;

            // todo -- profile this against skip tree
            UIElement ptr = element.parent;

            while (ptr != null)
            {
                if (ptr.bindingNode != null)
                {
                    node.parent = ptr.bindingNode;
                    break;
                }

                ptr = ptr.parent;
            }

            node.InitializeContextArray(slotName, templateScope, slotContextSize);

            node.SetBindings(application, rootElement, createdId, enabledId, updatedId, lateId);

            return(node);
        }