Can you ignore case while using replace? - twig

Pretty self explanatory. For example, I would want both test and Test to be replaced:
{{ var|replace({"test": 'replaced'}, ignore) }}

The replace filter is defined as a wrapper for strtr, so there's no built-in way to do that.
Extending twig is easy! Simply create a new filter called ireplace (or something else you find appropriate) that uses str_ireplace in the background.

I think this function is used to replace keywords, not to do a search and replace

Related

Can I format variables like strings in python?

I want to use printing command bellow in many places of my script. But I need to keep replacing "Survived" with some other string.
print(df.Survived.value_counts())
Can I automate the process by formating variable the same way as string? So if I want to replace "Survived" with "different" can I use something like:
var = 'different'
text = 'df.{}.value_counts()'.format(var)
print(text)
unfortunately this prints out "df.different.value_counts()" as as a string, while I need to print the value of df.different.value_counts()
I'm pretty sure alot of IDEs, have this option that is called refactoring, and it allows you to change a similar line of code/string on every line of code to what you need it to be.
I'm aware of VSCode's way of refactoring, is by selecting a part of the code and right click to select the option called change all occurances. This will replace the exact code on every line if it exists.
But if you want to do what you proposed, then eval('df.{}.value_counts()'.format(var)) is an option, but this is very unsecured and dangerous, so a more safer approach would be importing the ast module and using it's literal_eval function which is safer. ast.literal_eval('df.{}.value_counts()'.format(var)).
if ast.literal_eval() doesn't work then try this final solution that works.
def cat():
return 1
text = locals()['df.{}.value_counts'.format(var)]()
Found the way: print(df[var].value_counts())

Modx ifempty then else statements

Currently the code below will only show the HeroImage if one exists. If one doesn't exist, it doesn't show the other image blogImage. How can I change the code to show blogImage, if there is no HeroImage. Thanks
<img src="[[*HeroImage:isempty:then='[[*blogImage:phpthumbof=`w=1200&h=800`]] [[*]]':else='[[*HeroImage:phpthumbof=`w=1200&h=800`]] [[*]]']]" alt="[[*pagetitle]]" class="img-responsive"/>
Using the :default option should be enough:
[[*HeroImage:default=`[[*blogImage]]`:phpthumbof=`w=1200&h=800`]]
Ifempty and (default, empty, isempty) works like default value if input not specified else it returns the input value itself. So second else statement has no meaning and doesn't work.
For your purpose you can use "is" modifier like
[[*HeroImage:is=``:then=`[[*blogImage:phpthumbof=`w=1200&h=800`]]`:else=`[[*HeroImage:phpthumbof=`w=1200&h=800`]]`]]
And keep in mind using back-ticks for modx templating
I'd roll this a slightly different way, but it's likely the same result:
[[*HeroImage:neq=``:then=`[[*HeroImage:phpthumbof=`w=1200&h=800`]]`:else=`[[*blogImage:phpthumbof=`w=1200&h=800`]]`]]

Substring and Join in Twig

I know it's possible to join an array in twig, like so:
['name_first', 'name_second', 'name_third']|join(', ')
{# name_first, name_second, name_third #}
I also know I can use [5:] to remove the 'name_' part of each element. But, is it possible combine both, without looping over the whole thing. So, the final result will be:
first, second, third
With inspiration from DarkBee's comment, it's pretty easy to achieve.
{{ ['name_first', 'name_second', 'name_third']|join(',')[5:] }}
This removes the first 5 characters of each member of the array then concatenates them with a comma separating them.

Do I need to use an underscore separator in the aggregation query?

Is it ok to use a (semicolon) ; separator instead of (underscore) _
and adding my own format for easier parsing in the aggregation?
It looks like at the end it just needs to be a string so probably doesn't
matter but would like confirmation.
This is an example aggregation I would like to do.
object_type={{object_type}};verb={{verb.infinitive}};target={{target }};actor={{actor}};time={{time.strftime("%Y-%m-%d-%H-%M-%S-%f")}}
The example shows something like this:
{{ actor }}_{{ verb.id }}_{{ time.strftime('%H') }}
Yes it's just a string. This shouldn't be a problem. Also we're going to be releasing a new interface which will make it easier to test different aggregation formats.

expression engine dynamic variable names: {slide_{index}_title}

I am using a simple looping plugin so that my template looks like this:
{exp:loop_plus start="1" end="4" increment="1"}
<h3>{slide_{index}_title}</h3>
{/exp:loop_plus}
However, I am ending up with the following output:
<h3>{slide_1_title}</h3>
<h3>{slide_2_title}</h3>
<h3>{slide_3_title}</h3>
<h3>{slide_4_title}</h3>
Is there any way I can have dynamic variable names like this? I am not looking for alternative methods for building a slider, I simply would like to know if the dynamic variable names like this is possible. Thanks!
I'm assuming that Loop Plus (http://devot-ee.com/add-ons/loop-plus) sets the {index} part, so the question is what is defining {slide_1_title}...?
Assuming you have an entry field or variable with this defined, what you have is correct, but if it's not working, it means there's a parsing order issue.
Let's assume the code you supplied is wrapped in a {exp:channel:entries} tag pair, what happens is EE will try to parse the variable first, so will see: {slide_{index}_title} which doesn't exist. The {exp:loop_plus} add-on will then parse it, converting it to {slide_1_title} (but to late as channel:entries has already tried to parse it), which is what is finally output to the template.
So what you want to ensure is that EE parses {exp:loop_plus} before {exp:channel:entries}, do this using parse="inward" tag:
{exp:loop_plus start="1" end="4" increment="1" parse="inward"}
<h3>{slide_{index}_title}</h3>
{/exp:loop_plus}
This is a global EE parameter that EE uses to control parse order - you won't find it documented under the specific add-on. By adding the parameter, it means this child tag will get parsed before it's parent.
One way you could do it is to declare a preload_replace variable in your template and use it in your custom field name.
So something like:
{preload_replace:my_var_prefix="whatever"}
And then in your loop, you could then use:
{slide_{my_var_prefix}_title}

Resources