C
C
Cheizer2020-06-10 11:37:34
MODX
Cheizer, 2020-06-10 11:37:34

How to pass a variable (MODX) to a snippet?

Please tell me, I broke my whole head, it does not work. I want to pass a variable to a snippet that is in a loop.

There is a cycle, and a snippet is called in it

{set $tag = json_decode($row.tag, true)}
{foreach $tag as $idx => $row}

<div class="sidebar_info">
{'!pdoResources' | snippet : [
    'limit' => 1,
  'loadModels' => 'tagger',
  'class' => 'TaggerTagResource',
    'leftJoin' => '{
     "NameTag": {
      "class": "TaggerTag",
      "on": "TaggerTagResource.tag = NameTag.id"
    },
    "Posts": {
      "class": "modResource",
      "on": "TaggerTagResource.resource = Posts.id"
    },
    "intronews": {
    "class": "modTemplateVarResource",
    "on": "Posts.id = intronews.contentid and intronews.tmplvarid = 20"
      },
  "autor": {
    "class": "modTemplateVarResource",
    "on": "Posts.id = autor.contentid and autor.tmplvarid = 19"
     },
     "image": {
    "class": "modTemplateVarResource",
    "on": "Posts.id = image.contentid and image.tmplvarid = 1"
     }
  }',
  'select' => '{
  "TaggerTagResource": "*",
    "NameTag": "NameTag.tag, NameTag.alias",
    "Posts": "Posts.id, Posts.pagetitle",
    "intronews": "intronews.value as intronews",
    "autor": "autor.value as autor",
    "image": "image.value as image"
  }',
  'where' => '{"NameTag.alias" : $row.section}',
  'sortby' => 'createdon',
  'sortdir' => 'desc',
  'tplWrapper' => '@INLINE {$output}'
  'tpl' => 'news_block',
]}  

</div>
{/foreach}


There is a place in the snippet where the selection goes according to the condition, according to the tag.
'where' => '{"NameTag.alias" : $row.section}',

If you manually write the alias of the News tag, - news, everything works
'where' => '{"NameTag.alias" : "news"}',

. And if you substitute it from a variable, it doesn’t work, even though everything is contained in the $row.section variable.
'where' => '{"NameTag.alias" : $row.section}',

What am I doing wrong? How to pass information to a snippet in a variable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cheizer, 2020-06-10
@Cheizer

Phew, I fought for two days, I knew it, it's the FENOM syntax, it's difficult. The correct spelling is "'~$row.section~'"

'where' => '{"NameTag.alias" : "'~$row.section~'"}',

Maybe someone will come in handy.

A
Anton Tarasov, 2020-06-10
@an-tar

And if so:

{set $tag = json_decode($row.tag, true)}
{foreach $tag as $idx => $row}
{set $section = $row.section}
<div class="sidebar_info">
{'!pdoResources' | snippet : [
    'limit' => 1,
  'loadModels' => 'tagger',
  'class' => 'TaggerTagResource',
    'leftJoin' => '{
     "NameTag": {
      "class": "TaggerTag",
      "on": "TaggerTagResource.tag = NameTag.id"
    },
    "Posts": {
      "class": "modResource",
      "on": "TaggerTagResource.resource = Posts.id"
    },
    "intronews": {
    "class": "modTemplateVarResource",
    "on": "Posts.id = intronews.contentid and intronews.tmplvarid = 20"
      },
  "autor": {
    "class": "modTemplateVarResource",
    "on": "Posts.id = autor.contentid and autor.tmplvarid = 19"
     },
     "image": {
    "class": "modTemplateVarResource",
    "on": "Posts.id = image.contentid and image.tmplvarid = 1"
     }
  }',
  'select' => '{
  "TaggerTagResource": "*",
    "NameTag": "NameTag.tag, NameTag.alias",
    "Posts": "Posts.id, Posts.pagetitle",
    "intronews": "intronews.value as intronews",
    "autor": "autor.value as autor",
    "image": "image.value as image"
  }',
  'where' => '{"NameTag.alias" : $section}',
  'sortby' => 'createdon',
  'sortdir' => 'desc',
  'tplWrapper' => '@INLINE {$output}'
  'tpl' => 'news_block',
]}  

</div>
{/foreach}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question