Developer forum

Forum » Templates » Get Field names and values from current Page Item on a paragraph template?

Get Field names and values from current Page Item on a paragraph template?

Jacob Storgaard Jensen
Reply

Hi guys,

A small question here from a Razor idiot with a limited programming vocabulary... so bare with my childish explanations :-D.

I've got approx 30 item fields on a Page Item, 20 of those fields I would like to loop through BUT on a paragraph (which is inserted on the Page) and list them in a table (doesn't matter what markup, just for explanation purposes). The SystemName of the 20 fields begin with "Spec_" . So I would like the Name and Value of those fields.

Right now I can only figure out how get the value of each single field by supplying the SystemName of each field, which forces me to have to update the templates if more fields are added in the future + I have to supply the field name in template...
string width = Dynamicweb.Frontend.PageView.Current().Page.Item["Spec_Width"].ToString();

Any help is highly appreciated!

 


Replies

 
Jacob Storgaard Jensen
Reply

Well, I did it the dirty way...
A snippet on my Layout template:

@{
    if(GetString("Item.ItemInstanceType") == "Product"){
        @SnippetStart("specs")
            foreach (LoopItem field in GetLoop("Item.Fields")) {
                
                var sysName = field.GetString("Item.Field.SystemName");
                string fieldName = field.GetString("Item.Field.Name");
                string fieldValue = field.GetString("Item.Field.Value");
                if(sysName.Contains("Spec_") && !string.IsNullOrEmpty(fieldValue)){
                    <div>@Translate(sysName, fieldName), @fieldValue <br/></div> //This is just test markup!!
                }
                
            }
        @SnippetEnd("specs")

        if(1==2){
            @RenderSnippet("specs")
        }
    }
}

And in my paragraph template:

@RenderSnippet("specs")

 

You must be logged in to post in the forum