Smarty - String to Currency from comma to point to comma - type-conversion

i have the following string:
$betrag = 4,9
an want to convert this to a currency:
4,90 €
is there a better way as:
{$betrag|replace:",":"."|string_format:"%.2f"|replace:".":","} €

Good solution is to use smarty modifiers:
function smarty_modifier_num2front($string, $precision = 2)
{
// here you can use your own filter by call some class (or direct php code)
return \lib\Numeric::front($string, $precision);
}
Following function should be located in your smarty plugin directory and in this case named 'modifier.num2front.php'
In template you use this like that:
{$betrag|num2front}

Related

Dart replace curly braces from string with relative values

I got a couple of response from a json which includes curly braces such as ${title} or {title}
How can I replace these vaules with values I preselected?
For example:
jsonString = '{title}.2019.mkv'
jsonString2 = '${title}.2019.mkv'
How can I replace the field in those Strings with values I preselected like:
var title = 'Avengers'
Maybe I should just use regex or is there a better way?
In case you need to replace part of the string you can use
'{title}.2019.mkv'.replaceFirst('{title}', 'Avengers')
Link to documentation

TYPO3 - Fluid Translate in PHP ViewHelper within extension

Using PHP 8.7.17
I have the following viewhelper to give an example of what I require
<?php namespace SRS\SrsPccLog\ViewHelpers;
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
class ShowDateAsStringViewHelper extends AbstractViewHelper
{
public function initializeArguments()
{
$this->registerArgument('month', 'integer', 'month value', true);
}
public function render()
{
$month = $this->arguments['month'];
return $this->monthAsString($month, $year);
}
public function monthAsString ($month) {
switch ($month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
default:
return "";
}
}
I have the function but what I simply want to do is display the month in the native language, ie replace return "January'
with <f:translate key="tx_srspcclog_domain_model_myext.january" />
so that I can be less language specific and get the language from the language files like i do for a fluid view. Any ideas of how to do this when you not in a fluid view but a PHP view helper
\TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, $extensionName)
Look inside the TranslateViewHelper and see how they did it (most likely the translate(...) method), and use the code to render your own labels from your XLFF files
What you are actually looking for is "%B".
As the documentation clearly states, the FormatDate ViewHelper understands format strings in strftime() as well as date() format. strftime already has localization built in. You don't need to translate anything yourself.
https://docs.typo3.org/typo3cms/ExtbaseGuide/Fluid/ViewHelper/Format/Date.html#localized-time-using-strftime-syntax
http://php.net/manual/de/function.strftime.php

Cleanest way to remove trailing blanks a string in swift?

In Perl if I had trailing blanks in a string, I would use this code
$line =~ s/ +$//;
Is there a similar command or function in swift?
Swift provides no support for regular expressions. It doesn't need to, though, because the only place you'll ever use Swift is in the presence of Cocoa — and Cocoa's Foundation framework does provide support for regular expressions (and has other string-handling commands). Swift String and Framework's NSString are bridged to one another.
Thus, you could use stringByReplacingOccurrencesOfString:withString:options:range: with a regular expression pattern, for example (assuming that stringByTrimmingCharactersInSet: is too simple-minded for your purposes).
If you want to do this multiple times I suggest you add this handy extension to your project:
extension String {
var strip: String {
return self.trimmingCharacters(in: .whitespaces)
}
}
Now you can do something as simple as this:
let line = " \t BB-8 likes Rey \t "
line.strip // => "BB-8 likes Rey"
If you prefer a Framework that also has some more handy features then checkout HandySwift. You can add it to your project via Carthage then use it exactly like in the example above:
import HandySwift
let line = " BB-8 likes Rey "
line.strip // => "BB-8 likes Rey"

Add method to string class in node.js express.js coffeescript

I want to add some my methods to generic types like string, int etc. in express.js (coffeescript). I am completely new in node. I want to do just this:
"Hi all !".permalink().myMethod(some).myMethod2();
5.doSomething();
variable.doSomethingElse();
How to do this ?
You can add a method to the String prototype with:
String::permaLink = ->
"http://somebaseurl/archive/#{#}"
String::permalink is shorthand for String.prototype.permaLink
You can then do:
somePermalink = "some-post".permaLink()
console.log somePermalink.toUpperCase()
This will call the the "String.prototype.permaLink" function with "this" set to the "some-post" string. The permaLink function then creates a new string, with the string value of "this" (# in Coffeescript) included at the end. Coffeescript automatically returns the value of the last expression in a function, so the return value of permaLink is the newly created string.
You can then execute any other methods on the string, including others you have defined yourself using the technique above. In this example I call toUpperCase, a built-in String method.
You can use prototype to extend String or int object with new functions
String.prototype.myfunc= function () {
return this.replace(/^\s+|\s+$/g, "");
};
var mystring = " hello, world ";
mystring.myfunc();
'hello, world'

How to define a `separator` tag in play-1.x without modifing play's source code

I want to define a tag separator tag, which inside a list tag, can add separator between items.
The sample code is:
List<String> users = new ArrayList<String>();
users.add("Jeff");
users.add("Mike");
#{list users, as: 'user'}
#{separator ' + ' /}
<span>${user}</span>
#{/list}
If I don't use the separator tag, the code will be:
#{list users, as: 'user'}
${user_isFirst ? '' : ' + '}
<span>${user}</span>
#{/list}
The generated html code will be:
<span>Jeff</span> + <span>Mike</span>
I tried defined a fastTag:
public static void _separator(Map<?, ?> args, Closure body, PrintWriter out, GroovyTemplate.ExecutableTemplate template, int fromLine) {
Object value = args.get("arg");
// TODO how to get the value of `as` defined in parent `list` tag?
out.print(value);
}
But the problem is I can't get the value of as defined in list tag (which is user) in this case.
You can create a custom list tag in groovy like this
#{list items:_arg, as:'tmp'}
%{
attrs = [:]
attrs.put(_as, tmp)
}%
#{ifnot tmp_isFirst}${_sep}#{/ifnot}
#{doBody vars:attrs /}
#{/list}
and use it like this
#{myList users, as:'user', sep:','}
${user}
#{/myList}
You should trace into your FastTag implementation. I think you'll see all the variables in scope inside the args map. This is from memory - so, sorry if not.
That said, I think it might be simpler if you copy the Java code for #{list} and add a new parameter, like
#{list users, as: 'user', separator: '+' }
and handle the logic in there. It seems a bit cleaner too from a design point of view - if it is a separator, how come you can put it anywhere you like in the code (and why not put it in twice!).
A final option is to look at Groovy or Java collection operators. http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html