Question

I'm trying to figure out a way to render a resource with a specific tpl based on a template variable(tv) value. here is my use case:

I have setup a modx installation for a basic site with a homepage and a blog (I used the articles add-on). I want to display blog posts on the home page, outside of the articles container. From my experience the easiest way to do this is with getResource. However for this specific project I would like to change the tpl of the getResources results based on whatever the tv value is for each result.

The template variable can be looked at as a "post type". if you choose "text" it would have tplA, if you choose "multimedia" it would have tplB etc...

now based on my research you would use the properties 'tplCondition' and 'conditionalTpls' to achieve this in your getResources call, something like this:

[[getResources?
  &tplCondition=`tv.blogPostType`
  &conditionalTpls=`{"1":"tplA","2":"tplB","3":"tplC"}`
  &tpl=`defaultTpl`
]]

The problem is, this does not seem to work with template variables :( it even says it only uses resource fields in the documentation.....which is a REAL bummer, as i have no idea to pull this off otherwise. Based on my limited knowledge, you can maybe create a snippet or something that does this, But i have no clue.

Does anyone have an elegant solution to this problem?

to sum up what im trying to do, again:

-assign a "post type" to blog posts in my articles container via template variable.

-use the template variable value to set a specific tpl based on that value.

any help is highly appreciated. thanks

EDIT: okay I got a reply on the forums and have come across a"solution" to this.

you can accomplish this with css. in the tpl, you do something like:

<article class="[[+tv.post-type]]">....</article>

this will output the post type selected in the tv as the actual element class used, and you can then use css to give the output different looks based on the tv. its actually so simple im a little embarrased i didnt see it before. In my situation it 100% solves my problem, however if you needed to chasnge html im guessing javascript would need to be involved or another method with php. just leaving this here in case someone needs it!

Was it helpful?

Solution

If you need to do major changes to the html you could still probably solve it with css if you have a nice markup. However if thats not enough i would solve it by using one tpl for the getresources call, and letting that tpl handle the switching. If you could have your TV output the value symbolize the name of the tpl that should be used in each case, your tpl would only need this: [[$[[+tv.yourtv]]]] Your tv will be evaluated first, and then it will be processed as a chunk. That way you dont need a nasty switch or if-clause.

This is assumin you want toally different tpls, and not just need to change some small part of the standard tpl, in that case you could still use this tecnique though!

OTHER TIPS

Conditional templates seem to work fine with pdoResources (part of pdoTools). (I have to put "tv." in front of the TV in the tplCondition but not in the other TVs)

James is correct.

[[pdoResources?
   &includeTVs=`blogPostType`
   &tplCondition=`tv.blogPostType`
   &conditionalTpls=`{"1":"tplA","2":"tplB","3":"tplC"}`
   &tpl=`defaultTpl`
]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top