Developer forum

Forum » Templates » Declaring/using variable across paragraph templates

Declaring/using variable across paragraph templates

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

I have started posts about this before, but this is maybe a little bit different.

I want to declare a variable in paragraphTemplateA.cshtml, and then I want to use/show it in paragraphTemplateB.cshtml

I was thinking i could use ViewBag for this, but it doesn´t work.

 

So far I have this in ParagraphTemplateA.cshtml


@{
 
ViewBag.DeildType=GetString("Item.Heiti");
 
}

 

And try to use it like this in ParagraphTemplateB.cshtml

 

<div class="col-xl-4 col-12 folk starvsf @ViewBag.DeildType">

 

But it doesn´t work.

 

I have attached both paragraph templates

NB: They are both rendered on the same page

What am I doing wrong?

 

/Hans

 


Replies

 
Nicolai Pedersen
Reply

Each template has its own rendering instance and hence does not share viewbag - you can use the viewbag in include, masters etc.

But simply use httpcontext.items:

HttpContext.Current.Items["Test"] = "Hello World";

And

Convert.ToString(HttpContext.Current.Items["Test"])
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Perfect Nicolai 👍🏻

Works like a charm thank you :)

/Hans

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

@Nicolai, can I also use this to pass a variable from a pagetemplate to a paragraphtemplate?

 

/Hans

 
Nicolai Pedersen
Reply

Yes - this works across the entire request.

BR Nicolai

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

I Tried it. It works perfectly accross paragraph templates, but not from a page template to a paragraph template.

I have quadruple checked that there is no misspelling, and I have copied the example that works across paragraph templates to the pagetemplate, but no luck :/

Any suggestions?

 

/Hans

 

 
Nicolai Pedersen
Reply

It depends on what is executed first. A page template renders paragraphs that renders modules. Therefore if a paragraph has a module, that is rendered before the paragraph. Then the next paragraph etc. In the end, when all paragraphs and their modules have been rendered, then the layout is rendered with the result of the paragraphs.

So if you set a variable in the layout template, it cannot be accessed in the paragraph template since it has already been rendered and the code executed.

But if you have information in your layout template that you want in your paragraph template, I am pretty sure you can do the exact same thing in the paragraph template.

Tell us what your want to do - we will tell you how.

Thanks, Nicolai

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Ok, thank you

that sounds like an explanation. I want to declare the variable in the page template, and then render it in the paragraph template. According to you explanation, this is not possible as the paragraphs are rendered before the page, correct?

Is there any other way of passing from the page template to the paragraph template?

/Hans

 
Nicolai Pedersen
Reply

Well, just declare it in the paragraph template instead and use it in the layout template in the end... Much easier.

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

No, that won´t do the trick. I want to utilize this to change the behaviour of the pargraph based on the pagetemplate. Not the other way around.

/Hans

 
Nicolai Pedersen
Reply

Yes, I understand that. But that is not possible, so you need to take another approach.

Show me the code you have in your layout template.

BR Nicolai

 

You must be logged in to post in the forum