Return html from controller action in Zend - php

i have a structure like so:
modules -> Controllers -> Amodule -> IndexController
there i have an action which is called:
public function getTemplateAction(){
... //1) load html file
//2) return html
}
this function is called from javascript with get via the url: baseUrl+'/Amodule/index/get-template/viewid/3.
I would like based on the viewid to return an html template, where should i put the .html file and how can i load it in Zend? i tried
this->partial('thetemplate.html');
and putting the html file in Amodule folder but didnt work.
Thanks in advance!

Added another folder at the same level as controller called it views/scripts and in there i created the partial. Then from the view i load the partial as:
<?php echo $this->partial('_partialName.phtml', array(
var1 => value1,
var2 => value2
/*... vars to pass in the partial*/
));?>

A simple solution is to have your html files in a dedicated folder at the root of your project and output the file with readfile :
public function getTemplateAction() {
readfile(APPLICATION_PATH . '/../templates/thetemplate.html');
exit;
}

Related

Symfony2: Get HTML from another file in controller

In my Symfony2 project, I would like to get the HTML of a file and put it in a variable in my controller.
I have a controller function like this:
public function myControllerAction()
{
$htmlOtherFile = '';
return $this->render('#MyBundle/myTargetFile.html.twig', ['htmlOtherFile' => $htmlOtherFile]);
}
The html file I would like to import is in my views folder: htmlOtherFile.html.twig
How can I get the HTML of this file into my $htmlOtherFile variable? I have already tried with file_get_contents, but without success.
all you need to do is call renderView from your controller on the template and put the contents in a variable.
$html = $this->renderView('/path/myTargetFile.html.twig', [/*options in here*/]);
Be sure to call renderView and not render on its own as that will return a Response instance, and not any HTML.
Alternatively, you can call:
$this->render('/path/myTargetFile.html.twig', [/*options in here*/])->getContent();
to return the html from the response.
If it is twig file - you could try this:
$this->renderView('/path/to/template.html.twig', []);
You can call a controller directly from you twig file if you want.
Use the syntax {{ render(controller('MyBundle:functionName')) }}

How to send the value of a variable between two views in yii

Suppose, in Yii framework if any other parameter has to be sent to any other file. How can I do this.
If from admin.php I have to send another variable to update.php. How can I do this
If update.php is child view inside admin.php, you can do follow on admin.php
$this->renderPartial('update', array('param1'=>'value1', 'param2'=>'value2', ...))
(noted this example update.php is same folder level with admin.php)
If both of files came from two different actions, or they have their own URL look like below
public function actionAdmin(){ ...; $this->render('admin');}
public function actionUpdate(){ ...; $this->render('update');}
you have to treat it as query string
admin.php
Example:
<?php echo CHtml::link('Go to Update',array(
'controller/update', 'param1'=>'value1', 'param2'=>'value2', ...)); ?>
More about Yii URL Managger and querystring
http://www.yiiframework.com/doc/guide/1.1/en/topics.url

how to call a view file from controller(cakePHP)

I am learning cakephp from few days ... so please help a bit thanks .
I made a controller --
class PostController extends AppController {
var $name='Posts';
function index(){
$posts=$this->Post->find('all');
$this->set(compact('posts'));
}
in view had post folder with index.ctp file . I want to ask from where it gets "CakePHP: the rapid development php framework" and from where it takes content . I send this data from controller so it prints only var_dump($posts); ... Thanks in advance.
cake php has default layout files in folder "app/view/Layout" .. file name default.ctp in this folder will be taken as default ..
if you open default.ctp in layout you will see something like
<h1><?php echo $this->Html->link($cakeDescription, 'http://cakephp.org'); ?></h1>
and other links defined ..you can comment the and check what changes you get in view.
here is from where view gets ""CakePHP: the rapid development php framework"
this line
<?php echo $this->fetch('content'); ?>
in layout fetch the data from controller to show in view which we set using $this->set() in controller
if you want to change the layout create your own in layout folder
and use in contoller like
class PostController extends AppController {
var $name='Posts';
function index(){
$posts=$this->Post->find('all');
$this->set(compact('posts'));
$this->layout = false; // ot you can set ypur own file like 'xyz' for 'xyz.ctp'
}
to set layout for each action in controller use
function beforeFilter() {
parent::beforeFilter();
$this->layout = 'layout';
}
To learn more about layout see http://book.cakephp.org/2.0/en/views.html#layouts
Hope you got me
"CakePHP: the rapid development php framework"
For above look inside views -> layout -> default.ctp file.
Also change default cakephp routing so you can see posts index action when you have posts in URL.
The best way to call a layout from controller is
$this->layout = 'Your layout page name';

Symfony layout in another folder

Is it possibile to put layout files in the modules/templates (or something else) directory instead of the general app/templates?
I've tried to put the path in view.yml but it doesn't work.
Just found a workaround so I can keep layout.php inside the module/templates folder:
public function preExecute()
{
$template = $this->getContext()->getConfiguration()->getTemplateDir('MODULE', 'layout.php');
$this->setLayout($template . '/layout');
}

Simple File include problem in Zend?

I have created a zend project on ubuntu which is in /var/www/student/ directory.
Now I have head.phtml file in this location:
/student/application/views/scripts/index/head.phtml
When I try to include head.phtml file in
/student/application/modules/test/views/scripts/all/index.phtml
Like this:
echo $this->partial('index/head.phtml');
It gives me following error:
Message: script 'index/head.phtml' not found in path (/var/www/student/application/modules/notification/views/scripts/)
Including files is always a difficult job for me. How to fix this. I have to include this file in many modules then what is permanent solution for this that I should not guess the path
Thanks
You can add several path to look for script view files. The best way is to do it in the bootstrap file for all your common files (like head, footer, metas...).
Just add a setupView method in your bootstrap where you deal with everything which is realted to your views :
protected function _initView()
{
$view = new Zend_View();
// setup your view with jquery and other stuffs...
// [ ... ]
// add the view directory to the stack of scripts paths
$view->addScriptPath(APPLICATION_PATH . '/views/scripts/');
}
<? $this->setScriptPath('/student/application/views/scriptss'); ?>
<?= $this->partial('index/head.phtml') ?>
I added following line in Controller's init() function.
public function init() {
$this->view->addScriptPath( APPLICATION_PATH . '/views/scripts/' );
}
Add head.phtml file in view like this:
echo $this->partial('index/head.phtml');
File is successfully added.

Categories