Laravel File Issue - laravel

I am new to laravel. I have two files in two different folders. I can't access one through other.
views/layouts/header.blade.php
views/buysalerent/buy.blade.php
I want to add header.blade.php to buy.blade.php.

As you are using blade templating engine, you can include one view into another view like below
In buy.blade.php, just add
#include('layouts.header')
With the above line, header will be included into your buy.blade.php

Related

Laravel template snippets

Is this any possibilities to make code snippets in Laravel Blade templates ?
I need to do something similar like was in Smarty - you place [tag param1="value" param2="value"] and It's going throught function tag with render specify template under it.
Yes, this is possible by using either Components or Include. These allow you to create so called sub-views that can be called multiple times on a page with a number of parameters.

Strange character Laravel

I am using Laravel 5.3
Hello,
A stranger character "-" is showing at every pages.
See my files:
My View blade contains only a single <h1> tag.
<h1>New Test</h1>
Since it's appearing on every page, you will have 2 main places where it will be:
The top of your web/routes.php file.
It will most likely be located inside your layout file. And you probably are looking for the extra '-' inside your view file which extends the layout file.

Laravel : having the same element on each of my pages

On Laravel 5.2, I'm trying to put two menus (X latest contents, and the same for a specific user) on near every page.
The lazy way would be to generate those arrays in each of the controllers, but is there a way to generate it when the templating system needs them ?
For instance, my template could call something like {{ $menu }} and the menu would be generated only when this was present ?
Laravel has a feature called view composer ,which lets you do some actions when a template file is loaded.
you could set the X latest contents into a template file and then include it wherever you want to and use view composer to fetch the data.

Laravel: How to use composer to inject data across all views?

I want to offload some HTML sections of my site into partials (includes) files rather than repeating the information in each view or layout. However, I also need them to contain specific data. An obvious example is the head section; I would like to put this into a partial, however I also want to be able to set the document title and META fields for each page.
I think the best way is to use Laravel's view-composer
As per the docs, I can use an array to define the composer for multiple views at once:
View::composer(array('view1','view2', 'view3'), function($view)
{
$view->with('count', User::count());
});
However, what if I want to use this composer for every view, as I do in this case?
There's a few answers kicking around SO (such as this one) which suggests I use a wildcard. So here's my code:
View::composer('*', function($view)
{
$view->with('header', View::make('partials.head', $view->getData()));
$view->with('footer', View::make('partials.footer', $view->getData()));
});
Here's the problem: Using this is currently giving me an out of memory error, which suggests that it is very inefficient (and therefore that I really shouldn't be doing this).
So do I really need to pass an array listing every page on my website?
Isn't there a way to use composer for every page rendered, like I can with View::share(); ?
If the data is going to be unique for each view, there's no point in putting it in a view composer; you can do this just by using blade templates, and pass the page-specific data to the view from your controller.
Set up header and footer partials, then set up a base template that uses #include to load your header and footer partials, then a section for your content with #yield('content').
<!DOCTYPE html>
...
#include('partials.header')
#yield('content')
#include('partials.footer')
...
Then your individual page's views would each extend this base template:
#extends('base')
#section('content')
//...specific page content here
#stop
In your header and footer partials, include {{ $someData }} for whatever specific needs to change from page to page, and pass that data to each view from the controller.
This is now possible in Laravel 5.5
Put this is your AppServiceProvider boot method:
View::share('data', [1, 2, 3]);

joomla add view into another view

Im using joomla MVC and I want to build a form that has different tabs which are different sections of the form with inputs in it. There are some tabs that are common to other forms that I need to include.
I would like to be able to load this common content from a separate file or view so i dont have duplicate code, plus is easier when I need to do a change to the form so I dont have to do it in all the forms. It's like displaying a view inside another view.
Is there a way to accomplish this?
A Joomla! provides the loadTemplate method to views.
So if you're currently in a tmpl file loaded for layout edit (ie. tmpl/edit.php ) you can call $this->loadTemplate('tab1'); and Joomla! will load the tmpl/edit_tab1.php file in the same view as your edit.php.
In that same view if you wanted to include say tmpl/other_tab1.php you would have to temporarily set the layout to other, eg. in one of our components during the Run template we need a tab from the Edit template, so we use:
<?php $this->setLayout('edit'); // This is ugly
echo $this->loadTemplate('plan');
$this->setLayout('run'); ?>
To load a template from another view alltogether, I think you would have to temporarily over-ride the view value, load the template then restore the view. eg.
$jinput = JFactory::getApplication()->input;
$jinput->set('view', 'other');
$this->loadTemplate('tab2');
$jinput->set('view', 'original');
NB: this last bit I haven't had time to test but it should work.
You can load a different template file for a different view manually, by simply requiring it. The following is for a view called "nameofotherview" with the layout "layoutname". If this is for an admin view use JPATH_COMPONENT_ADMINSTRATOR instead.
require(JPATH_COMPONENT_SITE . '/views/nameofotherview/tmpl/layoutname.php');
Remember that the data set up in the view class needs to be compatible with the main layout as well as the layout you're loading from elsewhere.
A side effect of doing this is that template overrides won't work. The loadTemplate function is doing a require but it checks the template paths for overrides first.
You can use vews in other views as described in Joomla documentation: Sharing layouts across views or extensions with JLayout

Resources