Developer forum

Forum » Development » Accessing Paragraphs from a NotificationSubscriber

Accessing Paragraphs from a NotificationSubscriber


Reply
Hi there,

Is there a way to access a template tag in a paragraph template from a NotificationSubscriber? I am trying to assign a value to a template tag if it exists in one of the templates that is available in the page.

Ideally, I am looking for something like this:

foreach (Template template in PageView.Current().ParagrapghTemplates)
{
    template.SetTag("SomeKey", "SomeValue");
}

Can this be done somehow?

Imar

Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

There isn't really a way to do that. Whether or not it is possible depends on which Notification you subscribe to. The problem being that paragraph templates are rendered one at a time and in turn.
Let's say you're subscribing to an OnBeforeRender Notification for some module which is on the second paragraph on your page, then that Notification will not be fired until the first paragraph template was been rendered. And if the template tag you're looking for is on the first paragraph template, it's now too late to set the value.

If you know that your Notification will always be fired before the template you're looking for, then you might be able to use the following code as a starting point:

var pageView = Dynamicweb.Frontend.PageView.Current();foreach (Dynamicweb.ParagraphTemplateJoinClass PTRecord in pageView.Content.PTCollection)// Create a Template object from the current Template file

 

 

template.Template(

var template = new Dynamicweb.Template();var templateFile = "Paragraph/" + PTRecord.Template.TemplateFile;ref templateFile); // Yes, passing a string as reference is actually necessary!

 

// Check for the existance of the template tag

 

{

 

if (template.get_TagExist("MyTemplateTag"))// Do something here

}

}

Otherwise you might want to hook into the "Standard.Paragraph.OnBeforeRender".

Dynamicweb.Notifications.

This notification is fired each time a paragraph template is rendered and the OnBeforeRenderArgs object gives you access to the template object.

Please note that all template object mentioned here are Dynamicweb.Template objects and not Dynamicweb.Templatev2.Template objects.

 

 

{

 

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
Sorry for the varying font sizes in my previous post :)
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
I see some of the code that I pasted was cut away. Here's another go :)

 

 

{

 

var pageView = Dynamicweb.Frontend.PageView.Current();foreach (Dynamicweb.ParagraphTemplateJoinClass PTRecord in pageView.Content.PTCollection)// Create a Template object from the current Template file

 

 

template.Template(

var template = new Dynamicweb.Template();var templateFile = "Paragraph/" + PTRecord.Template.TemplateFile;ref templateFile); // Yes, passing a string as reference is actually necessary!

 

// Check for the existance of the template tag

 

{

 

if (template.get_TagExist("MyTemplateTag"))// Do something here

}

}

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Ok, that didn't work either. One more try. Without syntax highlighting I'm afraid.

var pageView = Dynamicweb.Frontend.PageView.Current();
foreach (Dynamicweb.ParagraphTemplateJoinClass PTRecord in pageView.Content.PTCollection)
{
 // Create a Template object from the current Template file
 var template = new Dynamicweb.Template();
 var templateFile = "Paragraph/" + PTRecord.Template.TemplateFile;
 template.Template(ref templateFile); // Yes, passing a string as reference is actually necessary!

 // Check for the existance of the template tag
 if (template.get_TagExist("MyTemplateTag"))
 {
  // Do something here
 }
}
 

 
Reply
Hi Jeppe,

Thanks for that. Vey useful. Will try that next.

And yes, I was aware of the new Standard.Paragraph.OnBeforeRender notification. However, it fires for each and every paragraph and doesn't give me the state information I have available in some of the other notification subscribers.

Kind regards,

Imar

 

You must be logged in to post in the forum