You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Scott Eade <se...@backstagetech.com.au> on 2001/08/13 05:25:21 UTC
#parse($stringValue)
What is the appropriate way to go about parsing the contents of a
variable?
A couple of examples:
1. What if my application's skin defines some text to be replaced
that is then used in another skin variable?
adminTitle = Systems Administrator
contactMsg = Please contact your $ui.adminTitle if you encounter problems.
2. What if I want to save a ui variable to the database - e.g. I want
to be able to save and edit my help text as part of my application. In
order to do this, my help text will need to use $ui.adminTitle.
How do I go about parsing the following terms so that $ui.adminTitle is
displayed as "System Administrator"?
$ui.contactMsg
$SomeDBObject.SomeColumn (data contains $ui.adminTitle)
Thanks,
Scott
Re: #parse($stringValue)
Posted by Scott Eade <se...@backstagetech.com.au>.
From: "Geir Magnusson Jr." <ge...@optonline.net>
> Scott Eade wrote:
> >
> >
> > I think my second example may however still be relevant in the context
> > of just velocity. What I want is to be able to retrieve some data from
> > a database that includes a context reference - e.g. in the database the
> > column for a specfic row may be:
> >
> > Contact the $adminTitle for help.
> >
> > Then with this:
> >
> > #set($adminTitle = "System Administrator)
> >
> > in the context I want to retrieve the data from the database and have
> > it output:
> >
> > Contact the System Administrator for help.
> >
> > The string that needs to be parsed for variables is the one being retrieved
> > from the database.
>
> Ok - the doublequote string literal won't work, because velocity doesn't
> "recurse" on string lits to interpolate. (This ain't Perl)
>
> > The only way I can currently see to make this work is to retrieve the
> > data nto a temporary file and then #parse() it. I was hoping there might
> > be a more direct way.
>
> There is - the org.apache.velocity.app.Velocity (and VelocityEngine)
> have an evaluate() method that will allow you to do to a String or
> InputStream what #parse() does to templates.
>
> So make a little wrapper around it and then you can do it in your
> template to your heart's delight :
>
> #set($dbstring = $dbtool.getColumn(<whatever>)
>
> (so I assume $dbstring contains "Contact the $adminTitle for Help")
>
> and then
>
> #set($adminTitle = 'System Administrator')
>
> #set($completestring = $yourtool.eval($dbstring) )
>
>
> The evalualte() method needs a context, so either make sure you stuff
> the current context into it when putting it in the context, or make it
> context agnostic, put a reference to the context in the context, and
> then
>
> #set($completestring = $yourtool.eval($context, $dbstring) )
>
>
> I would assume this tool might already exist in the 'rupert' project in
> commons-sandbox - so look there first. If not, that would be a great
> one to contribute to there :)
>
> geir
>
Geir,
That puts me on the right track.
Thanks a million,
Scott
Re: #parse($stringValue)
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Scott Eade wrote:
>
>
> I think my second example may however still be relevant in the context
> of just velocity. What I want is to be able to retrieve some data from
> a database that includes a context reference - e.g. in the database the
> column for a specfic row may be:
>
> Contact the $adminTitle for help.
>
> Then with this:
>
> #set($adminTitle = "System Administrator)
>
> in the context I want to retrieve the data from the database and have
> it output:
>
> Contact the System Administrator for help.
>
> The string that needs to be parsed for variables is the one being retrieved
> from the database.
Ok - the doublequote string literal won't work, because velocity doesn't
"recurse" on string lits to interpolate. (This ain't Perl)
> The only way I can currently see to make this work is to retrieve the
> data nto a temporary file and then #parse() it. I was hoping there might
> be a more direct way.
There is - the org.apache.velocity.app.Velocity (and VelocityEngine)
have an evaluate() method that will allow you to do to a String or
InputStream what #parse() does to templates.
So make a little wrapper around it and then you can do it in your
template to your heart's delight :
#set($dbstring = $dbtool.getColumn(<whatever>)
(so I assume $dbstring contains "Contact the $adminTitle for Help")
and then
#set($adminTitle = 'System Administrator')
#set($completestring = $yourtool.eval($dbstring) )
The evalualte() method needs a context, so either make sure you stuff
the current context into it when putting it in the context, or make it
context agnostic, put a reference to the context in the context, and
then
#set($completestring = $yourtool.eval($context, $dbstring) )
I would assume this tool might already exist in the 'rupert' project in
commons-sandbox - so look there first. If not, that would be a great
one to contribute to there :)
geir
--
Geir Magnusson Jr. geirm@optonline.net
System and Software Consulting
Developing for the web? See http://jakarta.apache.org/velocity/
Well done is better than well said - New England Proverb
Re: #parse($stringValue)
Posted by Scott Eade <se...@backstagetech.com.au>.
From: "Jon Stevens" <jo...@latchkey.com>
>
> From the documentation:
>
> <http://jakarta.apache.org/velocity/developer-guide.html>
>
> String Interpolation
>
> runtime.interpolate.string.literals = true
> Controls interpolation mechanism of VTL String Literals. Note that a VTL
> StringLiteral is specifically a string using double quotes that is used in a
> #set() statement, a method call of a reference, a parameter to a VM, or as
> an argument to a VTL directive in general. See the VTL reference for further
> information.
>
> -jon
>
I'm using turbine which uses true as the default value for the interpolate
string literals property.
Scott
Re: #parse($stringValue)
Posted by Jon Stevens <jo...@latchkey.com>.
on 8/12/01 11:01 PM, "Scott Eade" <se...@backstagetech.com.au> wrote:
> Hmm, this doesn't seem to work for me here. $foo is resolved to
> the content of $databaseContent, but any variables within it are not
> processed. Using my example, I still get:
>
> Contact your $adminTitle for help.
>
> Scott
>From the documentation:
<http://jakarta.apache.org/velocity/developer-guide.html>
String Interpolation
runtime.interpolate.string.literals = true
Controls interpolation mechanism of VTL String Literals. Note that a VTL
StringLiteral is specifically a string using double quotes that is used in a
#set() statement, a method call of a reference, a parameter to a VM, or as
an argument to a VTL directive in general. See the VTL reference for further
information.
-jon
Re: #parse($stringValue)
Posted by Scott Eade <se...@backstagetech.com.au>.
From: "Jon Stevens" <jo...@latchkey.com>
>
> > The only way I can currently see to make this work is to retrieve the
> > data nto a temporary file and then #parse() it. I was hoping there might
> > be a more direct way.
>
> There is:
>
> #set ( $foo = "$databaseContent")
> $foo
>
> -jon
>
Hmm, this doesn't seem to work for me here. $foo is resolved to
the content of $databaseContent, but any variables within it are not
processed. Using my example, I still get:
Contact your $adminTitle for help.
Scott
Re: #parse($stringValue)
Posted by Jon Stevens <jo...@latchkey.com>.
on 8/12/01 10:24 PM, "Scott Eade" <se...@backstagetech.com.au> wrote:
> The only way I can currently see to make this work is to retrieve the
> data nto a temporary file and then #parse() it. I was hoping there might
> be a more direct way.
There is:
#set ( $foo = "$databaseContent")
$foo
-jon
Re: #parse($stringValue)
Posted by Scott Eade <se...@backstagetech.com.au>.
Geir,
I understand the distinction between single and double quotes as used
in this context, but this isn't quite what I am after.
My first example uses Turbine's UIManager - so this is not a good place
(i.e. the velocity mail list) to use it as an example.
I think my second example may however still be relevant in the context
of just velocity. What I want is to be able to retrieve some data from
a database that includes a context reference - e.g. in the database the
column for a specfic row may be:
Contact the $adminTitle for help.
Then with this:
#set($adminTitle = "System Administrator)
in the context I want to retrieve the data from the database and have
it output:
Contact the System Administrator for help.
The string that needs to be parsed for variables is the one being retrieved
from the database.
The only way I can currently see to make this work is to retrieve the
data nto a temporary file and then #parse() it. I was hoping there might
be a more direct way.
Thanks,
Scott
Re: #parse($stringValue)
Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Scott Eade wrote:
>
> What is the appropriate way to go about parsing the contents of a
> variable?
>
> A couple of examples:
>
> 1. What if my application's skin defines some text to be replaced
> that is then used in another skin variable?
> adminTitle = Systems Administrator
> contactMsg = Please contact your $ui.adminTitle if you encounter problems.
>
Do you mean something like
#set($msg = "Please contact your $ui.adminTitle if you encounter
problems")
which will 'render' into $msg the string with the value of
$ui.adminTitle in the right place...
Double quotes in a directive/reference arg will always force a render
of the context. Single quotes won't :
#set($foo = 'hello')
#set($msg1 = "$foo")
#set($msg2 = '$foo')
$msg1
$msg2
will output
hello
$foo
> 2. What if I want to save a ui variable to the database - e.g. I want
> to be able to save and edit my help text as part of my application. In
> order to do this, my help text will need to use $ui.adminTitle.
>
> How do I go about parsing the following terms so that $ui.adminTitle is
> displayed as "System Administrator"?
>
> $ui.contactMsg
> $SomeDBObject.SomeColumn (data contains $ui.adminTitle)
The same way should work :
$SomeDBObject.SomeColumn("Contact $ui.adminTitle")
should pass 'Contact System Administrator' to the method 'SomeColumn(
String )' of the object refrenced by $SomeDBObject.
Hope I understood the question.
geir
--
Geir Magnusson Jr. geirm@optonline.net
System and Software Consulting
Developing for the web? See http://jakarta.apache.org/velocity/
Well done is better than well said - New England Proverb