Exemplo n.º 1
0
        private static void InitializeLavaShortcodes(ILavaEngine engine)
        {
            // Register shortcodes defined in the codebase.
            try
            {
                var shortcodeTypes = Rock.Reflection.FindTypes(typeof(ILavaShortcode)).Select(a => a.Value).ToList();

                foreach (var shortcodeType in shortcodeTypes)
                {
                    // Create an instance of the shortcode to get the registration name.
                    var instance = Activator.CreateInstance(shortcodeType) as ILavaShortcode;

                    var name = instance.SourceElementName;

                    if (string.IsNullOrWhiteSpace(name))
                    {
                        name = shortcodeType.Name;
                    }

                    // Register the shortcode with a factory method to create a new instance of the shortcode from the System.Type defined in the codebase.
                    engine.RegisterShortcode(name, (shortcodeName) =>
                    {
                        var shortcode = Activator.CreateInstance(shortcodeType) as ILavaShortcode;

                        return(shortcode);
                    });
                }
            }
            catch (Exception ex)
            {
                ExceptionLogService.LogException(ex, null);
            }

            // Register shortcodes defined in the current database.
            var shortCodes = LavaShortcodeCache.All();

            foreach (var shortcode in shortCodes)
            {
                // Register the shortcode with the current Lava Engine.
                // The provider is responsible for retrieving the shortcode definition from the data store and managing the web-based shortcode cache.
                WebsiteLavaShortcodeProvider.RegisterShortcode(engine, shortcode.TagName);
            }
        }
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            LavaShortcode lavaShortcode;
            var           rockContext          = new RockContext();
            var           lavaShortCodeService = new LavaShortcodeService(rockContext);

            int lavaShortCode = hfLavaShortcodeId.ValueAsInt();

            if (lavaShortCodeService.Queryable().Any(a => a.TagName == tbTagName.Text && a.Id != lavaShortCode))
            {
                Page.ModelState.AddModelError("DuplicateTag", "Tag with the same name is already in use.");
                return;
            }

            if (lavaShortCode == 0)
            {
                lavaShortcode = new LavaShortcode();
                lavaShortCodeService.Add(lavaShortcode);
            }
            else
            {
                lavaShortcode = lavaShortCodeService.Get(lavaShortCode);
            }

            var oldTagName = hfOriginalTagName.Value;

            lavaShortcode.Name                = tbLavaShortcodeName.Text;
            lavaShortcode.IsActive            = cbIsActive.Checked;
            lavaShortcode.Description         = tbDescription.Text;
            lavaShortcode.Documentation       = htmlDocumentation.Text;
            lavaShortcode.TagType             = rblTagType.SelectedValueAsEnum <TagType>();
            lavaShortcode.TagName             = tbTagName.Text.Trim();
            lavaShortcode.Markup              = ceMarkup.Text;
            lavaShortcode.Parameters          = kvlParameters.Value;
            lavaShortcode.EnabledLavaCommands = String.Join(",", lcpLavaCommands.SelectedLavaCommands);

            rockContext.SaveChanges();

            if (LavaService.RockLiquidIsEnabled)
            {
                // unregister shortcode
                if (oldTagName.IsNotNullOrWhiteSpace())
                {
                    Template.UnregisterShortcode(oldTagName);
                }

                // Register the new shortcode definition. Note that RockLiquid shortcode tags are case-sensitive.
                if (lavaShortcode.TagType == TagType.Block)
                {
                    Template.RegisterShortcode <Rock.Lava.Shortcodes.DynamicShortcodeBlock>(lavaShortcode.TagName);
                }
                else
                {
                    Template.RegisterShortcode <Rock.Lava.Shortcodes.DynamicShortcodeInline>(lavaShortcode.TagName);
                }

                // (bug fix) Now we have to clear the entire LavaTemplateCache because it's possible that some other
                // usage of this shortcode is cached with a key we can't predict.
#pragma warning disable CS0618 // Type or member is obsolete
                // This obsolete code can be deleted when support for the DotLiquid Lava implementation is removed.
                LavaTemplateCache.Clear();
#pragma warning restore CS0618 // Type or member is obsolete
            }

            if (oldTagName.IsNotNullOrWhiteSpace())
            {
                LavaService.DeregisterShortcode(oldTagName);
            }

            // Register the new shortcode definition.
            LavaService.RegisterShortcode(lavaShortcode.TagName, (shortcodeName) => WebsiteLavaShortcodeProvider.GetShortcodeDefinition(shortcodeName));

            LavaService.ClearTemplateCache();

            NavigateToParentPage();
        }