Pdotools load tpl by url-param

Hello,

is it possible to load a different template by url parameter?

www.my-domain.de/article >standard template
www.my-domain.de/article?special=1 >special template

Goal: for marked special content in individual branches marked by url params show a different offer

like this?

[[pdoResources?
&parents=myid
&tpl=[[url-param:promo=1:then=special-tpl:else=standard-tpl`
]]

thx
chris

This might work:

&tpl= `[[getUrlParam]]`
/* getUrlParam snippet */
$param = $modx->getOption('promo', $_GET, 0, true);
return $param? 'special-tpl' : 'standard-tpl';

You may run into trouble with the results being cached. If that’s the case, try calling both the snippet and pdoResources uncached.

An alternative would be to call a snippet higher up on the page that set a placeholder based on the URL param and use a placeholder tag for that placeholder as the value of the &tpl property. That might avoid any caching issues.

In any case, the same Tpl chunk will be used for all resources aggregated by pdoResources. I assume that’s what you want.

You could switch the page template with the SwitchTemplate extra. Would that cover your use case?

Hey Guys,

thx. I Try to work with bobray’s solution. On my first try it loads every time the same TPL.
Tomorow with a pack of Sleep, i will try it again. Thy So much, i will give you a feedback

greetings
chris

Make sure that you call pdoResources and the getUrlParam snippet uncached. Otherwise the template switch is not possible.

Hello,

i create a snippet like the idear fron Bobray

<?php
/* getUrlParam snippet */
$param = (int)$modx->getOption('promo', $_GET, 0, true);

if ($param == 1) {
    echo $modx->getObject('modResource', 188)->getContent();
} else {
    echo $modx->getObject('modResource', 189)->getContent();
}

In my Resources i use ContentBlocks a take the Snippet-Element. Here i vahe the Option for use Cache or not. worked perfect.

thanx, greetings and stay healthy
chris

don’t echo, but return the result

<?php
/* getUrlParam snippet */
$param = (int)$modx->getOption('promo', $_GET, 0, true);
$result = '';
if ($param == 1) {
    $result = $modx->getObject('modResource', 188)->getContent();
} else {
    $result = $modx->getObject('modResource', 189)->getContent();
}
return $result;