Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7720383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:39:35+00:00 2026-06-01T03:39:35+00:00

I am trying to accomplish a module where i need to use nested templates

  • 0

I am trying to accomplish a module where i need to use nested templates and I am stuck at HOW can i do that.

basically there are 3 levels in my UI, for example say Level 1, Level 2, Level 3.
So when the page is displayed i need to render level 1 only.
But when user clicks on “expand” button of any element of level1 i need to render corresponding elements of Level 2 (not all) below the selected element of level 1.

Now when user clicks on “expand” of any element of Level 2, corresponding Level 3 should be rendered..

To summarize it should be just like Windows Explorer’s navigation bar on left.!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:39:36+00:00Added an answer on June 1, 2026 at 3:39 am

    Generally, you should define separate components for each level, assign template to each of your components and implement something like expand()/collapse() methods. If a component is initially collapsed (your case) then it shouldn’t need to render child items on initialization, it would render them only when you expand them (the appropriate templates of child components would be used).

    Please provide a basic code that you are trying to make work, it would be easier to help you that way.

    Here is a quick prototype of Widget system with a simple rendering flow that uses templates. I guess you want something like that in your application. It is unoptimized, it’s just an idea of how your framework might look.

    /**
     * Widget constructor
     */
    var Widget = function(config) {
        // apply config
        $.extend(this, config);
        // attempt to render
        this.render();
    };
    
    /**
     * Widget prototype
     */
    $.extend(Widget.prototype, {
        // render target
        renderTo: null,
        // template
        tpl: '<div class="container-panel">' +
                '<p>${txt}</p>' +
                '<div class="items-container"></div>' +
            '</div>',
        // template data
        tplData: null,
        // child items array
        children: null,
        // initial collapsed state
        collapsed: false,
        // widget's root element
        el: null,
        // default render target selector for child items
        renderTarget: '.items-container',
    
        render: function() {
            var me = this,
                renderDom
    
            // render the widget
            if(!this.rendered && this.renderTo && this.tpl) {
                renderDom = $.tmpl(this.tpl, this.tplData);
                // assume that first element is widget's root element
                this.el = renderDom[0];
                $(this.renderTo).append(renderDom);
    
                 // clear the reference
                renderDom = undefined;
    
                // THIS IS JUST EXAMPLE CODE! Bind click handler...
                $(this.el).find('p').first().click(function() {
                    me.collapsed ? me.expand() : me.collapse();
                });    
    
                // find render target for children
                this.renderTarget = $(this.el).find(this.renderTarget).first();
    
                // render children if not collapsed
                this.renderChildren();
    
                // set rendered flag
                this.rendered = true;
            }
        },
    
        renderChildren: function() {
            var children = this.children;
            if(!this.collapsed && children && children.length) {
                for(var i = 0, len = children.length; i < len; i++) {
                    // render children inside 
                    children[i].renderTo = this.renderTarget;
                    children[i].render();
                }
            }
        },
    
        /**
         * Expand template method. Override it.
         */   
        expand: function() {
            this.collapsed = false;
            this.renderChildren();
            this.renderTarget.show();
        },
    
        /**
         * Collapse template method. Override it.
         */    
        collapse: function() {
            this.collapsed = true;
            this.renderTarget.hide();
        }
    });
    

    ​Here I pre-defined the templates and hardcoded the expanding/collapsing logic that happens on click inside widget’s first paragraph element.

    This is how you would use the widgets:

    // Using our widgets
    var containerPanel = new Widget({
        tplData: {txt: 'Hello world!'},
        renderTo: $('body'),
        collapsed: true,
        children: [
            new Widget({
                tplData: {txt: '&nbsp;&nbsp;Child 1'},
                collapsed: true,
                children: [
                    new Widget({
                        tplData: {txt: '&nbsp;&nbsp;&nbsp;&nbsp;Child 1.1'}
                    }),
                    new Widget({
                        tplData: {txt: '&nbsp;&nbsp;&nbsp;&nbsp;Child 1.2'}
                    }),
                    new Widget({
                        tplData: {txt: '&nbsp;&nbsp;&nbsp;&nbsp;Child 1.3'}
                    })
                ]
            }),
            new Widget({
                tplData: {txt: '&nbsp;&nbsp;Child 2'}
            })
        ]
    });
    

    You can see a live example on jsFiddle: http://jsfiddle.net/dipish/XDmWq/
    Just click on items and look at the dynamically generated markup.

    I think the code is self-explanatory but feel free to ask any questions. Note that the code uses jQuery Templates Plugin but it is just for convenience.

    If you have many complex components in your web app you may want to use something more serious than bare jQuery, like ExtJS or Dojo Toolkit. Such frameworks typically provide you a convenient class system and base widget/component logic to build on, besides lots of other things.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I'm trying to accomplish the same thing that mailto:bgates@microsoft.com does in Internet Explorer
I am trying to accomplish the following. I need to use Drupal 6 as
I am trying to accomplish something that seemed quite simple... I have 3 div
What I am trying to accomplish here is a dictionary with linked list. There
I'm using the flag friend module, and I'm trying to accomplish what I thought
I'm trying to use the python-daemon module. It supplies the daemon.DaemonContext class to properly
What I'm trying to accomplish My app generates some tabular data I want the
So I am trying to accomplish something like this: SELECT * FROM table WHERE
I'm trying to accomplish simply adding a css class to a div on alternate
I'm trying to accomplish a simple task of creating a database with 2 tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.