Developer forum

Forum » Development » Rendering Custom Tags on Page.OnOutput

Rendering Custom Tags on Page.OnOutput

Kevin O''Driscoll
Reply

Hi hope someone can give a little guidence.
I have a cs library creating assets for a DW 8.3. the Library.dll is being passed to the DW Application bin directly and all is cool.
I have an ItemType set at page level adding additional properties to each page created, extending the properties for the Page (I do similar for Paragraphs).
eg an audience type is used to configure page settings for a particular audience accessing the page (and relevent sub pages)
and an Item Type ItemList holds configuration data for each page, like a particular help desk phone number the audience should call,
the related Twitter feed script, the particular help desk emaill address for the audience type etc.

From here I want to create and set some custom Page (and Paragraph) Template Tags during the Page Output cycle
and render them in the HTML, all is good so far. I can render all the custom tags, and in a Loop all the "Item.Page.Attribute_List" tags...
    <!--@LoopStart(Item.Page.Attribute_List)--> 
        <li><!--@Item.Page.Attribute_List.Page_Phone_Number--></li>
        <li><!--@Item.Page.Attribute_List.Page_Advisor_Email--></li>
        <li><!--@Item.Page.Attribute_List.Page_Twitter_Feed--></li>
        <li><a href="<!--@Item.Page.Attribute_List.Page_FAQ_Page-->">FAQ page</a></li>
        <li><a href="<!--@Item.Page.Attribute_List.Page_Reviews_Page-->">Reviews Page</a></li>
    <!--@LoopEnd(Item.Page.Attribute_List)-->

When I come to get the "Item.Page.Attribute_List" in my Page.OnOutput, I need to get all these attributes and create seperate Template Tags
for each, since these are not available to the rendered page unless I Loop. ("Item.Page.Attribute_List" has only a Name field and a Value [3] corresponding the database Item ID)
It would be nice to be able to see a List<ItemEntry> within this object.

Do I have to go to the database to get these items? Or is there a better way?
Some sample code would be very welcome.

Here's mine:

 

DWPage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dynamicweb;
using Dynamicweb.Rendering;


namespace XXXWeb.Library.Methods
{
    [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.Standard.Page.OnOutput)]
    public class PageObserver : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            if(args == null)
            {
                return;
            }

            var item = args as Dynamicweb.Notifications.Standard.Page.OnOutputArgs;
            CheckTagCollection(item.template.Tags, item.template);
        }

        private void CheckTagCollection(TagCollection tagCollection, Template template)
        {
            if (tagCollection.ContainsTagName("Item.Page.Page_Audience_Type") || tagCollection.ContainsTagName("Item.Page.Attribute_List"))
            {
                var audienceTag = tagCollection.GetTagByName("Item.Page.Page_Audience_Type");

                var pageAttributesTag = tagCollection.GetTagByName("Item.Page.Attribute_List");

                // we create our new Page Tags from our ItemType Attribute List. 
                DisplayAttributeList(pageAttributesTag.Value, pageAttributesTag, template);
                //We get the audience type for the page from the Page Item Type properties
                DisplayAudienceType(audienceTag.Value.ToString(),template);
            }

        }

        private void DisplayAttributeList(string ID, Tag list, Template template)
        {
            // It seems we need to lookup our properties from the ItemType data
            string tagName = list.Name.ToString();
            TagCollection newTags = new TagCollection();

           //?????


           var a = Dynamicweb.Content.Items.Item.GetItemById("ItemType_PGL_Page_Attributes", ID);
           a.SerializeTo(?);


            template.Tags.Add(newTags);
        }


        private void DisplayAudienceType(string p, Template t)
        {
            Dynamicweb.Rendering.Tag audienceTypeTag = new Tag();
            audienceTypeTag.Name = "audienceTypeTag";
            audienceTypeTag.Value = p;
            t.Tags.Add(audienceTypeTag);
            
            // Also maybe tickle a CSS file link along the way perhaps
            Dynamicweb.Rendering.Tag cssTag = new Tag();
            cssTag.Name = "cssTag";
            cssTag.Value = p;
            t.Tags.Add(cssTag);
            
        }
   }
}


Replies

 
Mikkel Ricky
Reply

I'm not sure I understand what you're trying to do (I'll get back to that), but after the "var a = …" line, you can add something like

foreach (var e in a) {
  template.SetTag(e.Key, e.Value.ToString());
}

to get all item properties set as template tags.

And now back to me not understanding what you're aiming for:

Can you show an example of what template tags you want, e.g by showing an exceprt from the template using them? From reading your post, I think that you want @Page_Phone_Number, @Page_Advisor_Email, &c., but that does not make sense when you can define each multiple times (in the Attribute_List).

Maybe you can also share your item type definitions (the xml files in /Files/System/Items/) to enlighten me?

Best regards,
Mikkel

 
Kevin O''Driscoll
Reply

Hi Mikkel, hope you are well and getting some nice Chinese food. (And Beer!)

OK what I really want to do for the final solution (since this step was an interim babystep).

When a user creates a new PAGE she must define an Audience from a dropdown of Audience Types. This is working fine since these are defined hard coded in an ItemType_Page dropdown list and its (one) selected value can be passed to the page.

Next she must define the additional 5 properties for the page. Phone Number, Email etc.

She gets these also from a pick list on the page properties, so she dosnt have to add each 5 properties uniquely to the page. She picks one property set from the picklist. This allows efficient management (and minimal spelling errors) for all these properties, and the properties are set in one go.

The picklist itself is managed (somehow, dont know yet) in DW, so property SETS can be edited, added and removed, maybe by an admin at a higher level.

So, I can build a list of properties in one hit and output them to the page in a loop, the template tags as in my previous post.

However I want the Phone number at the top of the page and the email at the bottom of the page and other properties in <script> tags and <a> tags sprinkled liberally throughout the template. Like in Chinese food.

So I need to get each field from my ItemType_Page_Attributes ID = 3 and create a tag for each one the fly in DWPage.cs. Add them all to the template.

I could share the code but its a lot for a forum page, will mail you if your server dosnt bounce my .zip files

Best Regards

 

You must be logged in to post in the forum