Twig Integrity
NotJustPizza opened this issue · comments
NotJustPizza commented
How to use Stolz/Assets with TwigBridge? {{ Assets::css() }} doesn't work.
Unexpected token "punctuation" of value ":" ("end of print statement" expected) in "/var/www/MySuperWebsite/resources/views/home/base.twig" at line 16.
NotJustPizza commented
I'm currently using own TwigBridge extension:
<?php
namespace App\Extensions\Twig;
use Stolz\Assets\Laravel\Facade as Assets;
class TwigAssets extends \Twig_Extension
{
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('assets_css', [$this, 'assets_css']),
new \Twig_SimpleFunction('assets_js', [$this, 'assets_js']),
);
}
public function assets_css()
{
return Assets::css();
}
public function assets_js()
{
return Assets::js();
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'Stolz/Assets Integration';
}
}
Javi commented
I'm not familiar with Twig so I can help you with that but what you could do is use a ViewComposer to automatically send the values of the Assets::css()
and Assets::js()
functions to your layout view:
<?php
namespace App\ViewComposers;
use Illuminate\Contracts\View\View;
class AssetsComposer
{
public function compose(View $view)
{
$view->with([
'js' => Assets::js(),
'css' => Assets::css(),
]);
}
}
To register your view composer use an existing Service provider or create your own:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composers([
'App\ViewComposers\AssetsComposer' => 'layouts.master',
]);
}
public function register()
{
//
}
}
Then in your Twig layout just print the 'css' and 'js' variables.