getResources Modx return null? - modx

I am trying to access some TV from another resource using getResources, but nothing is returned.
Here is my code sample
[[getResources?
&resources=`13`
&tvPrefix=`tv.`
&tpl=`#CODE:[[+tv.DefaultEmail]]`
&showHidden=`1`
&includeContent=`1`
&includeTVs=`1`
&processTVs=`1`
]]

You have to use #INLINE for the template parameter (see #FILE and #INLINE tpls in https://rtfm.modx.com/extras/revo/getresources#getResources-TemplatingProperties)
&tpl=`#INLINE [[+tv.DefaultEmail]]`
But I would better install FastField to use the following tag syntax [[#13.DefaultEmail]].

Related

IDX condition in a getresource tpl

I use getresource like this:
[[!getResources? &tpl=`coffrets_id` &parents=`1115`]]
inside coffrets_id tpl I'd like to be able to do something like this:
[[+idx:is=`1`:then=`show only first getressrouce result`:else=``]]
[[+idx:is=`2`:then=`show only second getressrouce result`:else=``]]
[[+idx:is=`3`:then=`show only third getressrouce result`:else=``]]...
but I have no idea how to show only the result that matches the idx. Is there any solution?
Just use Templating Properties
[[!getResources?
&tpl=`coffrets_id`
&tpl_1=`coffrets_id_1`
&tpl_2=`coffrets_id_2`
&tpl_3=`coffrets_id_3`
&parents=`1115`
]]

Jade conditional with select options

How to set the option (of select) to selected to match the current language of the page? Is there a way to inline it and simplify it something like this:
(value="en" #{Locale}==='en' ? ',selected="selected"': '')
I have tried some answers on this site, but they do not seem to work. Thank you.
This is the view:
if(#{Locale} ==='en')
option(value="en", selected="selected") #{English}
option(value="bg") #{Bulgarian}
else if(#{Locale} === 'bg')
option(value="en") #{English}
option(value="bg",selected="selected") #{Bulgarian}
Adapted from this answer, you could create a mix-in that handles the logic for you:
mixin lang-option(code, name)
if (Locale === code)
option(value=code, selected="selected")= name
else
option(value=code)= name
+lang-option('en', English)
+lang-option('bg', Bulgarian)
This solution works if you need to parameterize the 'disabled' attribute as well. Jade will not output attributes that evaluate to false.
//Selects the option when option.value == selectValue
mixin selectOption(option, selectValue)
option(value=option.value, disabled=option.disabled, selected=(option.value==selectValue))= option.label

how to handle conditionally existing components in action code?

This is another problem I am facing while migrating from antlr3 to antlr4. This problem is with the java action code for handling conditional components of rules. One example is shown below.
The following grammar+code worked in antlr3. Here, if the unary operator is not present, then a value of '0' is returned, and the java code checks for this value and takes appropriate action.
exprUnary returns [Expr e]
: (unaryOp)? e1=exprAtom
{if($unaryOp.i==0) $e = $e1.e;
else $e = new ExprUnary($unaryOp.i, $e1.e);
}
;
unaryOp returns [int i]
: '-' {$i = 1;}
| '~' {$i = 2;}
;
In antlr4, this code results in a null pointer exception during a run, because 'unaryOp' is 'null' if it is not present. But if I change the code like below, then antlr generation itself reports an error:
if($unaryOp==null) ...
java org.antlr.v4.Tool try.g4
error(67): missing attribute access on rule reference 'unaryOp' in '$unaryOp'
How should the action be coded for antlr4?
Another example of this situation is in if-then-[else] - here $s2 is null in antlr4:
ifStmt returns [Stmt s]
: 'if' '(' e=cond ')' s1=stmt ('else' s2=stmt)?
{$s = new StmtIf($e.e, $s1.s, $s2.s);}
;
NOTE: question 16392152 provides a solution to this question with listeners, but I am not using listeners, my requirement is for this to be handled in the action code.
There are at least two potential ways to correct this:
The "ANTLR 4" way to do it is to create a listener or visitor instead of placing the Java code inside of actions embedded in the grammar itself. This is the only way I would even consider solving the problem in my own grammars.
If you still use an embedded action, the most efficient way to check if the item exists or not is to access the ctx property, e.g. $unaryOp.ctx. This property resolves to the UnaryOpContext you were assuming would be accessible by $unaryOp by itself.
ANTLR expects you access an attribute. Try its text attribute instead: $unaryOp.text==null

Regex for href value in Groovy

What's the best way in Groovy to capture a link's href value with regex?
example: some link
I want to capture just what's in bold.
It depends on what you're picking the anchor tag out of, but if you have that isolated to what you have shown, the following should work:
def link= """some "link"""
def url = (x =~ /\"(.*?)\"/)[0][1]

Get the image url from string

My string is: [slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_006.jpg" slide_desc="
How to get the part after "image=" from it?
In short, use RegEx to get the data between 2 strings. For the most part, the same Reg Expressions between different languages should work just fine.
In PHP, you would want to use a preg_match.
$string = "[slide image=\"http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uploadify/uploads/bas_006.jpg\" slide_desc=\""
preg_match("/image=\"(.*)\"/i", $string, $results)
var_dump($results)
Update regex (a little more strict):
/image\="([^"]*)"/i
All in all, going to depend on the language being used and in what context. You can get much more advanced with the RegEx, but this is just quick & dirty.
Full PHP Code Example:
<?php
$string = '[slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uplo‌​adify/uploads/bas_006.jpg" slide_desc="<h4>Promotional Package</h4> Project description sentence" text_color="#464646" slide_horizontal="false"] [slide image="http://themes.devatic.com/konzept/wp-content/themes/konzept/includes/uplo‌​adify/uploads/bas_005.jpg" slide_desc="<h4></h4>" text_color="#464646" slide_horizontal="false"]';
preg_match_all('/image\="([^"]*)"/i', $string, $results);
foreach ($results[1] as $res):
echo 'Image URL:'.$res."\n";
endforeach;
?>

Resources