Hi I'm trying to make something like CMS with widgets so I have a folder widgets in my app folder there are my widgets for example app/Widgets/UsersOnline/UserOnline.php
I loading them from db with widgets model in beforefilter method in appController and there i pass them to the view, so with a widget helper I would like to render them on the position. But i can't get to the folder widgets/UserOnline/view.ctp with view->element() method this method require file to be in Elements/.
TLDR / Actual Question:
Is there any way to load files in view outside the view/ and /elements ? Thanks in advance.
You can use relative paths when calling the element:
<?php echo $this->Element('../../Widgets/UsersOnline/UserOnline'); ?>
Don't forget to name your element file 'UserOnline.ctp' too.
Related
I have the need to determine what app path my controller method will use for serving up the view before it does so. I'm using a theme but I also have many non-themed view files. I'm switching my themes based on domain name (2 domains point to the same Cake install) but need to exclude the non-themed views from rendering inside my theme.
This may sound confusing. Here's what is currently happening if a URL is accessed that does not have a theme view associated with it:
domainA.com/examples/index will render the view app/View/Examples/index.ctp with the layout from app/View/Layouts
domainB.com/examples/index will render the view app/View/Examples/index.ctp BUT with the layout from app/View/Themed/MyTheme/Layouts
This is because the "MyTheme" theme does not contain a view file for this controller-method pair (this is intentional). So I would like to instead have the following established:
domainB.com/examples/index continues to render the view app/View/Examples/index.ctp BUT INSTEAD with the layout from app/View/Layouts
This should only happen, of course, if and only if there is no view file within the "MyTheme" directory structure.
I think this is what you are looking for $this->View->viewPath.
You can use this in any controller call back function or action.
I want to load a modified .tpl file vtigercrm\layouts\vlayout\modules\Vtiger\ListViewContents.tpl to a .php file from which I get HTML data after using AJAX and javascript . It's something feasible?
Im assuming that you want to view that template in the DetailView. In that case you can make a widget in module/Vtiger/models/DetailView.php in getWidget(). Just copy paste and change. The link you give here should go to a function in /module/Vtiger/views/Detail.php.
you should add your new function in the constructor.
$this->exposeMethod('Yourfunctionname');
Here you can copy any function and change it. you need to feed your variables to smarty with :
$viewer->assign('SMARTYVARNAME', $phpVar);
to fill the template. And finally you need to referance the template.
echo $viewer->view('tplname.tpl', $moduleName, 'true');
im not able to comment yet so i hope this is enough.
I have a UsersController and index() action so by default cakePHP will look for the index.ctp in Users view folder. What i would like to do is to create a separate folder called Partials and have the controller look for the view inside the folder instead. Please help as I just started my journey in cake. Thanks
You can define the path to your view by $this->viewPath in your controller action. E.g. for the following folder strucuture:
... you can access the various templates by the following statements:
function index()
{
// by code conventions this action automatically matches /Home/index.ctp
$this->viewPath = "/Partials/"; // matches /Partials/index.ctp
$this->render("mypartialview"); // matches /Partials/mypartialview.ctp
}
I have 2 view files index.ctp and indexMobile.ctp.
i have to use one single controller logincontroller.php and a single function for both ctp file.
If you want to render a specific view use $this->render in your controller
$this->render('your_view_file');
see Rendering a specific view
Here is my example:
// Changing the "layout" (app/View/Layouts") if you want.
// Change this if you want a different layout for this view.
$this->layout='pdf';
// Folder where is the .ctp View file.
// This is also optional, if your view is in some other folder.
$this->viewPath = 'Pdf';
// Call the "{$view}.ctp" in "Pdf" folder (app/View/Pdf/{$view}.ctp).
// Use this for choosing the right View.
$this->render($view);
I have an script file called 'game.js' which I want to add to all actions inside game_controller.php.
I think there is a better solution than copy and paste "Html->script('game', array('inline' => false)); ?>" inside all my ctp files. How do I do that?
I suggest that you create a sperate layout for given pages and put the script in the layout. If you simply MUST do this, then you can use the beforeRender() method like so:
cakephp: can I set $scripts_for_layout from within a controller?