Developer forum

Forum » CMS - Standard features » Default value for Snippet

Default value for Snippet

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I have a situation where I am using a Snippet to write some data in the header.

More than just writing the content of the snippet, I need to have a default value for the snippet in case the snippet is not defined or it is empty.

To be more explicit, I need to write some data from ProductPage and another set of data form any other page.

Is there any way I can accomplish this scenario?

I have used this approach because I don't seem to be able to load ProductDetail information from the MasterPage. If there is any way I can access the ProductDetail data from Masterpage, I would prefer that option.

Thank you,

Adrian 

 


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Adrian,

 

You should be able to do an if statement with a snippet tag. Something like:

 

if (string.IsNullOrEmpty(RenderSnippet("MySnippet")) {

    // My default value/code

}

else {

    @RenderSnippet("MySnippet")

}

 

Best Regards,

Nuno Aguiar

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nuno,

I tried that already.

Apparently the check is true even if the Snippet is empty. The template renders something like this: <!--Snippet(MySnippet)-->

Adrian

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Adrian,

 

Then you can also check if .StartsWith("<!--Snippet")  smiley

 
Nicolai Pedersen
Reply

Do it in the snippets instead. This is not good for performance doing stuff like this.

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

I would do it in the Snippet but the Snippet is now defined in one of the ProductDetail templates.

If I would do it in the Snippet, I would have to move the Snippet in the Page template (which I agree it would be ideal) but then I would need to be able to access ProductDetails in the Page template. And that's something I was not able to do. If you have a suggestion on how to do that, I will happily make all the changes.

Thank you,

Adrian

 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

I'm facing the same issue.

It seems the snippet is not completely empty. If I check the length of a snippet like: RenderSnippet("BreadCrumbTitle").ToString().Length, it outputs 32, even though the snippet contains "string.empty".

--

Best regards
Kurt Moskjaer Andersen

 
Nicolai Pedersen
Reply

The snippet is a text file - and the length is probably spaces etc.

 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

I'm sorry, but it doesn't seem like it.

If I use the following regular expression, the length of "replacement" is still 32 chars long:

string replacement = Regex.Replace(RenderSnippet("BreadCrumbTitle").ToString().Trim(), @"\t|\n|\r", String.Empty)

Edit: Where is the file for the snippet located?

/Kurt

 

You must be logged in to post in the forum