Containerize your first Python project in minutes Docker is one of those environments that I can’t live without anymore. It’s interesting that there aren’t many resources that discuss getting a basic Python script going with Docker and docker-compose. Let’s see how simple this is to do with Python. Create a new directory for your project and navigate into it: mkdir python_test cd python_test Create a new file called Dockerfile with the following content: FROM python:3.11-slim-buster WORKDIR /app COPY requirements.txt ./ RUN pip install –no-cache-dir -r requirements.txt COPY . . CMD [ “python”, “app.py” ] This Dockerfile sets up a Python 3.11 environment,

The language that does it all I jumped into this Python tutorial series without explaining why one might want to learn Python in the first place. Python is a high-level, interpreted programming language that was first released in 1991 by Guido van Rossum. It is named after Monty Python, the British comedy group, which explains why many of its tutorials and examples use humorous references. https://www.dinocajic.com/python-p1-getting-started-with-python-and-docker-compose/ Python is designed to be easy to learn and read, with a syntax that emphasizes readability and reduces the cost of program maintenance. Its simple and elegant syntax has made it popular among beginners,

Compose complex layouts with multiple slots In the previous article, we looked at the $slot variable and how to inject content into our components. But what if our component was composed of multiple slots? Make different components you say? We could, but sometimes that’s just not practical. There can be over-segmentation. https://www.dinocajic.com/laravel-p69-blade-slots-intro/ To illustrate this concept, I’ll create a resources/views/components/details.blade.php file. The component will contain the details tag that’s standard with HTML. It just allows for the text to be clicked on and expanded. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details The structure of the details tag is as follows: <details> <summary>Details</summary> Something small enough to escape casual notice. </details> We would like to

Plug content into components with ease Aside from passing attribute data to blade components, sometimes you need to pass a large chunk of data. That data can be displayed through slots. Laravel renders the data by echoing out the $slot variable. https://www.dinocajic.com/laravel-p68-defining-custom-directives-in-blade/ We’ll start by creating a button component. All we want to be able to do with our button component is change the inner-text. It’ll be located in resources/views/components/button.blade.php. <button class=”btn”> {{ $slot }} </button> To use it, we’ll create a new Blade file: resources/views/blade-test/index.blade.php. <x-button> Test </x-button> We just need a route to display it. Route::get(‘/blade-slots’, function () { return view(‘blade-slots.index’); });

Extend Blade on your terms How many times have you wished that a specific directive existed in Blade? If the answer to that question is “many,” then do I have a surprise for you. You can create your own directives in Blade. First, you need to create a service provider using the php artisan make:provider command. This provider will register your Blade directives with Laravel. # php artisan make:provider CustomBladeServiceProvider INFO Provider [app/Providers/CustomBladeServiceProvider.php] created successfully. # <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class CustomBladeServiceProvider extends ServiceProvider { /** * Register services. * * @return void */ public function register() { // } /**

Embed logic directly into your HTML In the last article we covered the @class and @style directives to implement conditional styling in Blade. But, there are other HTML attribute directives that are present in Blade that we can utilize: @checked directive @selected directive @disabled directive @readonly directive @required directive @checked directive With the @checked directive, the radio button or checkbox will be checked if the condition is truthy. Route::get(‘/directive-test’, function () { return view(‘html-directives.index’, [‘isActiveVar’ => true]); }); The $isActiveVar variable is set to true and passed to the html-directives.index Blade file. <label for=”isActive”>Is Active?</label> <input type=”checkbox” id=”isActive” name=”isActive” value=”isActive” @checked( old(‘isActive’, $isActiveVar) ) /> Inside the Blade file, the @checked directive accepts an argument that will be truthy.

Style smart, render dynamic With Blade Components, conditional styling refers to the ability to add different styles to HTML elements based on certain conditions. Blade provides several conditional directives that can be used to accomplish this. The @if directive can be used to conditionally apply styles to an element based on a specific condition. For example, to apply a different background color to an element based on a variable value, you could use: @if ($showRed) <div style=”background-color: red;”> Red Background </div> @else <div style=”background-color: blue;”> Blue Background </div> @endif We can test this with the following route: Route::get(‘/conditional-styling’, function () { return

Environment checks made simple There comes a time when you want to use environmental variables in your Blade components. But, how do you do it? There are a couple of different ways. We’ll take a look at the env() function first and then the directive itself. https://www.dinocajic.com/laravel-p64-rendering-jason-in-blade/ Using the env() function Let’s open our .env file and take a look at some of the variables that are already defined there. APP_NAME=Laravel APP_ENV=local APP_KEY=base64:X2mYdNpqK29Z21Fb9Dx6PDa6Jwd6k4ThXuP+VYd9dco= APP_DEBUG=true APP_URL=http://laravel-tutorials.test LOG_CHANNEL=stack LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=laravel_tutorials DB_USERNAME=sail DB_PASSWORD=password We can access any of those variables with our env() function. Create a new Blade file and a route to access it.

Serve structured data straight from Blade Continuing with our previous theme on views, let’s get back into Blade. We frequently work with JavaScript and need to utilize the array data that’s sent from our Laravel application to our Blade component. How do you transform that data from a Laravel type array to a JSON file? https://www.dinocajic.com/laravel-p63-optimizing-views/ The first method is something that you’re most likely familiar with: json_encode. While this does work, there is a better way to do it and that’s utilizing the Js::from method. This method ensures that your JSON is properly escaped. If you’re running on a newer version of

Render faster, shine brighter We’re wrapping up our discussion on views and what better place to finish up on than optimizing our views. https://www.dinocajic.com/laravel-p62-view-composers/ Normally, Blade templates are compiled when requested by the user. Laravel will check to see if a compiled version of the view exists first, and if it does, it will send it, otherwise it will compile it and then send it. If a compiled version exists, Laravel will determine if the uncompiled version has modifications that are dated after the compiled version. If they are, Laravel will recompile the view before sending it. As you can