Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How can you declare a new asset in Vanilla?

whu606whu606 I'm not a SuperHero; I just like wearing tights... MVP
edited June 2012 in Vanilla 2.0 - 2.8

Vanilla obviously has pre-defined assets, such as Head, Content, Panel, etc.

How can I declare a new asset, so that I can use it in my Vanilla site?

Tagged:

Best Answer

  • ToddTodd Chief Product Officer Vanilla Staff
    Answer ✓

    Okay, so I assume you know how to render assets?

    Using smarty:

    {asset name="MyAsset"}
    

    Using php:

    $this->RenderAsset('MyAsset');
    

    So in your controller method you just need to add a string or Gdn_Module asset:

    $this->AddAsset("MyAsset", "Hello World!");
    

    or

    $Module = new Gdn_SomeModule();
    $this->AddAsset("MyAsset", $Module);
    

Answers

  • ToddTodd Chief Product Officer Vanilla Staff
    Answer ✓

    Okay, so I assume you know how to render assets?

    Using smarty:

    {asset name="MyAsset"}
    

    Using php:

    $this->RenderAsset('MyAsset');
    

    So in your controller method you just need to add a string or Gdn_Module asset:

    $this->AddAsset("MyAsset", "Hello World!");
    

    or

    $Module = new Gdn_SomeModule();
    $this->AddAsset("MyAsset", $Module);
    
  • whu606whu606 I'm not a SuperHero; I just like wearing tights... MVP

    @Todd

    Thanks for getting back so quickly!

  • thanks @Todd. nice to see these pearls of wisdom every once in a while - it gives us inspiration :) and helps us understand the inner workings better.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • whu606whu606 I'm not a SuperHero; I just like wearing tights... MVP

    I wasn't quite sure how to implement Todd's suggestion, but in searching how to, I found this code snippet which did what I was looking for

    public function AddAsset($AssetContainer, $Asset, $AssetName = 'NEWASSETNAME') {
          if (is_object($AssetName)) {
             return FALSE;
          } else if ($AssetName == 'NEWASSETNAME') {
             $this->Assets[$AssetContainer][] = $Asset;
          } else {
             if (isset($this->Assets[$AssetContainer][$AssetName]))
                $this->Assets[$AssetContainer][$AssetName] .= $Asset;
             else
                $this->Assets[$AssetContainer][$AssetName] = $Asset;
          }

    which I added in to my plugin.php file.

  • whu606whu606 I'm not a SuperHero; I just like wearing tights... MVP

    For future reference, Todd's solution was as simple as adding the line he suggested after

    public function Base_Render_Before($Sender) {

    My plugin (modeled on the Adding some jQuery to a plugin example in the Wiki)

    already had

    public function Base_Render_Before($Sender) { $this->AddJsCss($Sender);

    so all I needed to do was add the lines there:

    public function Base_Render_Before($Sender) { $this->AddJsCss($Sender); $this->AddAsset("MyAsset", "Category"); $this->AddAsset("MyAsset", "Table");

  • nice followup.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

Sign In or Register to comment.