themesoctobercmsrenderpartial

How can I render a theme partial from within a component


I want to reuse a modal partial between several plugins and components. Depending on the Bootstrap version supplied with a theme, the modal may have different properties so I have decided to create a modal in each theme to use site wide. When I change a theme I just add appropriately coded partial in the theme.

Then my component can just render the modal when needed with needed variables.

My solution will be like:

 public function modalSet($sections) {
        $modal_path = $this->getTheme()->getPath() . '/partials/site/modal.htm';

        return ['#modal' => $this->renderPartial($modal_path,
            [
                'title' => array_get($sections, 'title', ''),
                'subhead' => array_get($sections, 'subhead', ''),
                'body' => array_get($sections, 'body', ''),
                'foot' => array_get($sections, 'foot', ''),
            ]),
        ];
    }


    onShowInfo(){
        return $this->modalSet([
                'title' => 'Information About The Thing',
                'body' => '<p>The thing is really Big</p>',
        ]);
    }

The problem is that $this->renderPartial tries to find a partial in the component instead of the theme even if I pass the whole theme partial path.

I get Error: " The page '/home/kurt/public_html/mysite/themes/mytheme/partials/site/modal.htm' is not found"

So what do I need to do in order to render a Theme partial from inside a component?


Solution

  • There is little difference in theme partials they are acting differently from regular partials. they are kind of Halcyon Model // little off topic

    ok, here is the way to do it.

    just use name with directory [ make sure you use .htm extension for your partial files its needed. and site folder must be inside partials folder ]

        // use this path it will respect current active theme
        // and pick partial from active theme automatically 
    
        $modal_path = 'site/modal.htm'; 
        return ['#modal' => $this->renderPartial($modal_path,
            [
                'title' => array_get($sections, 'title', ''),
                'subhead' => array_get($sections, 'subhead', ''),
                'body' => array_get($sections, 'body', ''),
                'foot' => array_get($sections, 'foot', ''),
            ]),
        ];
    

    it will do trick and load partial from the directory and give you desired html

    if any doubts please comment.