Assets plugin and {assets:js} in markdown code lines

While writing a post about the use of the assets plugin I had to include the {assets:js} calls within the markdown code fences and the inline snippets… But, I think the plugin proccesses those tags and they (and their contents) won’t show on the code. How can I make it work? Is there a way to deactivate the plugin for that page or a solution?

Also, and while we are at it: when you make a {assets:js} call, how can you reference a js file that is located in the same page directory as the page where the call is? do you have to type all of the route to the file from the homepage, or is there a method to avoid that?

Thanks in Advance!

To be able to process the assets syntax and at the same time display it in code, I would use the Twig syntax to add the css/js to the page, and then disable the plugin for this page.

{% do assets.addAsyncJs('https://cdnjs.cloudflare.com/ajax/libs/amcharts/3.13.0/amcharts.js') %}

Currently you have to put the full path to the file. The plugin has no smarts to dynamically calculate the location of the page. However, since Grav 0.9.28, that logic is available in the Uri.php object, so it probably could do with an update to support this. I’ll add it to the list!

Thanks Rhukster! I disabled the plugin for the page and it worked perfectly.
Maybe you should add in the documentation of the plugin that you can do that by adding system.assets.enabled: false to the page headers for newbies like me :slight_smile:
The twig syntax was also a great help :smiley:

And thanks for adding the dynamic paths to the list! It would be great!

OK. I was wrong about the page headers. It was just that the assets plugin is not working for some reason most of the time… and I don’t have any idea why… :frowning:

How do you disable the plugin just for one page/post?

Hi @aRadionOff, as far as I can see you can only enable/disable Assets plugin completely. There are no checks or options for the page header. That’s why, the setting system.assets.enabled: false disabled the plugin.

However, if you can wait - I’m releasing a new plugin (Shortcodes) today, which will solve your problem easily without the need for Twig and such tricks. I will present a recipe here as soon as it is available.

In the meantime the only dirty trick I know is. Keep Assets plugin activated (here: delete your custom change). Then in your page enable Twig via process: twig: true. Then use

{{ '{assets' }}:js}

which fools the recognition although Assets is enabled. Basically you let Twig printing {assets and together with the rest it becomes {assets:js}.

See you later.

Hi @Sommerregen. You are releasing Shortcodes today? [clap, clap, clap] :smiley: That is great news! I can’t wait to try it. Thanks a lot for your reply! I will wait for the release then, because I am not really in such a hurry ;). BTW, this community is great and you guys are wonderful. I am learning a lot thanks to you!

And I’m learning from @rhukster :smiley: And yeah, I just noticed that @rhukster released a new version today with the changes that Shortcodes needs in order to work properly.

Ok, see you later.

I’m also going to update Assets plugin with some new features including the page path stuff, and options to disable on page. Shortcodes will be able to do this kinda of thing also, but Assets will remain targeted for those that need it.

Hi :-),

I just released my new Shortcodes plugin.

@rhukster I agree with you. The Assets plugin needs some new features and brought up to date and does it’s job quite well.

@aRadianOff You can now download my plugin and install it. As per your request the assets shortcode now also support relative paths, e.g. if you have a JS file hello.js located in the same directory as your page you can include it in the document with

{{% assets type="js" %}}
    hello.js
{{% end %}}

If you want to ignore a shortcode e.g. for showing it to the user just encloseit with the special shortcode {{% raw %}}...{{% endraw %}}. In your case, this is maybe you want

{{% raw %}}
'''
{{% assets type="js" %}} 
    hello.js
{{% end %}}
'''
{{% endraw %}}

There are lot of other features and all my plugins will be made “Shortcodes” compatible in the upcoming weeks. For a reference at the moment, please read the README.

FYI, Assets 1.2.0 has just been released also. Should show up in GPM within 30 minutes or so. It supports the pathing so all of these are valid:

{assets:css}
sampler.css
/sampler.css
/blog/some-post/sampler.css
//cdnjs.cloudflare.com/ajax/libs/1140/2.0/1140.css
http://somesite.com/js/cookies.min.css
{/assets}

first one will be from current page folder
second one would be from homepage folder
third is from some arbitrary page folder
third and fourth are remote URls (obviously)

Also you can disable for one page by putting this in the page header:

assets:
  enabled: false

Thank you both for your comments and help! I tried de Shorcodes plugin today (left an issue in the repository) and it works really well. The option to disable it in a page is great and solved the code-not-showing issue perfectly - haven’t tried the raw shortcode yet (@rhukster: I really think assets should have that option, too :slight_smile: for people who don’t need all the extra functionallity of shortcodes). I also figured out (I think) why assets and shortcodes did seem to work just when I made a modification to a page… I disabled the cache and it works fine now, I think I will have to try the advanced page cache to see if that one works for me. :slight_smile:

Ohhh… you were reading my mind… Thanks