Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID Sign In with Twitter

In this Discussion

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 Questions

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
    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);
    
    whu606peregrine

Answers

  • 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.


    factoid: Most questions have been previously answered, try the search box first, please provide your Vanilla version Number!

    Peregrine's Addons - donations gladly accepted for "successful solutions" and addons - kind of like tipping a waiter at a restaurant

  • 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");

    peregrine
  • nice followup.


    factoid: Most questions have been previously answered, try the search box first, please provide your Vanilla version Number!

    Peregrine's Addons - donations gladly accepted for "successful solutions" and addons - kind of like tipping a waiter at a restaurant

Sign In or Register to comment.