morenoCami
New Creator

How to pass values between templates/assets

Here's my scenario:

I have a child template where I use the Out.Wrap Template API call for the master page portion (header, footer, etc).

I want to exclude certain parts of the master page template based on a value that is found in the child template.

How can I pass the value to the master? Or how can I reach the value from the master? Other suggestions and approaches are also welcome.

I was thinking I could use the context object to hold this value, as shown below. It hasn't worked so far:
***storing the value in child template

string other;
if (asset.GetContent().TryGetValue("remove_right_nav_buttons", out other)) { 
    context.UserVariables["remove_right_nav_buttons"] = other;
}

***accessing the value in master template

StringBuilder sbContent = new StringBuilder();
if (context.UserVariables["remove_right_nav_buttons"] == "true") {
     string componentMarkupNoRightButtons = @"html";
     sbContent.Append(componentMarkupNoRightButtons);
}
else
     sbContent.Append(componentMarkup);
2 Replies
BeverlyDunn
Crownpeak (Retired)

Thank you, @morenoCami , for posting a question in the Crownpeak Community.  I am a member of the Customer Success team and would like to help answer this question with the help of my friend, @DavidGreenberg, from Crownpeak Support.

After some back and forth, our recommendation is this:

  • Out.wrap() can be used to call a template, which is then run with the current asset as the value asset.
  • If you add this <%= Out.GetWrapContentPlaceholder() %> in the template, that puts the output of the current asset's template there.

We would love for you to try this and let us know if this solves your question.  If so, appreciate if you would mark this as solved so others looking for the same answer can easily find it. 

Good luck!

@BeverlyDunn and @DavidGreenberg 

 

Beverly Dunn
Sr. Director - Customer Success

If you enjoyed this content, please give me a thumbs up or mark as a solution!
0 Kudos
MarcusEdwards
Crownpeak (Retired)

You do not need to do anything if you are using Out.Wrap. The parameter that you pass to the method is a template rather than an asset so the asset does not change in the output context. You can verify this yourself by outputing asset.Id in both the child and the navigation wrapper templates and you'll see that the output is the same.

However, if you are using Asset.Show to get the rendered output of another asset, then you will need to use the mechanism you outlined (UserVariables).

This example code code demonstrates:

Navigation Wrapper template:

    <div class="tester" style="border: 1px solid silver; width; 100%">
      <p>Asset ID: <%= asset.Id %></p>
      <p>var1: <%= context.UserVariables["var1"] %></p>
      <p>var2: <%= context.UserVariables["var2"] %></p>
    </div>

Child template:

<%
context.UserVariables["var1"] = "set before call";
Out.Wrap("/path/navigation-wrapper");
context.UserVariables["var2"] = "set after call";
%>