Certainly! To use Torchlight for syntax highlighting in your Laravel application, you'll need to follow these steps:
- Install the Torchlight Client: First, you need to install the Torchlight Laravel package via Composer. Run the following command in your terminal:
composer require torchlight/torchlight-laravel
- Publish the Config File: After installing the package, you should publish the configuration file with the following Artisan command:
php artisan vendor:publish --provider="Torchlight\TorchlightServiceProvider" --tag=config
-
Configure Your API Token: Sign up for a Torchlight account (if you haven't already) and obtain your API token. Then, add your Torchlight API token to your
.envfile:
TORCHLIGHT_TOKEN=your_api_token_here
- Use Torchlight in Your Views: In your Blade templates, you can now use the Torchlight directives to highlight your code snippets. Here's an example of how to use it:
@torchlightCode(['language' => 'php'])
// Your PHP code here
@endtorchlightCode
Replace 'php' with the appropriate language identifier for your code snippets.
- Cache Your Views: It's a good practice to cache your views for performance. You can do this with the following command:
php artisan view:cache
-
Include the Torchlight CSS: To style the highlighted code, you need to include the Torchlight CSS in your layout. You can add the following line in the
<head>section of your HTML:
<link href="https://unpkg.com/torchlight-api/torchlight.css" rel="stylesheet">
Here's a full example of how you might include a code snippet in a blog post:
@extends('layouts.app')
@section('content')
<article>
<h1>My Coding Blog Post</h1>
<p>Here's an example of a code snippet using Torchlight:</p>
@torchlightCode(['language' => 'javascript'])
// An example JavaScript function
function greet(name) {
console.log('Hello, ' + name + '!');
}
@endtorchlightCode
</article>
@endsection
Remember to include the Torchlight CSS link in your layout file.
- Clear Your Cache: If you make changes to your views or Torchlight configuration, you may need to clear your cache:
php artisan cache:clear
That's it! Your code snippets should now be beautifully highlighted using Torchlight in your Laravel application. If you encounter any issues, refer to the official Torchlight documentation for more detailed instructions and troubleshooting tips.