<commit_before># Views

- [Basic Usage](#basic-usage)
- [View Composers](#view-composers)

<a name="basic-usage"></a>
## Basic Usage

Views contain the HTML served by your application, and serve as a convenient method of separating your controller and domain logic from your presentation logic. Views are stored in the `resources/views` directory.

A simple view looks like this:

	<!-- View stored in resources/views/greeting.php -->

	<html>
		<body>
			<h1>Hello, <?php echo $name; ?></h1>
		</body>
	</html>

This view may be returned to the browser like so:

	$router->get('/', function()
	{
		return view('greeting', ['name' => 'James']);
	});

As you can see, the first argument passed to the `view` helper corresponds to the name of the view file in the `resources/views` directory. The second argument passed to helper is an array of data that should be made available to the view.

#### Passing Data To Views

	// Using conventional approach
	$view = view('greeting')->with('name', 'Victoria');

	// Using Magic Methods
	$view = view('greeting')->withName('Victoria');

In the example above the variable `$name` would be accessible from the view, and would contain `Victoria`.

If you wish, you may pass an array of data as the second parameter given to the `make` method:

	$view = view('greetings', $data);

#### Sharing Data With All Views

Occasionally, you may need to share a piece of data with all views that are rendered by your application. You have several options: the `view` helper, the `Illuminate\Contracts\View\Factory` [contract](/docs/master/contracts), or a wildcard [view composer](#view-composers).

First, using the `view` helper:

	view()->share('data', [1, 2, 3]);

> **Note:** When the `view` helper is called without arguments, it returns an implementation of the `Illuminate\Contracts\View\Factory` contract.

Alternatively, obtain an instance of the `Illuminate\Contracts\View\Factory` [contract](/docs/master/contracts). Once you have an implementation of the contract, you may use the `share` method to make data available to all views:

In this example, we'll assume we're sharing the data from within a [global HTTP filter](/docs/master/filters). However, you could also share the data from a service provider, or even a controller:

	<?php namespace App\Http\Filters;

	use Illuminate\Http\Request;
	use Illuminate\Contracts\View\Factory as ViewFactory;

	class TestFilter {

		/**
		 * The view factory implementation.
		 */
		protected $view;

		/**
		 * Create a new filter instance.
		 *
		 * @param  ViewFactory  $view
		 * @return void
		 */
		public function __construct(ViewFactory $view)
		{
			$this->view = $view;
		}

		/**
		 * Run the request filter.
		 *
		 * @param  Request  $request
		 * @return mixed
		 */
		public function filter(Request $request)
		{
			$this->view->share('data', [1, 2, 3]);
		}

	}

#### Determining If A View Exists

If you need to determine if a view exists, you again have two options: the `view` helper and the `Illuminate\Contracts\View\Factory` [contract](/docs/master/contracts):

Using the helper:

	if (view()->exists('emails.customer'))
	{
		//
	}

Alternatively, type-hint the `Illuminate\Contracts\View\Factory` contract and use the `exists` method on the resolved instance:

	<?php namespace App\Services;

	use Illuminate\Contracts\View\Factory as ViewFactory;

	class TaskRunner {

		/**
		 * The view factory implementation.
		 */
		protected $view;

		/**
		 * Create a new class instance.
		 *
		 * @param  ViewFactory  $view
		 * @return void
		 */
		public function __construct(ViewFactory $view)
		{
			$this->view = $view;
		}

		/**
		 * Do some work!
		 *
		 * @return void
		 */
		public function performTask()
		{
			if ($this->view->exists('emails.customer'))
			{
				//
			}
		}

	}


<a name="view-composers"></a>
## View Composers

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want bound to a certain view each time that view is rendered, a view composer can organize that code into a single location.

#### Defining A View Composer

Let's organize our view composers within a [service provider](/docs/master/providers). We'll need an instance of the `Illuminate\Contracts\View\Factory` [contract](/docs/master/contracts), so we'll type-hint that in our provider's `boot` method:

	<?php namespace App\Providers;

	use Illuminate\Support\ServiceProvider;
	use Illuminate\Contracts\View\Factory as ViewFactory;

	class ComposerServiceProvider extends ServiceProvider {

		/**
		 * Register bindings in the container.
		 *
		 * @return void
		 */
		public function boot(ViewFactory $view)
		{
			$view->composer('profile', 'App\Http\ViewComposers\ProfileComposer');
		}

	}

Now each time the `profile` view is rendered, the `ProfileComposer@compose` method will be executed. So, let's define the composer:

	<?php namespace App\Http\ViewComposers;

	use Illuminate\Contracts\View\View;
	use Illuminate\Users\Repository as UserRepository;

	class ProfileComposer {

		/**
		 * The user repository implementation.
		 *
		 * @var UserRepository
		 */
		protected $users;

		/**
		 * Create a new profile composer.
		 *
		 * @param  UserRepository  $users
		 * @return void
		 */
		public function __construct(UserRepository $users)
		{
			$this->users = $users;
		}<commit_msg>More view work.<commit_after># Views

- [Basic Usage](#basic-usage)
- [View Composers](#view-composers)

<a name="basic-usage"></a>
## Basic Usage

Views contain the HTML served by your application, and serve as a convenient method of separating your controller and domain logic from your presentation logic. Views are stored in the `resources/views` directory.

A simple view looks like this:

	<!-- View stored in resources/views/greeting.php -->

	<html>
		<body>
			<h1>Hello, <?php echo $name; ?></h1>
		</body>
	</html>

This view may be returned to the browser like so:

	$router->get('/', function()
	{
		return view('greeting', ['name' => 'James']);
	});

As you can see, the first argument passed to the `view` helper corresponds to the name of the view file in the `resources/views` directory. The second argument passed to helper is an array of data that should be made available to the view.

#### Passing Data To Views

	// Using conventional approach
	$view = view('greeting')->with('name', 'Victoria');

	// Using Magic Methods
	$view = view('greeting')->withName('Victoria');

In the example above the variable `$name` would be accessible from the view, and would contain `Victoria`.

If you wish, you may pass an array of data as the second parameter given to the `make` method:

	$view = view('greetings', $data);

#### Sharing Data With All Views

Occasionally, you may need to share a piece of data with all views that are rendered by your application. You have several options: the `view` helper, the `Illuminate\Contracts\View\Factory` [contract](/docs/master/contracts), or a wildcard [view composer](#view-composers).

First, using the `view` helper:

	view()->share('data', [1, 2, 3]);

> **Note:** When the `view` helper is called without arguments, it returns an implementation of the `Illuminate\Contracts\View\Factory` contract.

Alternatively, obtain an instance of the `Illuminate\Contracts\View\Factory` [contract](/docs/master/contracts). Once you have an implementation of the contract, you may use the `share` method to make data available to all views:

In this example, we'll assume we're sharing the data from within a [global HTTP filter](/docs/master/filters). However, you could also share the data from a service provider, or even a controller:

	<?php namespace App\Http\Filters;

	use Illuminate\Http\Request;
	use Illuminate\Contracts\View\Factory as ViewFactory;

	class TestFilter {

		/**
		 * The view factory implementation.
		 */
		protected $view;

		/**
		 * Create a new filter instance.
		 *
		 * @param  ViewFactory  $view
		 * @return void
		 */
		public function __construct(ViewFactory $view)
		{
			$this->view = $view;
		}

		/**
		 * Run the request filter.
		 *
		 * @param  Request  $request
		 * @return mixed
		 */
		public function filter(Request $request)
		{
			$this->view->share('data', [1, 2, 3]);
		}

	}

#### Determining If A View Exists

If you need to determine if a view exists, you again have two options: the `view` helper and the `Illuminate\Contracts\View\Factory` [contract](/docs/master/contracts):

Using the helper:

	if (view()->exists('emails.customer'))
	{
		//
	}

Alternatively, type-hint the `Illuminate\Contracts\View\Factory` contract and use the `exists` method on the resolved instance:

	<?php namespace App\Services;

	use Illuminate\Contracts\View\Factory as ViewFactory;

	class TaskRunner {

		/**
		 * The view factory implementation.
		 */
		protected $view;

		/**
		 * Create a new class instance.
		 *
		 * @param  ViewFactory  $view
		 * @return void
		 */
		public function __construct(ViewFactory $view)
		{
			$this->view = $view;
		}

		/**
		 * Do some work!
		 *
		 * @return void
		 */
		public function performTask()
		{
			if ($this->view->exists('emails.customer'))
			{
				//
			}
		}

	}


<a name="view-composers"></a>
## View Composers

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want bound to a certain view each time that view is rendered, a view composer can organize that code into a single location.

#### Defining A View Composer

Let's organize our view composers within a [service provider](/docs/master/providers). We'll need an instance of the `Illuminate\Contracts\View\Factory` [contract](/docs/master/contracts), so we'll type-hint that in our provider's `boot` method:

	<?php namespace App\Providers;

	use Illuminate\Support\ServiceProvider;
	use Illuminate\Contracts\View\Factory as ViewFactory;

	class ComposerServiceProvider extends ServiceProvider {

		/**
		 * Register bindings in the container.
		 *
		 * @return void
		 */
		public function boot(ViewFactory $view)
		{
			$view->composer('profile', 'App\Http\ViewComposers\ProfileComposer');
		}

	}

> **Note:** Laravel does not include a default directory for view composers. You are free to organize them however you wish.

Now that we have registered the composer, the `ProfileComposer@compose` method will be executed each time the `profile` view is being rendered. So, let's define the composer class:

	<?php namespace App\Http\ViewComposers;

	use Illuminate\Contracts\View\View;
	use Illuminate\Users\Repository as UserRepository;

	class ProfileComposer {

		/**
		 * The user repository implementation.
		 *
		 * @var UserRepository
		 */
		protected $users;

		/**
		 * Create a new profile composer.
		 *
		 * @param  UserRepository  $users
		 * @return void
		 */
		public function __construct(UserRepository $users)
		{
			// Dependencies automatically resolved by service container...
			$this->users = $users;
		}
{'fraction_non_alphanumeric': 0.11175020542317174, 'fraction_numerical': 0.001460786998995709, 'mean_word_length': 3.7073485174043834, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1803522', 'n_tokens_mistral': 3432, 'n_tokens_neox': 3100, 'n_words': 1220}