8.5.2: How to insert and render content in theme template from view controller

Permalink 1 user found helpful
Laravel and concrete5 gurus,

I'm trying to insert a piece of content into a theme's template and I follow the view rendering section:
https://documentation.concrete5.org/developers/framework/routing/vie...

and I have this in my view controller:
$view = new View('test');
$view->setPackageHandle('my_package');
$view->setController($this);
$view->setViewTheme('elemental');
$view->setViewTemplate('full');
$this->set('html', $this->app->make('helper/html'));
$this->set('content', 'This is a test');
return $this->responseFactory->view($view);


but it throws an error "Call to undefined method Concrete\Core\View\View::getStylesheet()" at the theme's header_top.php at this line:
<?php echo $html->css($view->getStylesheet('main.less')) ?>

What am I missing?

Is this even possible in concrete5? I want something similar to Laravel's view rendering with a '@yield('content')' inside a blade template.

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
What I'm trying to do is to break down the c5 single page controllers and views to be closer to RESTful API.

I have a package with Dashboard controllers and views. Each controller has hundreds if not thousand lines of code. Same with the views, all controller actions are in the same view file.

You know how it's done in Laravel? You have a single layout app.blade.php with a '@yield('content')' and you have various separate views rendering the content depending on the controller/route.

Is this possible to do in c5? The above approach doesn't work because of the way the c5 themes are done, they need the style sheets. Building and supplying a whole theme for the package seems wrong.

Take for example a Classifieds package. It's currently set up with a Dashboard controller called Classifieds in packages/classifieds/controllers/single_page/dashboard/classifieds.php and a corresponding single page view in packages/classifieds/single_pages/dashboard/classifieds.php. This page is templated and themed as any other Dashboard page.

The controller has many actions: view, add, edit, save, delete etc. All code is in the single kilometer long file. Same with the view, all action views are in that single kilometer long file. All is working - no problem. But it's a nightmare to find and edit.

What I want is the following. I want to be able to have a single Dashboard templated page (like that Laravel's layout) and a number of separate view files with content depending on the controller actions, e.g. index.php, create.php, edit.php, show.php etc. which would be inserted into the main template. This way each file will be nice and short. Can this be done?
Parasek replied on at Permalink Reply
Parasek
Leave only view() method in this controller (or just redirect it to any page)
packages/classifieds/controllers/single_page/dashboard/classifieds.php


Create another controllers inside 'classifieds' folder and put your code into view() method (or any other like add() edit() etc.)
packages/classifieds/controllers/single_page/dashboard/classifieds/edit.php
packages/classifieds/controllers/single_page/dashboard/classifieds/something.php


Your single page view files hierarchy should look like that:
packages/classifieds/controllers/single_page/dashboard/classifieds.php
packages/classifieds/single_pages/dashboard/classifieds/edit.php
packages/classifieds/single_pages/dashboard/classifieds/something.php


If you want to select specific view to render, paste this at the end of your dashboard controller method:
$this->render('/dashboard/classifieds/something');
linuxoid replied on at Permalink Reply
linuxoid
That's not really how RESTful controllers are used. A RESTful controller has all 7 methods in 1 file, + separate views to serve the actions, + extra classes for extra processing/calls.

Are you suggesting to have a separate controller for each REST action x number of pages?
Parasek replied on at Permalink Reply
Parasek
Just use one controller and last code in my post in every method (load different view depending on method: add/edit etc.).
And move as much code as you want/can outside controller.
linuxoid replied on at Permalink Reply
linuxoid
that may work if you override the core but it doesn't work from a package because the package doesn't provide a theme and that rendering only works with themes without requiring stylesheets.

They say it in the doc:
https://documentation.concrete5.org/developers/framework/routing/vie...

"Important Note About Themes

When rendering a view template inside a theme, you need to ensure that the theme will work fine without a valid page object. This is not always the case. Many themes are coded to work with the automatic $view object being an instance of the Concrete\Core\Page\View\PageView object, which subclasses Concrete\Core\View\View. If your theme templates call methods like $view->getStylesheet() for example, rendering them without a valid page (like in this instance) will cause an error."

and I confirm that - rendering and their instructions don't work for a package.