MODX Community

Use of class_key Tag

I’m trying to get the resource type on an individual resource so I can apply conditional modifier, and only load certain javascript on MoreGallery pages. I see the [[*class_key]] tag when hovering over the option in a resource’s settings, but it always outputs mgResource regardless of resource type. How would I get the resource type properly? Thanks!

1 Like

If I want to load something only on certain pages, it’s easier to just check [[*parent]] id and this will match all of its children.

If that’s not what you meant, then is mgResource a string? It kind of looks more like an object name, does it have properties? Can you var_dump it?

1 Like

Parent works for the blog, but not if the user wants to have a gallery resource somewhere else with a different template. That takes out the template tag and the parent tag as being a solution. I need flexibility for the user.

[[*class_key]] does output “mgResource” when I make it’s output visible on a page. So yes a string. As for var_dump, I don’t know what that means or how to do so.

1 Like

Hi @ShatteredSite,

[[*class_key]] should return the current page’s class key. Are you perhaps creating a list of other pages where you would need a placeholder instead? e.g. [[+class_key]]?

Would you be able to show the relevant code / markup you’re using?

1 Like

I’m not creating a list where I would need a placeholder like in getResources, I’m just inserting the [[*class_key]] tag to see it’s output. For right now I’m just throwing it into a standard page template in the page content (not richtext) to test what I can actually get from it. All I get is “mgResource” and nothing else.

1 Like

My guess is that MoreGallery uses a custom resource type with a class_key of mgResource. You can confirm this in the modx_site_content table in the DB. Those resources will have their class_key field set to mgResource.

Resources that are not part of a gallery should return modResource with that tag, unless they’re part of an Articles blog, in which case you’ll see Article as the class key.

I think what you want is a snippet named MoreGalleryJs placed in all templates used by a MoreGallery page.

Put this tag in the template(s):

[[MoreGalleryJs]]

and create a snippet called MoreGalleryJs with code something like this:

if ($modx->resource->get('class_key') === 'mgResource') {
    $modx->regClientStartupScript('[[++assets_url]]path/to/js/file.js');
}

You can also put the JS in a chunk and do this:

$modx->regClientStartupScript($modx->getChunk('ChunkName'));

BTW, if the More Gallery pages all have their own template that’s not used by any other pages, you can just put the usual JS tags in the template, or put the full JS code in the template itself.

2 Likes

Thanks Bob! I wasn’t expecting a string output at all, so it it was a tad confusing. Both the snippet and this work just fine.

[[*class_key:is=`mgResource`:then=``:else=`JS/Chunk Here`]]

It was a lot simpler than I thought. Thanks!

1 Like

The snippet will be faster because there’s no need to parse the output modifier string.

1 Like

Your right, it certainly is.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.