TWIG - Remove Everything After Certain Character - twig

In Twig, I am wondering if it is possible to not render everything after a certain character. For example, if I had the string 'Ironman 3 : Marvel' and I want to strip everything after the semicolon so my string will become 'Ironman 3'. I am wondering if this is possible?

You should be able to do this with some variation of {{ movietitle|split(':')[0] }}

Related

preprocessing html data with powershell

i have some html source code of customer data that needs to be cleaned from html tags before deployed with a line joining string split.
i want to be able to target specific types of information.
if for example a customer has a list of categories on his page.
each 'category' sits, perched inside of an easily distinguishable tag:
<span _ngcontent-jal-c67="" class="category-name">Cryptocurrency</span>
would it be possible to remove everything else that is not nested inside a similar html tag?
let's say, for exampple i want evrything thats occurs inside of <span *>*</span>. so that every non <span></span> tag and its contents would be removed. the contents of all the <span ***>***</span> would stay, without the tag.
is that something i could do in powershell?
let's avoid paste.exe and cygwin type of stuff. i'm looking for standard native windows approach (cmd or powershell).
again, i want to remove all tags.
just the contents that i don't remove should be limited to those found in a specific tag. like ,<span _ngcontent-jal-c68="" class="category-name">Shopping</span>
everything that fits the <span *>*</span> profile
leave only the contents. no tag.
from: <span _ngcontent-jal-c32="" class="category-name">Home and Graden</span>
to: Home and Graden
i'm really looking for an answer for how to do this in powershell without needing to install anything or to make any interesting changes to the OS (windows10)
Instead of using delicate Regular Expressions, you might just use the [System.Net.WebUtility]::HtmlDecode method for this:
$Html = '<span _ngcontent-jal-c67="" class="category-name">Cryptocurrency</span>'
([Xml][System.Net.WebUtility]::HtmlDecode($Html)).GetElementsByTagName('span').'#text'
Result:
Cryptocurrency
Please try to investigate into the problem before asking on Stackoverflow. Did you know there is a -replace operator in PowerShell which allows you to use RegEx? Did you identify that RegEx might help you with your problem?
Anyway, here is one approach, you could take.
$html = '<span _ngcontent-jal-c32="" class="category-name">Home and Graden</span>'
if ($html -match '(<span.*>)(?<Category>.+)(</span>)') {
$Matches.Category
}
Home and Graden
The -match operator can test for a RegEx. The RegEx (<span.*>)(?<Category>.+)(</span>) will create three groups, one of which is named Category. The category sits in between the span-tags. For your input, you have to be sure that any categories will sit inside of a span-tag.
If -match returns true, the automatic variable $Matches is filled. Since we named second group Category, we can easily access it as a property with $Matches.Category.
Alternatively, and for more complex html files even preferrably, you can parse html with PowerShell, see Powershell Tip : Parsing HTML from a local File or a String

Twig tests VS php tests

I have to work with an array of information extracted from a database and I want to display each part of this array according to a criterion, so it is better to make the test in the PHP or in the Twig template?
Depends on a couple of things:
the complexity of the test
The number of places you need to execute this test logic in.
If it's just something like a simple comparison like {{ product.color == 'red' ? 'love' : 'hate' }} then you should most likely put it in the twig template itself. If it is more complex it's best to put it in PHP code to keep it readable and thus separate the layout from the logic.
If you need to do this test in more than a couple of places you should also put it in a PHP object method and use that in all of those places. That way you do not have to change the 'red' color in multiple places if you'd ever want to change it.

TWIG: What is the similar instruction to PHP's $$var?

I get an array in view and I only know the name (string) of a variable.
I need to evaluate it.
How can I do?
Twig doesn't have variable variables but you can make a twig extension / filter that would sort that problem and it shouldn't be very difficult.
Here is how to do one, in case you haven't been working with this:
http://symfony.com/doc/current/cookbook/templating/twig_extension.html

mongoDB search only last 6 digits

Is there away to use find() to search for barcode that ends with the last digits 365478 I dont care what is in front
I thought something like this.
$db.find(array('barcode'=>*365478))
but that does not seem to work. am I missing something?
A regular expression of /365478$/ would provide the right filter.
Regex will work but you won't be able to use an index for the query. I'd suggest storing the last 6 digits in a separate field, put an index on it and use that.
This Works Well /$365478/ of course you can use regex but keeping it simple pays off.
You can use a regex for this but you have to store the barcode reversed if you want to be able to use an index (only rooted regex can use an index, see http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions)
If that is unpractical for you you will have to save the last X digits seperately and do exact matching. If your database is going to be relatively small you might be able to get away without indexing.
the regex should be like this: 365478$
using PHP for eg.
$regex = new MongoDB\BSON\Regex ( '365478$');

Is there a way to get the display tags back (with spaces), rather than the coded versions?

I'm using phpFlickr to make this query:
$photos = $f->photos_search(array(
'tags'=>$tag,
'sort'=>'date-taken-desc',
'per_page'=>100,
'user_id'=>'me',
'privacy_filter'=>5,
'extras'=>'date_taken,tags,url_sq,description'
));
It's working great, except, I have some tags with spaces in them and I want to display them that way. Is it possible? Maybe I need to do a tag lookup first?
found it:
http://www.flickr.com/services/api/flickr.tags.getListUserRaw.html

Resources