2

How can I register an assetbundle in a frontend template? This is my bundle

namespace secondred\formbuilder\assetbundles\Formbuilder;

use Craft;
use craft\web\AssetBundle;
use craft\web\assets\cp\CpAsset;

class FormbuilderAsset extends AssetBundle
{
    // Public Methods
    // =========================================================================

    /**
     * Initializes the bundle.
     */
    public function init()
    {
        // define the path that your publishable resources live
        $this->sourcePath = "@secondred/formbuilder/assetbundles/formbuilder/dist";

        // define the dependencies
        $this->depends = [
            CpAsset::class,
        ];

        // define the relative path to CSS/JS files that should be registered with the page
        // when this asset bundle is registered
        $this->js = [
            'js/Formbuilder.js',
        ];

        $this->css = [
            'css/Formbuilder.css',
        ];

        parent::init();
    }
}

And I can register it with in my controller without any problems
$this->view->registerAssetBundle(FormbuilderAsset::class);

But when I do

{% do view.registerAssetBundle("secondred\\formbuilder\\assetbundles\\formbuilder\\FormbuilderAsset") %}

nothing happens.. no request, no error, nothing in network tab and no html is added to the page

Update
I found the solution. If you are in a frontend ressource you need to call the {{ endBody() }} method in order to get the bundles

3
  • 1
    A case-sensitivity issue maybe? Try {% do view.registerAssetBundle("secondred\\formbuilder\\assetbundles\\Formbuilder\\FormbuilderAsset") %}
    – Brad Bell
    Oct 25, 2017 at 23:35
  • Thanks Brad, but I found the solution myself.. it was because I didn't call the {{ endBody() }} in my frontend template. Maybe it is worth mentioning in the documents? github.com/craftcms/docs/blob/master/en/asset-bundles.md Oct 26, 2017 at 9:19
  • Yup... added a note to update it. Would you mind adding that as an official answer?
    – Brad Bell
    Oct 26, 2017 at 23:04

2 Answers 2

0

Answer:
In order to render all the scripts in your registered assetbundles you need to call this function

{{ endBody() }}

somewhere in your page (preferred at the end of the body)

0

This question is related to YII2 Advanced Template: https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/views/layouts/main.php

Frontend Twig layout template

<!DOCTYPE html>
<html>
<head>  
    {% block head %}
    <meta charset="UTF-8"/>
    <title>Title</title>
    {{ head() }} {# css from AssetBundle #}
    {# {% do view.registerCssFile( "url") %} #} {# Your custom css #}
    {% endblock %}
</head>
<body>
    {{ beginBody() }} {# js from AssetBundle #}
        {% block content %} 
            Hello world! 
        {% endblock %}
    {# {% do view.registerJsFile( "url") %} #}{# Your custom js #}
    {{ endBody() }} {# js from AssetBundle #}
</body>
</html>

In your plugin Controller use renderPageTemplate not only renderTemplate. The renderPageTemplate envelops renderTemplate using beginPage() and endPage(), see craft\web\View.php

The combination renderTemplate and {{endBody}} only generates in HTML <!CDATA ...> lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.