Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » Witz and map variable
Witz and map variable [message #37981] Tue, 27 November 2012 08:51 Go to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Hi,

I would like to replace some numbers in witz script with string.
Do something similar to Format("%[0:OK;1:ERROR;???]s", error_code).
I guess that map variable should do the trick. Can someone show me example how to create and use map variable in witz?
Re: Witz and map variable [message #37984 is a reply to message #37981] Tue, 27 November 2012 09:45 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Zbych wrote on Tue, 27 November 2012 08:51

Hi,

I would like to replace some numbers in witz script with string.
Do something similar to Format("%[0:OK;1:ERROR;???]s", error_code).
I guess that map variable should do the trick. Can someone show me example how to create and use map variable in witz?


Hi Zbych,

Using maps in witz is quite straightforward. You just pass it to Skylark in handler:
SKYLARK(MyHandler,"path"){
    int i=1;
    ValueMap map; // ValueArray could be used as well
    map.Add(0,"OK");
    map.Add(1,"ERROR");
    http("ID",i)("MAP",map).RenderResult("MyApp/template");
}

and then you can work with it in the template:
element $ID in MAP is: $MAP[ID]

I haven't tested the code, hopefully I made no mistakes Smile

Best regards,
Honza
Re: Witz and map variable [message #37987 is a reply to message #37984] Tue, 27 November 2012 10:50 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Thanks for reply.
I know that I can pass the map from handler. The problem is that this map will be different for every language and I would like to keep all language things in one place (witz scripts).

That is why I am looking for something like this:

error_map = { 0:"OK", 1:"ERROR", 2:"???" }
<b> System status: $error_map[$error_code] </b>

Is it possible to create new variables inside witz scripts?
Re: Witz and map variable [message #37990 is a reply to message #37987] Tue, 27 November 2012 12:29 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Zbych wrote on Tue, 27 November 2012 10:50

Thanks for reply.
I know that I can pass the map from handler. The problem is that this map will be different for every language and I would like to keep all language things in one place (witz scripts).


Ok, I think I understand now. I think your best chance if you really want to keep it that way is to create a witz function, something like translate(lang,word1,word2,word3,...) that would just return the parameter according to the value of lang. But I think this is not really the correct way, as it might require a lot of work when adding support for new language. You would have to modify all of your templates to add another parameter, even if the same word would be used repeatedly...

So what about a function that would utilize U++ internationalization capabilities? It could behave exactly as t_() macro, then you could just write t_("String in English") and it would return the correct translation based on .t files. I know it violates your wish to "have everything language specific in witz", but .t files are designed for this, so it might not be so bad idea Wink

I assume you are using the .lang/.__lang__ variables and language specific templates (as described in chapter 11 of the tutorial), right?

Zbych wrote on Tue, 27 November 2012 10:50

Is it possible to create new variables inside witz scripts?

No, quoting from the manual:
Quote:

Witz primary values come from shared variable space of Skylark handler; the only other way to define a new variable is by using for statements.


Honza
Re: Witz and map variable [message #38028 is a reply to message #37990] Wed, 28 November 2012 19:50 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Thank you Honza for clarification. I guess that creation of variables in witz should be added to the next wish list Smile
Re: Witz and map variable [message #38040 is a reply to message #38028] Thu, 29 November 2012 07:03 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Zbych wrote on Wed, 28 November 2012 19:50

Thank you Honza for clarification. I guess that creation of variables in witz should be added to the next wish list Smile

Well, it is kind of a philosophic question... I think that at the moment you put custom variables to templates, te designer might as well request more and more "reasonable" features to work with them and you end up with half the codebase located in templates Smile Which is of course less effective, than having it in pure C++.

If there is a need for something like this, it should be IMHO handled through a function and only used in some special cases, not as a general rule.

Honza
Re: Witz and map variable [message #38050 is a reply to message #38040] Thu, 29 November 2012 10:37 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Perhaps you are right. But know I need to keep some language specific things in *.t files and some in *.witz.

I have one more question for you.
I have a drop list and want to preselect one value from it.
Right now I create a map ("Item1", "0")("Item2", "1")("Item3", "0"), pass it to witz and use $if to preselect position.

<select name="MyList"> 
	$for(i in MYLIST)
		$if ( i != 0 )
			<option value="$i._key" selected="selected">$i._key</option>
		$else
			<option value="$i._key">$i._key</option>
		$/
	$/
</select>


Is there a shorter way (without $if)? Maybe something similar to c?
$for(i in MYLIST)
	<option value="$i._key" $(i != 0) ? selected="selected" : "">$i._key</option>
$/

[Updated on: Thu, 29 November 2012 10:42]

Report message to a moderator

Re: Witz and map variable [message #38052 is a reply to message #38050] Thu, 29 November 2012 11:11 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Zbych wrote on Thu, 29 November 2012 10:37

I have one more question for you.
I have a drop list and want to preselect one value from it.
Right now I create a map ("Item1", "0")("Item2", "1")("Item3", "0"), pass it to witz and use $if to preselect position.
<select name="MyList"> 
	$for(i in MYLIST)
		$if ( i != 0 )
			<option value="$i._key" selected="selected">$i._key</option>
		$else
			<option value="$i._key">$i._key</option>
		$/
	$/
</select>

I'm not aware of any other simple way, I do it like this too. Only alternative I know about is sending MYLIST as an array of arrays/maps of two field, where the second contains the "selected=\"selected\"" or empty string, so it can be included in single pass, without $if. But it seems to me even worse, because it pushes html code into app Smile

Zbych wrote on Thu, 29 November 2012 10:37

Is there a shorter way (without $if)? Maybe something similar to c?
$for(i in MYLIST)
	<option value="$i._key" $(i != 0) ? selected="selected" : "">$i._key</option>
$/

I believe (can't test right now) you can inline the $if:
$for(i in MYLIST)
	<option value="$i._key" $if(i != 0) selected="selected" $/>$i._key</option>
$/


Honza
Re: Witz and map variable [message #38054 is a reply to message #38052] Thu, 29 November 2012 11:26 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
dolik.rce wrote on Thu, 29 November 2012 11:11

I believe (can't test right now) you can inline the $if:
$for(i in MYLIST)
	<option value="$i._key" $if(i != 0) selected="selected" $/>$i._key</option>
$/




This is it! Thank you.

I have one more problem with map. I can not display map with key zero:
*.cpp
	ValueMap yn;
	yn.Add(0, "No");
	yn.Add(1, "Yes");
	http
		("YESNO", yn)
	[...]

*.witz:
	$YESNO[0] <-- displays nothing
	$YESNO[1]


I am quite sure I already saw this in forum, but I can not find it now.
Re: Witz and map variable [message #38064 is a reply to message #38054] Thu, 29 November 2012 14:55 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Zbych wrote on Thu, 29 November 2012 11:26

I have one more problem with map. I can not display map with key zero:
*.cpp
	ValueMap yn;
	yn.Add(0, "No");
	yn.Add(1, "Yes");
	http
		("YESNO", yn)
	[...]

*.witz:
	$YESNO[0] <-- displays nothing
	$YESNO[1]


I am quite sure I already saw this in forum, but I can not find it now.

I think you mean this post. That problem was fixed, but it seems it only works for ValueArray. Try using that or ValueMap with some other indices...

Honza
Re: Witz and map variable [message #38083 is a reply to message #38064] Thu, 29 November 2012 21:18 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
It works with ValueArray. Thanks.
Re: Witz and map variable [message #38088 is a reply to message #37981] Fri, 30 November 2012 07:41 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Ha, I finally found the actual problem! Very Happy

It all goes down to the compiler, it decides that the 0 is not integer, but empty string on the line yn.Add(0,"NO") Smile So it saves it to the map with "" as key, not 0. To make this work, you have to cast the 0 to Value first:
	ValueMap yn;
	yn.Add(Value(0), "YES");
	yn.Add(1,"NO");


Best regards,
Honza
Re: Witz and map variable [message #38089 is a reply to message #38088] Fri, 30 November 2012 07:51 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
dolik.rce wrote on Fri, 30 November 2012 07:41


It all goes down to the compiler, it decides that the 0 is not integer, but empty string


Bug or feature? Smile

Re: Witz and map variable [message #38091 is a reply to message #38089] Fri, 30 November 2012 08:30 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Zbych wrote on Fri, 30 November 2012 07:51

dolik.rce wrote on Fri, 30 November 2012 07:41


It all goes down to the compiler, it decides that the 0 is not integer, but empty string


Bug or feature? Smile


I'd say it is not a feature Smile Perhaps a limitation Smile

I think it is possible to solve by adding new overload of Add (and perhaps also similar one for Set):
	void Add(int n, const Value& value)           { Add(Value(n), value); }


Mirek, what do you think? I'm not familiar enough with the Value * internals, so I can't really tell if it breaks anything else Smile

Honza
Re: Witz and map variable [message #38110 is a reply to message #38091] Sat, 01 December 2012 12:17 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
dolik.rce wrote on Fri, 30 November 2012 02:30


I think it is possible to solve by adding new overload of Add (and perhaps also similar one for Set):
	void Add(int n, const Value& value)           { Add(Value(n), value); }


Mirek, what do you think? I'm not familiar enough with the Value * internals, so I can't really tell if it breaks anything else Smile

Honza


Agreed. It is not the most elegant solution, but if we want to be able to use ValueMap directly with Id and SqlId keys, it is probably the only possible solution...

(fixed)

Mirek
Previous Topic: How to close a Thread?
Next Topic: What's wrong with this code?
Goto Forum:
  


Current Time: Thu Mar 28 20:54:02 CET 2024

Total time taken to generate the page: 0.01178 seconds