I am new to CI and i just want to set the default template page, I am now using a library called Eztemplatei tried to load the view by using the following code:
`$this->template
->set('title','Home')
->load('view','index')
->render();`
but it's not working and it seems that all the files haven't been loaded. Any help would be much appreciated
First, you have to make sure your css and js files directories are correct
'asset_path' => 'assets/urApp/',
// ex: assets/ -> http://www.yourwebsite.com/codeigniter/base/url/assets/
'asset_css_folder' => 'css/', // is added to asset_path
'asset_js_folder' => 'js/', // include trailing slash
'asset_views_folder' => 'views/', // include trailing slash
second, remember once you load some view you can't render again.
so your code should be sth like:
$this->template
->set('title','Home')
->set('view','index')
->render();
Related
I use laravel 5.2 and tried to display a carousel only in index page but doesn't work.
I chose that the codes are not "spreaded" on the index page, they are stored in: public/carousel/carousel.php, too(not ...blade.php).
route.php:
Route::get('/', 'HomeController#index')
HomeController.php:
{
$cats = Category::all();
$carousel = public_path('carousel/carousel.php');
//$carousel = storage_path('public/carousel/carousel.php');
return view('layouts.app', compact('cats', 'carousel'));
}
layouts/app.blade.php:
{{-- #include('carousel/carousel');--}}
#if($carousel)
{{ $carousel }}
#endif
#yield('content')
Finally it displays only: C:\wamp\www\app_name\public\carousel/carousel.php.
Can you help me or point to another better way?
In your controller, you are passing to the view a variable called $carousel, which is the path to your file, as you defined here:
$carousel = public_path('carousel/carousel.php');
This is the reason why it only displays the string. You need to get the actual content of the file:
$carousel = file_get_content(public_path('carousel/carousel.php'));
A better and more laravel-ish way to do it would be to rename the file to carousel.blade.php, store it into the resources/views folder and simply include it from your main blade file (without the need of doing anything in the controller):
#include('carousel')
If you need to display the carousel on certain pages only, you can simply pass a variable $carousel = true on the pages that needs to display it:
$carousel = true;
return view('layouts.app',compact('carousel'));
And in your blade view, include the carousel file only when this variables is present and is true:
#includeWhen(isset($carousel) && $carousel, 'carousel')
I have a laravel project where I have the following view:
'ProjectName\custom\subfolder\resources\views\theview.blade.php'
How can I return this view?
I tried to use view('theview') That did not work because that only works on views inside:
'ProjectName\resources\views'
How can I return the view from outside theresources\views folder?
Inside the config/view.php config file, add to the paths key:
<?php
return [
'paths' => [
realpath(base_path('resources/views')),
realpath(base_path('custom/subfolder/resources/views')),
],
'compiled' => realpath(storage_path('framework/views')),
];
You can load your views in the usual way, eg view('my-view'), however duplicate names (or name collisions) will result in Laravel selecting the view based on the order specified in the paths key.
Absolutely spot on! Just in the Laravel 6.0
'paths' => [
resource_path('views'),
resource_path('views/canaanmodels'),
],
Where my view is in the folder 'canaanmodels' which is inside 'views' floder and the realpath has now been replaced with resource_path and there is no more base_path... Hope it gonna help someone! Cheers
In laravel blade system when we want to include a partial blade file we have to write the full path every time for each file. and when we rename a folder then we will have to check every #include of files inside it. sometimes it would be really easy to include with relative paths. is there any way to do that?
for example we have a blade file in this path :
resources/views/desktop/modules/home/home.blade.php
and I need to include a blade file that is near that file :
#include('desktop.modules.home.slide')
with relative path it would be something like this :
#include('.slide')
is there any way to do this?
if someone still interest with relative path to current view file, put this code in the boot method of AppServiceProvider.php or any provider you wish
Blade::directive('relativeInclude', function ($args) {
$args = Blade::stripParentheses($args);
$viewBasePath = Blade::getPath();
foreach ($this->app['config']['view.paths'] as $path) {
if (substr($viewBasePath,0,strlen($path)) === $path) {
$viewBasePath = substr($viewBasePath,strlen($path));
break;
}
}
$viewBasePath = dirname(trim($viewBasePath,'\/'));
$args = substr_replace($args, $viewBasePath.'.', 1, 0);
return "<?php echo \$__env->make({$args}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";
});
and then use
#relativeInclude('partials.content', $data)
to include the content.blade.php from the sibling directory called partials
good luck for everyone
you need to create custom blade directive for that, the native include directive doesn't work like that.
read this page to learn how to create custom blade directive :
https://scotch.io/tutorials/all-about-writing-custom-blade-directives
\Blade::directive('include2', function ($path_relative) {
$view_file_root = ''; // you need to find this path with help of php functions, try some of them.
$full_path = $view_file_root . path_relative;
return view::make($full_path)->render();
});
then in blade file you can use relative path to include view files :
#include2('.slide')
I tried to tell you the idea. try and test yourself.
There’s now a package doing both relative and absolute includes (lfukumori/laravel-blade-include-relative) working with #include, #includeIf, #includeWhen, #each and #includeFirst directives. I just pulled it in a project, it works well.
A sleek option, in case you want to organise view files in sub-folders:
public function ...(Request $request) {
$blade_path = "folder.subfolder.subsubfolder.";
$data = (object)array(
".." => "..",
".." => $..,
"blade_path" => $blade_path,
);
return view($data->blade_path . 'view_file_name', compact('data'));
}
Then in the view blade (or wherever else you want to include):
#include($blade_path . 'another_view_file_name')
I am new to ci. naybody knows how to minify the url.
for example : yourdmain.com/blog/view/blog-title
I need this url to be like this : yourdmain.com/blog-title
please explain how to do this
this can be many like blog, categories, pages, posts
please help..
Use route.php under your config folder
$route['blog-title] = 'blog/view/blog-title';
if you need dynamically loading based on a title
$route['(:any)/index.html'] = 'blog/view/$1';
// will route any url with /index.html to your controller
$route['(:any).html'] = 'blog/view/$1';
// will route any url with title.html to your controller then pass your title as your function variable
Why index.html or .html
This is my way i use to distinguish my other urls to my blog titles urls .. meaning only urls with extension index.html or .html will be routed to my blog/view path
You can have hyphens instead of underbars putting these lines in the file routes.php
$route['(.+)-(.+)-(.+)-(.+)-(.+)'] = "$1_$2_$3_$4_$5";
$route['(.+)-(.+)-(.+)-(.+)'] = "$1_$2_$3_$4";
$route['(.+)-(.+)-(.+)'] = "$1_$2_$3";
$route['(.+)-(.+)'] = "$1_$2";
I have researched this topic pretty thoroughly but can't find an answer.
I am trying to include a Magneto head into a WordPress page. I created a new wordpress template and added the following code to it.
try {
require_once ABSPATH . '/app/Mage.php';
if (Mage::isInstalled()) {
$mage = Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
if(Mage::getDesign()->designPackageExists('xxx')) {
Mage::getDesign()->setPackageName('xxx');
Mage::getDesign()->setTheme('xxx');
}
// init translator
$mage->getTranslator()->init('frontend');
// init head
$head = $mage->getLayout()->getBlockSingleton('page/html_head');
} }
Then a bit further down in the template I have
echo $head->toHtml();
Now what is happening is some parts of the head are being echoed and some parts are not.
When I go into head.phtml and try to figure out what is happening I notice that any line that contains
$this->getChildHtml()
does not get echoed.
I looked at this example and noticed that the author is manually adding the html and CSS. Why is this? Why don't they get added automatically? Is this a related problem
Thanks
To show a block that is generated inside the header block, you need to first create it, then set it as child of the header block.
eg. Here is how I display within Wordpress a Magento header block, including the currency drop-down block that was generated by getChildHtml() inside the original header.phtml:
Mage::getSingleton('core/session', array('name' => 'frontend'));
$session = Mage::getSingleton("customer/session");
$layout = Mage::getSingleton('core/layout');
$headerBlock = $layout->createBlock('page/html_header')->setTemplate('page/html/header.phtml');
$currencyBlock = $layout->createBlock('directory/currency')->setTemplate('currency/currency.phtml');
$headerBlock->setChild('currency_selector', $currencyBlock);
$headerBlock = $headerBlock->toHtml();
Then you can write the block where you need it on the page:
echo $headerBlock;
I know it's a little late but hopefully this helps others with this issue.
Are you familiar with how magento layouts are rendered? With $head = $mage->getLayout()->getBlockSingleton('page/html_head'); you create a new block without any children. That's why the author needs to add JS and CSS again. To load the default head block have a look at this thread Load block outside Magento. You can load it with $layout->getBlock('head')->toHtml();.