I am trying to include files on my page whose paths are stored in my db. I have tried :-
#foreach($data as $articles)
#include( $articles->path )
<br />
#endforeach
This is not working. What am I doing wrong.
I have my files in a folder called pages in view and I have added .blade.php extension as well.
You need to make sure that the path you are referencing actually exists as a file in the views folder i.e.
application/views/pages/articles/first-post.blade.php
One thing I've seen a lot of people do is add views to the home folder but not added it to the include path e.g.
application/views/home/pages/articles/first-post.blade.php
Also, it looks like you are not calling the path correctly in #include. Should be using dot notation
#include('pages.articles.first-post);
You also need to make sure you aren't trying to get a view from a bundle. You need to append the bundle name if you are, like so.
#include('bundle_name::pages.articles.first-post');
Related
I'm trying to put a link in a component's view file (default.php) to another file in the same view (default_formulaire.php) as you can see below:
But I really don't know how to access it in PHP.
I know that the default.php file's url is:
index.php?option=com_multicontact&view=reclamation
but I don't know that of default_formulaire.php.
Thanks for any help :)
The link to any view in Joomla is the following:
index.php?option=com_componentname&view=viewname&layout=layoutname
However, if the layout is omitted from the URL, then it is assumed that it is set to default. So, the following URL:
index.php?option=com_componentname&view=viewname
Will mean that the layout is default, which means that the default.php file will be loaded.
So, in your situation, the URL to load the default_formulaire layout will be:
index.php?option=com_multicontact&view=reclamation&view=default_formulaire
If you need to access a different layout in joomla then you need to add a layout value in joomla url like index.php?option=com_multicontact&view=reclamation&layout=default_formulaire
I am trying to include files in my homepage using blade syntax :-
#foreach($data as $articles)
#include(app_path().$articles->path)
<br />
#endforeach
This is not working.
The error says :-
View [/var/www/blogproject/project/app/pages/articles/first-post.php] not found
I even tried including just the first page :-
#include(app_path().'/pages/articles/first-post.php')
But the normal php include is working fine :-
<?php include(app_path().'/pages/articles/first-post.php'); ?>
Please help
That's because that file is not in the app/views directory. When you call #include('filename'), Blade automatically looks for any file with that name, inside the apps/views directory. Also, you're not supposed to write the file extension, as Blade automatically looks for files with .blade.php and .php extensions.
If you want to use files from other directories on the #include tag, add the directory to the paths array, on app/config/view.php. In your case, it'd be something like this:
app/config/view.php
<?php
// ...
'paths' => array(
__DIR__.'/../views',
__DIR__.'/../pages'
);
Then, you'd call it on blade like so:
#include('articles/first-post')
Is it possible to get the location of a view that is passed into a Laravel view composer?
View::composer('*', function($view) {
// I want to find out the location of the view file here
// e.g. master.something.header
// then add this to an array
$loadedViews = View::share("loadedViews");
$loadedViews[] = $thisViewName;
});
The reason is that I want to have a variable that will be shared between views and contain an array of all the views that are loaded. Any css and js files will be located in directory structure that matches the views one.
This means I can then have a css and js view which then include the required css and js files for the views on the page. All css and js will be directly linked to a specific view.
If there is already a way to do this, or a way to get a list of loaded views, please let me know!
$view->getName() was the answer that I wanted.
$view->getPath() is the actual path to the file.
One of my view file in cake is getting very long, like 300+ lines already. And i find it very difficult to keep track of the understanding.
Is it a good idea to split them up into smaller files and then including them in the parent view file?
If its ok to be done,
In what extension should i create the smaller files? .ctp or .php?
Including them with require_once(view-child1.ext) should be fine, right?
Im fairly new to cakePHP. So i prefer advises from the experts over here. Please put me in the right direction.
EDIT
Thanks for the help guys.
I tried it. But i cant seem to pass the variable. echo $this->Element('reviews/view-goal',$history); Parent view shows and error saying undefined variable in that element.
Im calling the elements from this loop:
foreach($histories as $date => $history)
Cant pass $history. But $histories is being passed correctly.
You should make elements in View/Elements folder with .ctp extension.
This link would help you to make clean separation of your view files with the related/repeated code.
An element is basically a mini-view that can be included in other views, in layouts, and even within other elements. Elements can be used to make a view more readable, placing the rendering of repeating elements in its own file. They can also help you re-use content fragments in your application.
Elements live in the /app/View/Elements/ folder, and have the .ctp filename extension. They are output using the element method of the view:
<?php echo $this->element('helpbox'); //without extension ?>
You can pass variables from your view to the element.
In your view:
<?php echo $this->Element('reviews/view-goal', array('history' => $history));
In view-goal.ctp element you can directly access $history variable.
Yes, it is a very good idea. But don't use the normal require() of PHP.
CakePHP has a feature called "elements", a mechanism to put parts of a view into separate .ctp files. The files go in a special folder, View/Elements
You can include an element like this:
echo $this->element('sidebar/recent_comments');
If you need any variables inside the element, you need to pass them in an additional array parameter:
echo $this->element('sidebar/recent_comments', array('variable_name' => /* Variable content */));
In order to keep your view files small, you should also make sure that you put stuff that is shared by most pages (header, footer) into the Layout file. And obviously: keep JS and CSS in external files.
I'm sure it's a simple one-liner, but I can't seem to find it.
How can I use a different layout file for a particular action?
Update: This worked for me, thanks!
// Within controller
$this->_helper->_layout->setLayout('other-layout') //other-layout.phtml
//Within view script
<?php $this->layout()->setLayout('other-layout'); ?>
From inside a Controller:
$this->_helper->layout->setLayout('/path/to/your/layout_script');
(via these docs)
EDIT: I should mention that the path is relative to whatever your layout directory is (by default, it's application/layouts/scripts/)
You can also use like this
// Within controller
Zend_Layout::getMvcInstance()->setLayout('layout_name');
//Within view script
<?php $this->layout()->setLayout('layout_name'); ?>
Your layout must be in /layouts/scripts/ folder, otherwise you need to specify the path also. No need to write .phtml, just name of the layout