简体   繁体   English

ModX revo 2.2x:如何使用电视值设置tpl?

[英]ModX revo 2.2x: How to use a TV value to set tpl?

I'm trying to figure out a way to render a resource with a specific tpl based on a template variable(tv) value. 我试图找出一种基于模板变量(tv)值使用特定tpl呈现资源的方法。 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). 我已经为带有主页和博客的基本站点设置了modx安装(我使用了文章附加组件)。 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. 根据我的经验,最简单的方法是使用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. 但是对于这个特定的项目,我想根据每个结果的tv值更改getResources结果的tpl。

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... 如果您选择“文本”,它将具有tplA,如果您选择“多媒体”,则将具有tplB等。

now based on my research you would use the properties 'tplCondition' and 'conditionalTpls' to achieve this in your getResources call, something like this: 现在根据我的研究,您可以使用属性“ tplCondition”和“ conditionalTpls”在您的getResources调用中实现此目标,如下所示:

[[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. -使用模板变量值基于该值设置特定的tpl。

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. 您可以使用CSS完成此操作。 in the tpl, you do something like: 在tpl中,您可以执行以下操作:

<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. 这会将在电视中选择的帖子类型输出为实际使用的元素类,然后您可以使用CSS根据电视为输出提供不同的外观。 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. 在我的情况下,它100%解决了我的问题,但是,如果您需要使用html,我想可能需要使用javascript或使用php的其他方法。 just leaving this here in case someone needs it! 只是把它留在这里,以防有人需要!

If you need to do major changes to the html you could still probably solve it with css if you have a nice markup. 如果您需要对html进行重大更改,并且您的标记不错,您仍然可以使用css解决它。 However if thats not enough i would solve it by using one tpl for the getresources call, and letting that tpl handle the switching. 但是,如果那还不够,我可以通过对getresources调用使用一个tpl并让该tpl处理切换来解决。 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. 如果您可以让电视输出该值,则表示在每种情况下都应使用的tpl的名称,则您的tpl仅需要以下内容: [[$[[+tv.yourtv]]]]您的电视将首先被评估,然后将其作为大块进行处理。 That way you dont need a nasty switch or if-clause. 这样,您就不需要麻烦的开关或if子句。

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! 假设您想要完全不同的tpl,而不仅需要更改标准tpl的一小部分,在那种情况下,您仍然可以使用此技术!

Conditional templates seem to work fine with pdoResources (part of pdoTools). 条件模板似乎可以与pdoResources(pdoTools的一部分)配合使用。 (I have to put "tv." in front of the TV in the tplCondition but not in the other TVs) (我必须在tplCondition的电视前面放“电视”,而不要在其他电视上放)

James is correct. 詹姆斯是正确的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM