Example #1
0
        /// <summary>
        /// Initializes the class with all the arguments of the parameter class.
        /// </summary>
        /// <param name="arguments">Cannot be null</param>
        /// <exception cref="ArgumentNullException">If arguments is null</exception>
        public TemplateArguments(TemplateArguments arguments)
        {
            if (arguments == null)
                throw new ArgumentNullException("arguments");

            _arguments = new Dictionary<string, ArgumentContainer>(arguments._arguments);
        }
Example #2
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 #3
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);
            }
        }
Example #4
0
        /// <summary>
        /// Compiles the specified code.
        /// </summary>
        /// <param name="fileName">Name of template.</param>
        /// <param name="code">c# code generated from a template.</param>
        /// <param name="arguments">Arguments as in name, value, name, value, name, value</param>
        /// <param name="templateId">
        /// An id to specify the exact instance of a template. Made from joining the 'TemplateClass' with the hashcode of the filename
        /// and the hash code of the supplied arguments
        /// </param>
        /// <returns>Template</returns>
        /// <exception cref="TemplateException">If compilation fails</exception>
        /// <exception cref="ArgumentException">Code is not specified.</exception>
        protected ITinyTemplate Compile(string fileName, string code, TemplateArguments arguments, string templateId)
        {
            if (string.IsNullOrEmpty(code))
                throw new ArgumentException("Code is not specified.");

            var compiler = new TemplateCompiler();
            foreach (Type type in _includedTypes)
                compiler.Add(type);

            try
            {
                return compiler.Compile(arguments, code, templateId);
            }
            catch (CompilerException err)
            {
                throw new TemplateException(fileName, err);
            }
        }
Example #5
0
 /// <summary>
 /// Render a partial
 /// </summary>
 /// <param name="filename">Path and filename</param>
 /// <param name="templateArguments">Variables used in the template. Should be specified as "name, value, name, value" where name is variable name and value is variable contents.</param>
 /// <param name="partialArguments">Arguments passed from parent template</param>
 /// <returns></returns>
 /// <exception cref="InvalidOperationException"></exception>
 /// <exception cref="TemplateException"></exception>
 /// <exception cref="ArgumentException"></exception>
 public string RenderPartial(string filename, TemplateArguments templateArguments, TemplateArguments partialArguments)
 {
     templateArguments.Update(partialArguments);
     return Render(filename, templateArguments);
 }
Example #6
0
        /// <summary>
        /// Generate HTML from a template.
        /// </summary>
        /// <param name="filename">Path and filename</param>
        /// <param name="args">Variables used in the template. Should be specified as "name, value, name, value" where name is variable name and value is variable contents.</param>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="CompilerException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <example>
        /// <code>
        /// string html = cache.Generate("views\\users\\view.haml", new TemplateArguments("user", dbUser, "isAdmin", dbUser.IsAdmin), null);
        /// </code>
        /// </example>
        public string Render(string filename, TemplateArguments args)
        {
            if (args == null)
                args = new TemplateArguments();

            GetGeneratorForWildCard(ref filename);

            string templateName = "TemplateClass" + filename.GetHashCode() + args.GetHashCode();
            templateName = templateName.Replace('-', 'N');

            TemplateInfoImp info;
            lock (_compiledTemplates)
            {
                if (_compiledTemplates.ContainsKey(templateName))
                    info = _compiledTemplates[templateName];
                else
                {
                    info = new TemplateInfoImp
                    {
                        Filename = filename,
                        Template = null,
                        CompiledWhen = DateTime.MinValue
                    };
                    _compiledTemplates.Add(templateName, info);
                }
            }

            lock (info)
            {
                if (!CheckTemplate(info) || info.Template == null)
                {
                    string code = GenerateCode(ref filename);
                    info.Template = Compile(filename, code, args, templateName);
                    info.CompiledWhen = DateTime.Now;
                    info.Filename = filename;
                }

                return info.Template.Invoke(args, this);
            }
        }