how do I make available some data for a layout/layout.phtml script without having to create a view script from a controller?
I've tried the following in indexAction function, but it does not work. When I do not create the view script I get an error. I could created empty one, but I don't like this solution much. Any better ideas?
$this->layout->content = "foo"
$this->_helper->viewRenderer->setNoRender(true);
Thanks in advance
You don't actually render some data you make the data available and then choose which script to render. Don't confuse the terminology.
What you have done there is stopped any script from being rendered by using the :
$this->_helper->viewRenderer->setNoRender(true);
When you do
$this->layout->content = "foo";
You are setting the property content, which you then neeed to make use in your layout script.
So then in your layout.phtml script (which I hope you have already configured to render by efault) you then just do this
echo $this->content
Notice that I don't actually use $this->layout because when you are inside the layout, $this equal $this->layout. The same goes for $this->view->foo is $this->foo inside your view.
I hope this helps.
Any questions just ask.
Related
I'm posting this after my hair has been ripped out, ran out of rum, and tried everything I can find on google. I've been developing a site using codeigniter which makes use of templates. I've built the backend first and all is working properly there. So now i've started on getting the front end working which is where I'm hitting the issue.
I've created a controller called pages.php which is going to parse the uri string of the current page, use my library to get the page data from the database, then display it. My pages are all created through an editor on the back end and stored in the database.
So here's the pages controller
class Pages extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library("pages");
}
public function display_page()
{
$page_slug = $this->uri->segment(1);
$data["joes"] = "Here's joes first variable";
$this->pages->get_page($page_slug);
}
}
and here's the error message i get when i hit my url like this demo.mydomain.com/joes-test
and here is how my routes are set up. $route['(:any)'] = 'pages/display_page';
My Pages.php library works perfect on the back end but it's a large file. I've only posted the get_page function below. If you need to see everything let me know. But i dont believe the issue has anything to do with the library itself.
public function get_page($slug){
$objPages = new pages();
$objPages->get_object('slug="'.$slug.'"');
return $objPages;
}
[EDIT] If i place the following inside my homepage controller it works. But the calling function needs to be inside the library.
$this->load->library('pages');
$the_page = $this->pages->get_page("joes-test");
I want to call $this->get_object("joes-test") but this doesn't work. get_object() is an inherited function inside the library.
Now oddly enough. The code i put above will NOT work if i do the exact same thing inside the pages controller
any help leading to a solution would be awesome. I'm under a time crunch and pay to get some assistance. Thanks in advance.
No you can't use the same name with controller and library. please choose another name. for example Mypages for you controller name.
change your routes
$route['(:any)'] = 'mypages/display_page';
then call your controller.
http://demo.mydomain.com/joes-test
I think there nothing wrong with library uri, because as codeigniter official website say: This class is initialized automatically by the system so there is no need to do it manually.
I don't know about lib pages, but how about use
$this->load->view(<file-html>);
and if you want to passing data in variable, you can add variable like this
$this->load->view(<file-html>, $data);
Hope this help, Cheers
I have two questions:
In a controller ControllerMainIndex in opencart we do define
$this->children = array("common/footer");
Then how should I utilize it in the main/index.tpl file?
It is a little bit vague for me. We have told the controller to get footer but in the main file how should we specify their position?
Second question, I create controller ControllerMasterNewPage and then in I $this->render() (after setting the template to "master/newpage.tpl").
But how should I access this controller? I means what should be typed in the browser for this controller to be process and have output?
1) Just call <?php echo $footer ?> it will output that child content
2) http://example.com/index.php?route=path/MasterNewPage/actionName
e.g.: We have file in {root}/catalog/controller/product/category.php (class name ControllerProductCategory), call it: http://example.com/index.php?route=product/category
In a class file, I am trying to render a .phtml file into a variable and pass the HTML output of that .phtml file to a variable in another ViewModel. How do I do this in ZF2?
The code I have so far is, which doesn't really seem to work:
$layout = new ViewModel();
$layout->setTemplate('myPhtmlFile.phtml');
$layout->setTerminal(true);
$viewModel = new ViewModel();
$viewModel->setVariable('formBody', $layout);
Please see the answer I have provided in the following thread:
How to render ZF2 view within JSON response?
This is basically the same:
you need the PhpViewRenderer
you save the output of the render() function into a variable
you pass that variable to a view
However doing this has pretty much only the use-case that's described within the other post. Why do you want to do this? It is highly likely that there's a much better solution to what you're trying to accomplish.
I'm loading javascript files in the bootstrap as usual, but there's a file that I want to have included only if it's a page that has a form
->appendFile('http://myurl.com/js/formscript.js');
Is there a way to detect the page being loaded, from the bootstrap so I can decide whether or not to include this file?
I thought about passing a variable from the Form to the view, and then checking for that variable in the bootstrap, but it's not working.
This would be in my form
$layout = new Zend_Layout();
$view = $layout->getView();
$view->formscript = true;
and this would be in my bootstrap
if ($view->formscript)
but var_dump($view->formscript) give me null, so any other ideas to activate js files only in specific conditions?
To include javascript files in a particular pages alone, add the following code in those pages(I mean view scripts - *.phtml).
<?php
$this->headScript()->appendFile('http://myurl.com/js/formscript.js');
?>
Similarly, to add CSS files to a particular page, do the following.
<?php
$this->headLink()->appendStylesheet('http://myurl.com/styles.css');
?>
It is possible, but you do not need your bootstrap. You can just access the variable from your layout:
//form
$view = Zend_Layout::getMvcInstance()->getView();
$view->formscript = TRUE;
//layout
if($this->formscript)
{
$this->headScript()->appendFile('http://myurl.com/js/formscript.js');
}
echo $this->headScript();
Do not use getView() in your form as it will return the view object for the form, not for your application. This has tripped me up more than a couple of times >.>
Your idea to set a flag - something like $view->hasForm - in your view seems like a pretty reasonable approach. But as others have noted, it shouldn't be the form itself that attempts to set the flag since it doesn't really have access to view object until rendering time.
Instead, wherever you place a form into your view - probably in a controller, perhaps even in a front controller plugin - simply set your flag there.
Then your view script or layout can call $this->headScript()->appendFile() if the flag has been set.
Why not move over appendFile() to your form class (of course if you use Zend_Form), you would be sure that your JS line will be created only in the same time as your form. The place for this line is good in init() as well as in render()
class Your_Form extends Zend_Form {
public init(){
$this->getView()->appendFile('http://myurl.com/js/formscript.js');
[...]
}
}
I am currently building a small admin section for a website using Zend Framework, this is only my second time of using the framework so I am a little unsure on something things. for example are I have an archive option for news articles where the user will hopefully click a link and the article will be archived however I cannot work out how to get this to run without having a view?
this is my controller
public function archiveNewsAction()
{
//die(var_dump($this->_request->getParam('news_id')));
$oNews = new news();
$this->_request->getParam('news_id');
$oNews->archiveNewsArticle($news_id);
//die(var_dump($oNews));
$this->_redirect('/admin/list-all');
}
and this is my model
public function archiveNewsArticle($news_id)
{
//die($news_id);
$db = Zend_Registry::get('db');
$sql = "UPDATE $this->_name SET live = '0' WHERE news_id = '$news_id' LIMIT 1";
die($sql);
$query = $db->query($sql);
$row = $query->fetch();
return $row;
}
I would appreciate any help any one can give.
Thanks
Sico
I use this with calls to AJAX-only actions that I either don't want output or I'm using some other output, like XML or JSON:
// Disable the main layout renderer
$this->_helper->layout->disableLayout();
// Do not even attempt to render a view
$this->_helper->viewRenderer->setNoRender(true);
This has the added benefit of no overhead of redirection if what you are doing has no output/non-HTML output.
To disable view rendering in an action (put this in the specific action. If you want it for the entire controller put it in the init method):
$this->_helper->viewRenderer->setNoRender();
If you are using the layout component of ZF also add this:
$this->_helper->layout->disableLayout();
I could not figure out your code there. in your model you are calling die(). why?
it will stop the execution. are you sure about that line? anyway, if you have a controller in Zend Framework and do not need any view, you can turn the view off by this line:
// code in your controller
$this->_helper->viewRenderer->setNoRender(true);
// the rest of the controller
now the controller will not search for a view script to show to the user.
make sure you will call
$this->_redirect()
after all of your controller job is done.
Orignal Answer:
Your call to:
$this->_redirect();
Calls the Redirector action helper, which (unless you've configured it not to) will automatically exit the script as soon as the headers are written, so the view will never be called or rendered, there's no need for a view script.
Follow-up Answer:
In order to call the action without sending the user to the other "page" and then redirecting back again you'll need to use an XMLHttpRequest (AJAX) call. These links should provide the information you need:
http://developer.mozilla.org/en/AJAX
http://www.ibm.com/developerworks/xml/library/wa-ajaxintro1.html
http://www.oracle.com/technology/pub/articles/schalk-ajax.html
Also take a look at some JS frameworks that make using XMLHttpRequest cross-browser much easier:
http://www.prototypejs.org/
http://mootools.net/
Zend Framework actually has built in support for the Dojo JS framework, which you may find easier:
http://framework.zend.com/manual/en/zend.dojo.html
http://www.dojotoolkit.org/