gantry5 icon indicating copy to clipboard operation
gantry5 copied to clipboard

Twig in Custom HTML Particles

Open AdamWarnock opened this issue 8 years ago • 11 comments

I'm still learning my way around Gantry 5 and I ran across an option in the custom HTML particle that allows Gantry to process twig code in the particle. I've looked, but I can't find anything on what's allowed or even how certain attributes can be included into the particle.

So, say I want to pull in the menu item title from a menu item in Joomla into the particle, how would I do that? Sorry if this is a dumb question. I'm just not familiar with Twig and how it works with Gantry.

AdamWarnock avatar Mar 14 '16 18:03 AdamWarnock

Allowing twig in custom html doesn't mean that you can access everything in the platform, but it does give you access on Gantry DI container and to some basic classes/functions of Joomla.

Here's short list:

{{ gantry.platform.call('pow', 2, 4) }}

{{ gantry.platform.filter('<p>my name is [[name]]. Calls content plug-ins.</p>') }}

{% set menu = joomla.factory('menu') %}

{% set me = joomla.factory('user') %}

{% set user = joomla.instance('user', 64) %}

{% set route = joomla.route('index.php?option=com_content') %}

{% do joomla.html('jquery.framework') %}
{% do joomla.html('jquery.ui', ['core']) %}

mahagr avatar Mar 15 '16 09:03 mahagr

I hadn't realized that I could set things myself. That makes things easier. If I may, is there a list of everything that you can have access to via Twig in the custom HTML particle?

And before I forget, thank you for your help. :)

Edit:

Correct me if I'm wrong, but it looks like joomla.factory('menu') would be JFactory::getApplication()->getMenu(); so, if I wanted the title of the active menu item, I'd have something like joomla.factory.menu.active('title') or is this totally wrong.

AdamWarnock avatar Mar 15 '16 14:03 AdamWarnock

right now there is no list, but I started to create one when you first asked about it.

All of the above should be pretty obvious; I don't think I need to give you JFactory functions or list all Joomla classes with getInstance(). Same is true for available PHP functions etc..

Every call also accepts the same parameters as the real functions.. All that you need to do is to just list them after the first parameter.

mahagr avatar Mar 15 '16 14:03 mahagr

No, I think I can find those. ;)

Thanks for your time and help. I look forward to seeing where Gantry goes from here. It is really a lot of fun to work with.

AdamWarnock avatar Mar 15 '16 14:03 AdamWarnock

Cc-ing @FrugalGeek for documenting this. Would be nice to get this list out of GitHub and into our docs.

w00fz avatar Apr 21 '16 04:04 w00fz

Was this ever done?

w00fz avatar May 26 '16 20:05 w00fz

+1

nicutor avatar Jul 08 '16 11:07 nicutor

Its not yet documented. I decided to concentrate on some simple fixes/improvements instead.

mahagr avatar Jul 08 '16 11:07 mahagr

Any update on documentation? I am very interested in being able to use {% set user = joomla.instance('user', 64) %}

But for some reason all it ever returns is the user 64, not the active logged in user. Am I missing something?

Your help is MUCH appreciated!

goldrush-bob avatar Mar 15 '17 19:03 goldrush-bob

You are using wrong call. In Joomla you can either load a fixed userid (by using above call) or the current user by using {% set user = joomla.factory('user') %}.

mahagr avatar Mar 20 '17 08:03 mahagr

right now there is no list, but I started to create one when you first asked about it.

Is there any progress in the documentation?

webarion avatar May 13 '23 07:05 webarion