Example #1
0
        /// <summary>
        /// Compiles the specified args.
        /// </summary>
        /// <param name="args">Arguments, should contain "name, value, name, value" etc.</param>
        /// <param name="template">c# code that will be included in the generated template class</param>
        /// <param name="templateId">Id of the template class</param>
        /// <returns>Tiny template if successful; otherwise null.</returns>
        /// <exception cref="CompilerException">If compilation fails</exception>
        /// <exception cref="ArgumentException">If args are incorrect</exception>
        public ITinyTemplate Compile(TemplateArguments args, string template, string templateId)
        {
            ArgumentContainer[] arguments = args.GetArguments();
            foreach (ArgumentContainer arg in arguments)
                _compiler.Add(arg.Type);

            string members = string.Empty;
            string body = string.Empty;
            foreach (ArgumentContainer arg in arguments)
            {
                members += Compiler.GetTypeName(arg.Type) + " " + arg.Name + ";" + Environment.NewLine;
                body += "this." + arg.Name + " = (" + Compiler.GetTypeName(arg.Type) + ")args[\"" + arg.Name + "\"].Object;" + Environment.NewLine;
            }

            body += template;

            _generatedTemplate =
                TemplateBase.Replace("{id}", templateId).Replace("{members}", members).Replace("{body}", body);

            _compiler.Compile(_generatedTemplate);
            return _compiler.CreateInstance<ITinyTemplate>();
        }
Example #2
0
        /// <summary>
        /// A function that merges two argument holders updating and adding values
        /// </summary>
        /// <param name="arguments"></param>
        /// <exception cref="ArgumentNullException">If arguments is null</exception>
        public void Update(TemplateArguments arguments)
        {
            if (arguments == null)
                throw new ArgumentNullException("arguments");

            foreach (ArgumentContainer argument in arguments.GetArguments())
            {
                if (_arguments.ContainsKey(argument.Name))
                    _arguments[argument.Name] = argument;
                else
                    _arguments.Add(argument.Name, argument);
            }
        }