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 = [

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

        $this->css = [


And I can register it with in my controller without any problems

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

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

  • 1
    A case-sensitivity issue maybe? Try {% do view.registerAssetBundle("secondred\\formbuilder\\assetbundles\\Formbuilder\\FormbuilderAsset") %} – Brad Bell Oct 25 '17 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 – Robin Schambach Oct 26 '17 at 9:19
  • Yup... added a note to update it. Would you mind adding that as an official answer? – Brad Bell Oct 26 '17 at 23:04

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)


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>
    {% block head %}
    <meta charset="UTF-8"/>
    {{ head() }} {# css from AssetBundle #}
    {# {% do view.registerCssFile( "url") %} #} {# Your custom css #}
    {% endblock %}
    {{ beginBody() }} {# js from AssetBundle #}
        {% block content %} 
            Hello world! 
        {% endblock %}
    {# {% do view.registerJsFile( "url") %} #}{# Your custom js #}
    {{ endBody() }} {# js from AssetBundle #}

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, privacy policy and cookie policy

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