Roots Discourse

[Sage 10] How to use and create a component - Like, for example /app/View/Components/Alert.php

Hey all,

I was tinkering with making a custom component for product images. This should process some arguments. If I look at the file /app/View/Components/Alert.php I get the feeling that this is something I could use as a boilerplate.

From what I understand you can call it and pass 2 arguments, $type and message, then it grabs the view from components.alert and renders it.

The thing is that I don’t know how to call this Alert component and pass arguments from, for example, /resources/views./page.blade.php.

How would I accomplish this?

It’s always like this. You’re stuck for over a day, post a question and find the answer… <x-alert />
But then I copy the Alert.php files and rename the class to ProductImage And it does not render with <x-productimage /> I did a composer dump-autoload but no go.

The Blade documentation is a good place to start: https://laravel.com/docs/7.x/blade#components

I think the Acorn WP-CLI command also has a command to help you create components. If you run wp acorn it’ll list the available commands.

@alwaysblank. Thanks Ben, that gave all the missing info I needed.

One thing though. Even though I got it working a Sage 10 project folder is not able to call php artisan make:component Alert It has no artisan.

Acorn has wp acorn make:component myName It prints Component created successfully. But there is no trace of this newly created component. Not in the app/ and the resources/views/ folder. Not even id I search project wide for this name in camelCasing or kebab-casing. So where would it live.

If I re-run the command it says it already exists. Plus is there documentation on Acorn? It might give me hints on how to work with components. But I don’t see documentation on it.

Lately. I managed to create it manually by cloning the /app/View/Components/Alert.php Rename the file, class and view render in the return. Then create a custom view component in the /resources/views/components

Running wp acorn make:component TestComponent results in creating two files:

  • app/View/Components/TestComponent.php
  • resources/views/components/test-component.blade.php

I did the above on a Sage 10 site I’m working on, and got the above result.

If you’re trying to find files that have been created by an automated process, a good way to do that is to run git status: So long as you’ve been keeping up with your commits, it will clearly show the files that have just been added.

2 Likes

Son of a gun… I stand corrected. You are right, I’m sorry. I had two terminals open one for the parent theme and one for the child theme. Looks like I was working in the other terminal than expected. I found the files, yes. Thanks Ben, this makes it all a lot easier :slight_smile:

1 Like