I am using Widget in my Header layout.
In My controller .I have set following variables:
public $testVal="testing";
I have created a widget in YII and I use it in my Header layout:
<?php $this->beginWidget('CountryWithCityWidget'); $this->endWidget();?>
My widget Code is:
<?php
class CountryWithCityWidget extends CWidget
{
public $countryList;
public $cityList;
public function init()
{
print_r($testVal);// I need $testVal testing here from view ***How ????***
echo "==============================";
die;
parent::init();
}
public function run()
{
$this->render('countryWithCityWidget',array('countryList' => $this->countryList,'locationDetailList'=>$this->cityList));
}
}
?>
In view I will get it like $this->testVal and then from view I want to send $testVal to widget where I am using it.
How can I do that?
Try
$this->widget('CountryWithCityWidget', array(
'testVal' => $testVal
));
and add a property testVal to your widget class. Also pass your $testVal var to your view.
for further reading
Related
I'm running Laravel 7, and I wonder if it is possible to return a rendered Blade component from a controller, just like you would with a view. I can return the view of the component like the following.
return View::make('components.some-view');
However, I do not have access to any of the data or methods inside the SomeView component class. If I try to use a variable defined in the component, I receive an undefined variable error.
Try this, it works for me in Laravel 8
for example I have App\View\Components\Form\Button component
<?php
class Button extends Component{
public $variable1;
public $variable2;
public function __construct($variable1, $variable2){
$this->variable1 = $variable1;
$this->variable2 = $variable2;
}
public function render(){
// return view('view_component_name');
}
}
?>
& I wants to render that component in controller without view layout
then I can do like this
<?php
use App\View\Components\Form\Button;
class TestController extends Controller{
public function index(Request $request)
$obj = new Button($variable1, $variable2);
$obj->render()->with($obj->data());
}
}
?>
** data function include methods, properties and attributes of Component.
I hope this one helps to you
You can render a blade component from your controller using view function:
//first parameter = Path of your component
//second argument = All your variables that your component receive
$html = view('components.yourComponentName', ['x-variable' => $value]);
return $html;
Create object of component in controller like
$com = new SomeComponent($variable1, $variable2);
//call the render function
$comHtml = $com->render();
Hopefully this will help.
Use YourDirectory\some-view;
$controllerObject = new some-view;
$controllerObject -> variable;
i am using codeigniter (template) library in codeigniter. I always repeat template library code in every function. will you help me to reduce the code.
public function index(){
//Load View in template file
$this->template->title('Contacts')
->set_layout($this->admin_home_layout)
->set_partial('header', 'admin/admin_share/admin_header')
->set_partial('sidebar', 'admin/admin_share/admin_sidebar')
->set_partial('footer', 'admin/admin_share/admin_footer')
->set_partial('hidenbar', 'admin/admin_share/admin_hidenbar')
->build('admin/admin_home');
}
These 4 lines i am using in every function. how i can use one time in one controller and how to get in function.
->set_partial('header', 'admin/admin_share/admin_header')
->set_partial('sidebar', 'admin/admin_share/admin_sidebar')
->set_partial('footer', 'admin/admin_share/admin_footer')
->set_partial('hidenbar', 'admin/admin_share/admin_hidenbar')
This is how I do it ( applicable for any framework in PHP that has similar way of rendering views):
Suppose that the controllers look like this:
<?php
class AdminController extends BaseController{
public function viewData($id){
// do something
$this->render('something static 1');
$this->render('customview');
$this->render('something static 2');
$this->render('something static 3');
}
}
?>
What I would do is create a base controller to do this rendering tasks ( and more repetitive stuffs):
<?php
class MyBaseController extends BaseController{
protected function renderAll($param){
if(empty($param)){
return false;
}
$this->render('something static 1');
$this->render($param);
$this->render('something static 2');
$this->render('something static 3');
return true;
}
}
?>
Once this is done, I will simply inherit the newly created controller in all my controllers, such as:
<?php
class AdminController extends MyBaseController{
public function viewData($id){
// do something
$this->renderAll('customview');
}
}
?>
You can re-use this in any controller action you want to, just inherit the custom base controller class.
you can set them in the constructor:
public function __construct() {
parent::__construct();
//Do your stuf here
}
Or you could create a function for it:
function set_partials() {
$this->template->set_partial('header', 'admin/admin_share/admin_header')
->set_partial('sidebar', 'admin/admin_share/admin_sidebar')
->set_partial('footer', 'admin/admin_share/admin_footer')
->set_partial('hidenbar', 'admin/admin_share/admin_hidenbar')
}
In this second case, you're index would look like this:
public function index(){
//Load View in template file
$this->template->title('Contacts')
->set_layout($this->admin_home_layout);
$this->set_partials();
$this->template->build('admin/admin_home');
}
i suggest a different logic, just a custom array for your partials like this :
protected $partials = [
'header' => 'admin/admin_share/admin_header',
'sidebar' => 'admin/admin_share/admin_sidebar',
'footer' => 'admin/admin_share/admin_footer',
'hidenbar' => 'admin/admin_share/admin_hidenbar'
];
and use this function to build layout
public function build_layout()
{
$this->template->title('Contacts')->set_layout($this->admin_home_layout);
foreach($this->partials as $k => $partial){
$this->template->set_partial($k,$partial);
}
$this->template->build('admin/admin_home');
}
I've tried many solutions that had the same questions like mine. But didn't found a working solution.
I have a controller:
event.php
And two views:
event.phtml
eventList.phtml
I use eventList to get data via ajax call so I want to populate both views with a variable named "eventlist" for example.
Normally I use this code for sending a variable to the view:
$this->view->eventList = $events;
But this variable is only available in event.phtml.
How can I make this available for eventlist.phtml? (without adding a second controller)
Edit:
I get this error now
Call to undefined method Page_Event::render()
Function:
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
$this->eventList = $this->_event;
$this->render();
$this->render('eventlist');
}
If I use $this->view->render('event.phtml') and eventlist.phtml it won't pass the data
I'm using zend version 1
You can pass variables to other views using render()
public function fooAction()
{
// Renders my/foo.phtml
$this->render();
// Renders my/bar.phtml
$this->render('bar');
}
Copy and paste this in your controller and rename your controller from event.php to EventController.php
class EventController extends Zend_Controller_Action
{
private $_event;
public function init(){
$dbTable = new Custom_Model_DbTable_Events();
//Get Events
$this->_event = $dbTable->getEntries($this->webuser->businessId);
$this->index();
}
public function indexAction(){
// You're calling the index.phtml here.
$this->eventList = $this->_event;
$this->render('event');
$this->render('eventlist');
}
}
To specify that only written #Daan
In your action:
$this->view->eventList= $events;
$this->render('eventList'); // for call eventList.phtml
In you View use : $this->eventList
You could render it within the view itself (eventList.phtml), rather than within the controller, using the same line of code you used above:
$this->render('event[.phtml]');
I have created a custom view in Calls that is based on the editview but I want to change the save button to post back to a custom action in my custom controller. I think I basically need to overide the editview defs with my own defs. I have tried adding calllistviewdefs.php but this doesn’t seem to work.
What is the correct way to override edit view buttons in this case?
Here is the code so far:
Controller
require_once('include/MVC/Controller/SugarController.php');
class CallsController extends SugarController {
function action_CallList() {
$this->view = "calllist";
}
}
view
require_once('include/MVC/View/SugarView.php');
class CallsViewcallList extends ViewEdit {
public function CallsViewcallList() {
parent::SugarView();
}
public function preDisplay()
{
parent::preDisplay();
$metadataFile = 'custom/modules/Calls/metadata/calllistviewdefs.php';
}
public function display() {
parent::display();
}
}
There are a few ways, but I would suggest outputting javascript after parent::display() and hijack the existing Save buttons (top and bottom). Reset the onclick for those buttons to whatever you would like.
i want to send some data from Action Helper to view Partial and i am unable to do it, to get the clear picture. here is all the related code i am using.
in my layout.phtml i am using this placeholder. to generate onDemand navigation menu.
<?php echo $this->placeholder('action-navigation'); ?>
so when i need it in my controller or action method i can simply place use this code.
$this->_helper->navigation()->renderActionNavigation();
The Action helper i am using is.
class Zend_Controller_Action_Helper_Navigation extends Zend_Controller_Action_Helper_Abstract
{
private $_view = null;
public function direct()
{
$this->_view = $view = Zend_Layout::getMvcInstance()->getView();
$this->_view->placeholder('action-navigation');
return $this;
}
public function renderActionNavigation()
{
$config = new Zend_Config_Xml(
APPLICATION_PATH.'/configs/navigation.xml', strtolower(
$this->getRequest()->getControllerName().
$this->getRequest()->getActionName()
)
);
$container = new Zend_Navigation($config);
// here i want to send $container to _action-navigation.phtml.
$this->_view->addScriptPath(APPLICATION_PATH.'/layouts/')->render('partials/_action-navigation.phtml');
}
}
this is my view partial _action-navigation.phtml
$this->placeholder('action-navigation')->captureStart();
//i want to get zend_navigation instance. from the above action helper here.
$this->placeholder('action-navigation')->captureEnd();
i have problem sending data from action helper to partial view _action-navigation.phtml how do i do it?
Thank you.
Use partial() instead of render():
$this->_view->partial('partials/_action-navigation.phtml', array('nav' => $container));
And in your partial:
$this->nav // to get your container