Skip to content

Define custom template #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ return Invoice::make()->buyer($customer)->addItem($item)->stream();

After publishing assets you can modify or make your own template for invoices.

Templates are stored in the `resources/views/vendor/invoices/templates` directory. There you will find `default.blade.php` template which is used by default.
Templates are stored in the `resources/views/vendor/invoices/templates` directory by default. There you will find `default.blade.php` template which is used by default.

You can specify which template to use by calling `template` method on Invoice object.

Expand All @@ -217,6 +217,13 @@ Invoice::make('receipt')->template('my_company');

Too see how things work in a template you can view `default.blade.php` as an example.

You can also define your own template path in this way.

For example, if your application follows a modular architecture and stores resources in module-specific directories (e.g., `modules/users/resources/views/invoices/v1/simple.blade.php`), you can configure the path as follows:
```php
Invoice::make('receipt')->template('users::invoices.v1.simple', true);
```

## Config

``` php
Expand Down
9 changes: 3 additions & 6 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function __construct($name = '')
$this->name = $name ?: __('invoices::invoice.invoice');
$this->seller = app()->make(config('invoices.seller.class'));
$this->items = Collection::make([]);
$this->template = 'default';
$this->template = 'invoices::templates.default';

// Date
$this->date = Carbon::now();
Expand Down Expand Up @@ -269,8 +269,7 @@ public function render()

$this->beforeRender();

$template = sprintf('invoices::templates.%s', $this->template);
$view = View::make($template, ['invoice' => $this]);
$view = View::make($this->template, ['invoice' => $this]);
$html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');

$this->pdf = PDF::setOptions($this->options)
Expand All @@ -283,9 +282,7 @@ public function render()

public function toHtml()
{
$template = sprintf('invoices::templates.%s', $this->template);

return View::make($template, ['invoice' => $this]);
return View::make($this->template, ['invoice' => $this]);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Traits/InvoiceHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,16 @@ public function getCustomData()
/**
* @return $this
*/
public function template(string $template = 'default')
public function template(string $template = 'default', bool $from_default_resources = true)
{
if ($from_default_resources) {
$template = sprintf('invoices::templates.%s', $template);
}

$this->template = $template;

return $this;
}

/**
* @return $this
*/
Expand Down