terreporter / stringbladecompiler Goto Github PK
View Code? Open in Web Editor NEWRender Blade templates from string
License: MIT License
Render Blade templates from string
License: MIT License
Not sure if this is intended, but livewire components aren't working.
{{ view(["template" => "<livewire:my-component />"], ["test" => "ABC"]) }}
I am getting "Undefined variable: _instance". Same livewire component inside a regular blade.php template is working.
A solution I found is:
$parsedContent = \View::make(['template' => Blade::compileString($this->content)], ['token' => 'I am the child template'])->render();
Hi,
I am trying to use this package in a Resource like below
$this->content has a custom directive:
<p>@tv('test') Error voluptate</p>
The blade directive is:
Blade::directive('tv', function ($tv) {
return "<?php echo $tv . '-works'; ?>";
});
What would it take to have something like this working:
'parsedContent' => StringBlade::compileString($this->content)
Thank you.
class ResourceResource extends JsonResource
{
public function toArray($request)
{
return array_merge(
parent::toArray($request),
[
'template' => $this->template,
'type' => $this->type,
'parsedContent' => StringBlade::compileString($this->content)
]
);
}
}
I am using the new version 3.6.0
When returning a view in a controller:
return view (['template' => '{{$token}}'], ['token' => 'I am the child template']);
Works fine, but when trying to create a Mailable:
return $this->view (['template' => '{{$token}}'], ['token' => 'I am the child template']);
Creates an empty mail. When I set a file as template, it works fine.
Any plans to support Laravel 5.5?
This seems to work:
3.4...IT-swap:3.5
but it's probably a good idea to create 3.5 branch, but Pull Request doesn't seem to allow to do that
In Laravel 5 I'm getting the following error (obviously I understand why this is appearing)
FatalErrorException in StringView.php line 68:
Class 'StringCompilerException' not found in StringView.php line 68
I installed spatie/laravel-ray
which has a @ray()
directive for debug output, however the only way I can get this directive to work is by:
spatie/laravel-ray
to my composer.json
's dont-discover
: "extra": {
"laravel": {
"dont-discover": [
"spatie/laravel-ray"
]
}
},
StringBladeCompilerServiceProvider
to my app.php
directly after the standard ViewServiceProvider
(and I comment it out too because it seems to be more consistent that way):// Illuminate\View\ViewServiceProvider::class,
\Wpb\String_Blade_Compiler\StringBladeServiceProvider::class,
RayServiceProvider
at the end of my app.php
php artisan view:clear
FYI. Just forked and bumped dep to Laravel 7, seems to work fine! 👍
I have been very much helped by this library for some time.
I also want to use string-blade-compiler in my laravel9 project.
Do you have any plans for support?
Hello,
I can't install it...
composer require "wpb/string-blade-compiler"
Using version ^3.5 for wpb/string-blade-compiler
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for wpb/string-blade-compiler ^3.5 -> satisfiable by wpb/string-blade-compiler[3.5.1].
- Conclusion: remove laravel/framework v5.5.14
- Conclusion: don't install laravel/framework v5.5.14
- wpb/string-blade-compiler 3.5.1 requires illuminate/view 5.1.* -> satisfiable by illuminate/view[v5.1.1, v5.1.13, v5.1.16, v5.1.2, v5.1.20, v5.1.22, v5.1.25, v5.1.28, v5.1.30, v5.1.31, v5.1.41, v5.1.6, v5.1.8].
- don't install illuminate/view v5.1.1|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.13|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.16|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.2|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.20|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.22|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.25|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.28|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.30|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.31|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.41|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.6|don't install laravel/framework v5.5.14
- don't install illuminate/view v5.1.8|don't install laravel/framework v5.5.14
- Installation request for laravel/framework (locked at v5.5.14, required as 5.5.*) -> satisfiable by laravel/framework[v5.5.14].
Installation failed, reverting ./composer.json to its original content.
Hi,
it seems that here haven't been implemented all methods and when I am calling @includeWhen from my blade template, I am getting this error.
Could you have a look?
Update: I have added the renderWhen function from Iluminate/View/Factory class and everythign works just great.
Could you add to the repository please?
Are you planning to upgrade this package to support Laravel 5.6 version ?
This package is great -- however, I hit a snag when trying to upgrade to Laravel 6.x:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Root composer.json requires wpb/string-blade-compiler 4 -> satisfiable by wpb/string-blade-compiler[4.0.0].
- wpb/string-blade-compiler 4.0.0 requires laravel/framework 6.0.* -> found laravel/framework[v6.0.0, ..., v6.0.4] but the package is fixed to v6.20.12 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.
The only way to install it is to downgrade from Laravel v6.20.12 to Laravel v6.0.4, which I can't do because it will cause conflicts with some of my other vendor packages.
EDIT: When I tried upgrading to v5.0, composer installs as expected, though I'm seeing errors... will follow up after testing.
Hello all,
I am trying to run one of your basic examples -
return view (['template' => '{{$token}}'], ['token' => 'I am the token value']);
And no matter what I tried, I keep getting this error which resolved in vendor/wpb/string-blade-compiler/src/Factory.php:114
protected function stringViewInstance($view, $data)
{
return new StringView($this, $this->engines->resolve('stringblade'), $view, null, $data);
}
Hey,
Great package :) How I can add support for custom directive?
The directive registered at AppServiceProvider in the Boot function.
Blade::directive('flash', function ($expression) {
if (str_contains($expression, ',')) {
$expression = collect(explode(',', $expression))->map(function ($item) {
return trim($item);
});
return "<?php if (isset({$expression->get(0)}) && (bool) {$expression->get(0)} === true) : ?>".
"<?php echo {$expression->get(1)}; ?>".
'<?php endif; ?>';
}
return "<?php if (isset({$expression}) && (bool) {$expression} === true) : ?>";
});
Thanks!
Could we get the change in #35 in a release for the 3.5 version branch please?
Is this package supporting composer 2? If not, Do you consider to support it?
Thanks.
Plans to support Laravel 5.7?
Hi
Is it possible to extend a layout from string.
That is providing a layout as string and extending from it
Hi terreporter, great package! There's a problem i'm facing and don't know how to solve it.
I'm using compiled strings in my mailables like this:
$template->body = view (['template' => $template->body], ['member' => $member->toArray()]);
this works fine when sending mail like this:
Mail::to($member->email)->send(new MemberMail($member));
however the variables in the template are not rendered when sending the mail using queues like this
Mail::to($member->email)->queue(new MemberMail($member));
Any solutions?
Hi,
I have started using this extension as a base for this one that I'm currently writing: https://github.com/delatbabel/viewpages
I have a question relating to your build, I'm currently using the 3.0 branch. Maybe I'm missing something so perhaps you can explain it to me a bit better.
Why aren't you making your classes extend the Laravel base classes and use the functionality that's already there? For example your Wpb\String_Blade_Compiler\Factory class could extend Illuminate\View\Factory and only change what is required.
If you did extend then, for example, your make() function could look like this:
if (is_array($view)) {
$engine = $this->engines->resolve('stringblade');
$data = array_merge($mergeData, $this->parseData($data));
$this->callCreator($view = new StringView($this, $engine, $view, 'not-used', $data));
return $view;
}
// else
return parent::make($view, $data, $mergeData);
... thus saving about half of the code that you need to create.
It seems so obvious to me that I think that I'm missing something as to why this can't be done.
Hello,
Do you plan a version for Laravel 5.5 of your component ?
Best regards,
Dominique
"wpb/string-blade-compiler": "^3.5.5",
Laravel 5.5
Change app.php: remove Illuminate\View\ViewServiceProvider::class, and add Wpb\String_Blade_Compiler\ViewServiceProvider::class,
result:
Class view does not exist
Every thing is in the title, since namespace is namespace Wpb\String_Blade_Compiler\Facades;
the folder should be named Facades
Just a note, here as a submitted patch has not been added to the main repo.
Related to StringBladeCompiler version 3.0 and 3.2.
If you use https://github.com/barryvdh/laravel-debugbar with StringBlade, you will get an error - "realpath() expects parameter 1 to be valid path, object given"
This is because the debugbar expects the view it receives to be a file string.
To fix you will need to manually update your vendor package (src/DataCollector/ViewCollector.php), at least until the submitted patch is accepted (if that ever happens).
See barryvdh/laravel-debugbar#422 for code changes.
Hi, we are using your package for sending email using templates that are entered using the database. However, these views will always be different and our views directory is getting huge, and it's starting to give problems for the server.
Can you disable the creating of this compiled view file, or return the view path so we can immediately delete it, or create an option to delete it after usage.
After updating laravel I've got this error:
Call to undefined method Wpb\String_Blade_Compiler\Factory::startPush() in [project_root]/storage/framework/views/956a4ed4204425c97c4b7ea75a71ed70b9dcbd67.php on line 3
After some digging around I've found this pull request in laravel repos: laravel/framework#12808
If you look at the file changes of this pull request: https://github.com/laravel/framework/pull/12808/files. There are four methods added: startPush, stopPush, extendPush and yieldPushContent.
These methods needs to be added to the factory. Otherwise you can't update laravel.
Will you provide support for Laravel 8?
I'm currently unable to upgrade, since this library requires Laravel 6 or 7.
Hi there,
I have added this package to my project successfully:
When I try and render the example though I get:
return view (['template' => '{{$token}}'], ['token' => 'I am the token value']);
Error: strpos() expects parameter 1 to be string, array given
...
vendor/laravel/framework/src/Illuminate/View/ViewName.php:17
It looks to me like the package is not auto loading and replacing the view class?
If I run composer dump-autoload I get:
Deprecation Notice: Class Wpb\String_Blade_Compiler\Facades\StringBlade located in ./vendor/wpb/string-blade-compiler/src/Facade/StringBlade.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///usr/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:185
Any help much appreciated.
After installing multiple versions of this package with Laravel 6, I cannot seem to get it to work.
As stated in the docs I've most recently tried version 4 of this package for laravel 6.
Trying the example:
// string blade template load return view (['template' => '{{$token}}'], ['token' => 'I am the token value']);
I am confronted with the error from the title. Some other issues seem to be open with this error as well, however there they seem to occur on laravel 7
Any ideas on how to patch this?
when update to 5.0.1 has this errors:
Hello!
Just a friendly invitation to activate https://styleci.io for your project.
I have followed installation steps, but not work. Someone help pls?
When we use:
Blade::directive('block', function($expression) {
return "<?php echo "Hello world"; ?>";
});
And have @block() in our string template, we import with.
$view = view(['template' => $template])->with($variables);
It does not seem to know the directive.
Even if we dd() inside the compileStatements function we get a empty $customDirectives.
Do i need to register the directive in a other way?
How do you get the blade::extend to work?
I have a file containing several Blade:extend that is loaded in my global.php.
I've also tried to do it in my controller juste above the call to StringView::make but it doesn't take the blade extends into account.
Note that it works fine when working with the default Blade compiler...
Thx!
All i did was to following the instructions and receive the following error:
Method Wpb\String_Blade_Compiler\StringView::execute does not exist.
How can I get around this?
StringView::make(
array(
// this actual blade template
'template' => '{{ $img-src-one }}',
'cache_key' => "somename",
'updated_at' => 0
),
array( "img-src-one" => "TEST")
);
Is the exact code I am using to test this, The error returned (fairly useless) is:
FatalErrorException in Editorial.php line 0: Method Wpb\StringBladeCompiler\StringView::__toString() must not throw an exception
in Editorial.php line 0
at HandleExceptions->fatalExceptionFromError(array('type' => '1', 'message' => 'Method Wpb\StringBladeCompiler\StringView::__toString() must not throw an exception', 'file' => '/home/vagrant/Code/app/Editorial.php', 'line' => '0')) in HandleExceptions.php line 116
at HandleExceptions->handleShutdown()
I am using Laravel 5.0 with branch 2.0
How do you setup an run the test?
composer install
composer require -dev phpunit:^8
vendor/bin/phpunit
PHP Warning: require(/Users/jscharf/Workspace/test/StringBladeCompiler-fork/tests/../../../vendor/autoload.php): failed to open stream: No such file or directory in /Users/jscharf/Workspace/test/StringBladeCompiler-fork/tests/bootstrap.php on line 15
tests/bootstrap.php
changing require __DIR__.'/../../../vendor/autoload.php';
-> `require DIR.'/../vendor/autoload.php';vendor/bin/phpunit
HP Fatal error: Uncaught Error: Class 'Wpb\String_Blade_Compiler\Tests\Blade\AbstractBladeTestCase' not found in /Users/jscharf/Workspace/test/StringBladeCompiler-fork/tests/View/Blade/BladeAppendTest.php:5
Are the tests up tp date?
How am I supposed to run them?
just want to know when will b release of wpb new version for laravel 5.8
hi,
I tried to build a mail the way you described in #21, but I get the error:
Symfony\Component\Debug\Exception\FatalErrorException: Declaration of Wpb\String_Blade_Compiler\Compilers\StringBladeCompiler::compile($viewData) must be compatible with Illuminate\View\Compilers\BladeCompiler::compile($path = NULL) in /.../vendor/wpb/string-blade-compiler/src/Compilers/StringBladeCompiler.php:8
Stack trace:
#0 {main}
I couldn't figure out the problem as the stack trace wasn't helpful.
Hello,
In an ordinary controller, this works fine, but when I call the exact same code in a Mailable class, it doesn't do the substitution, as:
$results = view(['template' => $body], ['unsubscribe' => 'click this link to unsubscribe']);
Any ideas?
Thanks
Hello,
I'm attempting to install the version 3.7 branch from Composer but I notice that on Packagist, only up to version 3.6 is included.
Is there a way to push the latest versions to Composer/Packagist? Thanks in advanced!
Best,
Ian
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.