Render Partial TemplateView in Typo3 using Extbase - ajax

I want to lazy load several PartialTemplates in an Extbase Controller via Ajax.
Therefore I found the following script to render only a Partials but it doesn't work in my context:
private function renderPartial($partialName='', $data = array()){
if($partialName==''){
throw new \TYPO3\CMS\Extbase\Mvc\Exception\RequiredArgumentMissingException('The Partial name must be defined.', 123456789);
}
$this->templateView = $this->objectManager->create('TYPO3\CMS\Fluid\View\TemplateView');
$res = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($this->controllerContext->getRequest()->getControllerExtensionKey()) . 'Resources/Private/';
$this->templateView->setLayoutRootPath($res);
$this->templateView->setPartialRootPath($res . 'Partials/');
$this->templateView->setRenderingContext($this->objectManager->create('TYPO3\CMS\Fluid\Core\Rendering\RenderingContext'));
$this->templateView->setControllerContext($this->controllerContext);
return $this->templateView->renderPartial($partialName, Null, $data);
}
This results in the following Error:
Fatal error: __clone method called on non-object in /var/www/html/typo3_src-7.6.0/typo3/sysext/fluid/Classes/View/AbstractTemplateView.php on line 281
I Goolge for this and found many posts having the same problem. What can I do to make this working. A workaround using the StandaloneView and setTemplatePathAndFilename() works well. Is it possible to use Partial without a Layout/Section?
EDIT:
I added some debug Informations and the Controller Action...
public function searchAction(\PCON\Avm\Domain\Model\DemandSearch $demandSearch = NULL) {
//GlobalSearch
if($demandSearch != NULL) {
$searchResult = $this->demandSearchRepository->findDemanded($demandSearch);
$this->view->assign('searchResult', $searchResult);
}
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->controllerContext);
$this->templateView = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\TemplateView');
$res = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath($this->controllerContext->getRequest()->getControllerExtensionKey()) . 'Resources/Private/';
$this->templateView->setLayoutRootPath($res);
$this->templateView->setPartialRootPath($res . 'Partials/');
$this->templateView->setRenderingContext($this->objectManager->get('TYPO3\\CMS\\Fluid\\Core\\Rendering\\RenderingContext'));
$this->templateView->setControllerContext($this->controllerContext);
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->templateView);
return $this->templateView->renderPartial('Search/SearchForm.html', Null, array('searchResult'=>$searchResult));
}
I do not think that this is a AJAX Problem (definded by PageNum).
If I call the controller Action without AJAX the same error appears ...

You can't use renderPartial() oder renderSection() from extbase/php in StandaloneView. It's a missing feature that will probably not be implemented in TYPO3 7.6 and below. See this task: https://forge.typo3.org/issues/54509
However, you can use partials and sections in Fluid. Just assign your variables, then render your template with
$this->templateView->render();
And use partials and section in your Fluid template.

Related

How to Route Codeigniter with URI Segments there

how to route properly url with segments in codeigniter.
this is my url .
https://www.test.com/region/india/about/people/gaurav-Singh/1
this is in my route
$route['region/india/about/people/(:any)/(:any)'] = "region/india/memberview/$1/$2";
this is my controller
public function memberview()
{
$teamid = $this->uri->segment(5);
$data['view'] = 'region/india/team-member-view.php';
$this->load->model('region/India_model');
$data['team'] = $this->India_model->tmview($teamid);
$this->load->view('region/layout', $data);
}
this is my model
public function tmview($teamid){
$this->db->query("select * from ojiteam");
$this->db->where('id',$teamid);
$query = $this->db->get();
return $query->result_array();
}
in my view i am showing data with
<?php echo $team['tmname'];?>
but its not working, it is showing 500 error.
help me with this issue. i have searched and went through codeigniter but not able to solve this.
Depending on the environment you are working on (see docs) you can debug to figure out what is causing this 500 error. Usually this means somehting is wrong with your code, you will have to debug to find out what file and line this error is generated from.
To use numbers in your routing you should do (:num), this way only numbers are allowed on that part of your routing (see docs).
On the controller part, you can pass variables to your controller from your routing options, so;
$route['region/india/about/people/(:any)/(:any)'] = "region/india/memberview/$1/$2";
public function memberview( $area, $teamid )
{
// Your coding
// $area now is; gaurav-Singh
// $teamid now is; 1
}
This way you don't have to worry about which part of the URL you need to use because it's all set.

How to render a cms page with default theme AND variables from controllers in OctoberCMS?

I'm wondering how I can render a view, or display a page with my default theme in OctoberCMS, via a route that executes a function in a controller.
If I have the following route:
Route::get('bransje', [
'uses' => 'Ekstremedia\Cityportal\CPController#bransje'
]);
And in my controller CPController ive tried several things, like I used to with Laravel:
public function bransje() {
$stuff = Stuff::with('info');
return View::make('cms::bransje')->with('stuff',$stuff);
}
But I cannot seem to get it to work, and I've tried to search the web, but it's hard to find answers. I have found a workaround, and that is to make a plugin component, then I can include that component and do:
public function onRun()
{
$this->eventen = $this->page['stuff'] = $this->stuff();
}
protected function stuff()
{
return ...
}
Is there any way so I can make pages without using the Cms, and that are wrapped in my default theme? I've tried
return View::make('my-theme-name::page');
and a lot of variants but no luck.
I know I can also do a:
==
public function onRun()
{
}
in the start of my page in the cms, but I'm not sure how to call a function from my plugin controller via there.
You can bypass frontend routing by using routes.php file in your plugin.
Full example in this video turotial.
If this answer can still be useful (Worked for October v434).
I have almost the same scenerio.
What I want to achieve is a type of routing like facebook page and profile.
facebook.com/myprofile is the same url structure as facebook.com/mypage
First I create a page in the CMS for each scenario (say catchpage.htm)
Then a created a catchall route at the buttom of routes.php in my plugin that will also not disturb the internal working of octobercms.
if (!Request::is('combine/*') && !Request::is('backend/*') && !Request::is('backend')) {
// Last fail over for looking up slug from the database
Route::get('{slug}/{slug2?}', function ($slug, $slug2 = null) {
//Pretend this are our routes and we can check them against the database
$routes = ["bola", "sade", "bisi", "ade", "tayo"];
if(in_array($slug, $routes)) {
$cmsController = new Cms\Classes\Controller;
return $cmsController->render("/catchpage", ['slug' => $slug]);
}
// Some fallback to 404
return Response::make(View::make('cms::404'), 404);
});
}
The if Request::is check is a list of all the resource that october uses under the hood, please dont remove the combine as it is the combiner route. Remove it and the style and script will not render. Also the backend is the url to the backend, make sure to supply the backend and the backend/*.
Finally don't forget to return Response::make(View::make('cms::404'), 404); if the resource is useless.
You may put all these in a controller though.
If anyone has a better workaround, please let us know.

Reopening page on Codeigniter

I am trying to reopen same page on Codeigniter. See code below:
<?php
class Leads extends CI_Controller
{
public $tempName;
public $tempFrom;
public $tempTo;
public function index() {
$this->load->model('Lead');
$leads = $this->Lead->getAllLeads();
$pageNum = count($leads);
$this->load->view('main', array('leads' => $leads, 'pageNum' => $pageNum));
}
public function getLeads(){
$this->load->model('Lead');
$name = $this->input->post('name');
$from = $this->input->post('from');
$to = $this->input->post('to');
$leads = $this->Lead->getLeads($name, $from, $to);
$pageNum = count($leads);
$this->load->view('main', array('leads' => $leads, 'pageNum' => $pageNum));
}
}
?>
As you can see, first I open 'main' from 'index()'. In main, I call 'getLeads()', but my page is not getting updated. Is it because I can't refresh data while on the same page? If yes, how can I work around it?
Thanks a lot!
You cannot call a controller from a view. It sounds like you probably should be using AJAX in your "main" view to get additional data from getLeads().
Option A:
redirect with js to the same function you have, but change the method to get (send params in the URL and get them on the server with $this->input->get()).
Option B:
Since you are posting to getLeads I assume you are using ajax. And since you are using ajax, you should return an url or data to render on your website. Maybe you could use $this->session->set_flashdata('leads',$leads) and $this->session->set_flashdata('pageNum',$pageNum) , send an url to another controller function, extract leads and pageNum $this->session->flashdata('leads') & $this->session->flashdata('pageNum') and then render the view?
Responding with load view will not automatically set the html to what you get.

handling database error with ajax request codeigniter

I am intentionally making error i.e. using one of the coulmn's name wrong to learn how to handle the error with ajax call with codeigniter.
Last Comment in controller function at the end is my question/problem
My AJX Code is following. I am using ajaxform plugin. It shows me all response from controller perfectly but problem is only I am unable to get response from model in controller while using ajax call that is little weird
$('#myForm').ajaxForm
({
success: function(responseText)
{
if(responseText != "1")
$('#feedback').html(responseText);
}
});
Following is snapshot of exact error which i can see in console but unable to get in controller and hence in view. It describes complete error i.e unknown column in given query, but i captured only upper portion.
My model function is below
public function updateItem($id, $data, $tbl)
{
$this->db->where('id', $id);
$r = $this->db->update($tbl, $data);
if($r)
return $r;
else
{
$r = $this->db->_error_message();
return $r;
}
}
My controller function code
public function upadteme()
{
$r = $this->ajax_model->updateItem($uid, $data, 'users');
echo $r." -- "; // Unable to get this echo working
//when using ajaxcall (calling controller through ajax) otherwise fine
}
It looks like the class will not populate _error_message if db_debug is on which it appears to be by default.
In config/database.php, set
$db['default']['db_debug'] = FALSE;
and try again.

Laravel Trying to get property of non-object but not sure why

I want to grab some data from a database and display on a layout page, I've basically started building a small CMS to get into Laravel and all has gone fine so far but now i'm at a wall, and can't find a solution.
I have a layout blade file like so: http://paste.laravel.com/1fB1 nothing majot but you will see i have used $page->meta_title etc in there and in my controller i have:
public function home()
{
$pages = Pages::all();
return View::make('frontend/home')->with('pages',$pages);
}
Which I have a pages model doing nothing else really like so:
class Pages extends Eloquent {
protected $table = 'pages';
}
So why is it trying to get property of non-object and I don't really want to use a foreach because this is going to be the frontend of my 'test' website so a foreach wouldn't suite.
You'll need to access these items as a multi-dimensional array if you don't want to loop through them.
$pages[0]['field_name_here']
or
$pages[1]['field_name_here']
Its a bit of a tough one to answer without knowing how you want your CMS to work.
For example, you could have a route as {pagename} in your routes.php file, then have a page controller where you would get the requested route from the variable passed in. This would then load the page you wanted using the variable
public function page( $pagename ) {
$page = Page::where('page_title', '=', $pagename)->first();
View::make('frontend/page', array( 'page' => $page ));
}
Using a route like that, and the controller, in your view you could use {{ $page->content }} to get the content of the requested page from the database and display it.
Hope this helps.
Edit: Example Route:
Route::get('{pagename}', 'PageController#page');

Resources