adamgaskins/view-string

This package is abandoned and no longer maintained. The author suggests using the Laravel 9's Blade::render() package instead.

The view() helper's little brother; a function to compile blade views from strings.

v1.3.0 2022-05-03 17:54 UTC

This package is auto-updated.

Last update: 2022-06-07 21:19:39 UTC


README

Notice: This package is obsolete as of Laravel 9, which includes this feature as part of the framework. view-string has been updated to work with Laravel 9, but migrating to use Blade::render() is extremely easy, so view-string will not be updated to keep up with future Laravel releases.

You are welcome to continue using, or to copy the class into your code if the "abandoned" packagist status annoys you.

screenshot.png

The view() helper's little brother; a function to compile blade views from strings.

68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6164616d6761736b696e732f766965772d737472696e672e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6164616d6761736b696e732f766965772d737472696e672f54657374733f6c6f676f3d476974687562267374796c653d666c61742d737175617265266c6162656c3d7465737473

Installation

You can install the package via composer:

composer require adamgaskins/view-string

Usage

This package allows you to compile a blade template dynamically from a string instead of it being hardcoded in the resources/views folder of your site. Usually this will look like a trusted user (developer/admin) using an embedded editor to write a blade template which is then saved to the database. You can then easily render the template like so:

$design = Design::find(1);

view_string($design->content, [ 'blogPost' => $post ]);

Or if you want to include it in your blade templates, there's a directive for that:

@includeString($design->content [ 'blogPost' => $post ])

Use view_string where you'd use view, and @includeString where you'd use @include.

eval

This package uses eval behind the scenes. It is not sandboxed. The goal of this package is to allow developers to write small blade snippets without needing to deploy. I am not responsible for what happens if you let random strangers on the internet run code on your servers.

We use this package at my company to allow us to quickly write tiny snippets of custom elements for our customers, saving us the need to do a full site deploy every time a user requests a change.

Testing

composer test

Related

  • wpb/string-blade-compiler: A great package that I used for years, and inspired this package. A little bit more opinionated though, the fact that it completely replaces Illuminate\View\View leads to issues in some random edge cases.

License

The MIT License (MIT). Please see License File for more information.