2

I have around 50 lines of code in multiple page templates.
I am not able move it to a component template as I need component and template ID from page and render some text from the components in foreach loop.
I can not use context variable as the component template is DCP.
I am planning to move all of these 50 lines to either Razor TBB function or to a C# TBB. I am still POCing as how will I do that but I am confident that it should be doable.
My questions are-
1. Is there a better way that I am not seeing?
2. If I go with TBB option which will give me better performance: Razor TBB or the C# TBB? We have razor implementation for the page tempalate.
Below is the code snippet I have in page template-

@if(Package.FAQTopicCountFromPage == "1"){
                            @foreach (var cp in GetComponentPresentationsByTemplate("FAQ Topic")) {
                               @cp.RenderComponentPresentation()
                            }
                        }
                        @if(Package.FAQTopicCountFromPage != "1" && Package.FAQTopicCountFromPage != "0"){
                            <div id="id" class="classes"> 
                                <div>
                                    <h2 id="id" class="classes">
                                        @RenderComponentPresentation(Package.componetID, Package.templateID)
                                    </h2>
                                    <a href="javascript:void(0);" class="class" style="display: inline;">
                                        @RenderComponentPresentation(Package.componetID, Package.templateID)
                                    </a>
                                    <a href="javascript:void(0);" class="class" style="display: none;">
                                        @RenderComponentPresentation(Package.componetID, Package.templateID)
                                    </a>
                                </div>
                                <div class="">
                                    <div id="tabContainerID">
                                        <ul class="class">
                                            @foreach (var cp in GetComponentPresentationsByTemplate("FAQ Topic")) {
                                                @if(cp.Index==0){
                                                    <li><a href="#tabs-1">Topic 1 Title (This is a render component presentation))</a></li>}
                                                @if(cp.Index!=0){
                                                    <li><a href="#tabs-2">Topic 2 Title (This is a render component presentation)</a></li>}
                                            }
                                        </ul>
                                    </div>
                                    <div class="mainContainer">
                                        @foreach (var cp in GetComponentPresentationsByTemplate("FAQ Topic")) {
                                            @if(cp.Index==0){
                                                 <div id="tabs-1" class="">
                                                    Question Answers ul/li goes here for tab-1 (This is a render component presentation)
                                                </div>
                                            }
                                            @if(cp.Index!=0){
                                                <div id="tabs-2" class="">
                                                    Question Answers ul/li goes here for tab-2 (This is a render component presentation)
                                            </div>
                                        }
                                        }
                                    </div>

                                </div>
                            </div>

                            }   

2 Answers 2

3

The advantage of having it in Razor or DWT is readability of HTML. If you dump it all into a dirty C# TBB, you'll be coding all kinds of escaped strings with HTML - PITA to read/maintain/update IMO. Performance difference will probably be negligible (unless you're publishing hundreds of these components) until you move to DXA that quickly serializes fields into JSON.

My advice, don't spend your time studying how to optimize old obsolete technology, like DWT or Razor mediators and the old way of using context variables. Invest this time to convert to DXA.

2
  • Thank you Nick. I am also planning to go with Razor as we already have this type of XPM code in Razor. We do have 100 of the Topic components but they rarely change so performance should be fine. I am not sure about DXA at this moment. Commented Jul 19, 2018 at 4:49
  • I will mark the answer once I complete a POC and see the results. Commented Jul 20, 2018 at 3:17
0

Well, it's a balance between maintainability and performance, isn't it? In theory, Razor templates may be easier to maintain long term, but this all depends on your (team's) knowledge and preferences.

I would expect a c# TBB to be faster simply due to having less overhead. I guess an easy-ish way to measure it is to use Template Builder and check the measurements there (even if you can't really trust those values 100% as execution in context of Template Builder is different from Publisher) but should give a proper starting point.

2
  • Thank you Nuno for your inputs. Any other way I could achieve this? Is there a way to move this to component template? Commented Jul 18, 2018 at 13:29
  • Sorry, not sure I understand what you mean with "move this to component template". Your template building blocks run in Page or Component Templates depending on what it is they're rendering (pages or individual components). So the answer is "yes, probably, but sometimes that might not be what you want."
    – Nuno Linhares
    Commented Jul 20, 2018 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.